mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
Updated inventory modification code
This commit is contained in:
parent
318218e3bc
commit
2f87ffd74e
@ -1,56 +1,74 @@
|
||||
class GildedRose
|
||||
|
||||
def initialize(items)
|
||||
@items = items
|
||||
end
|
||||
|
||||
def update_quality()
|
||||
def update_quality
|
||||
@items.each do |item|
|
||||
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert"
|
||||
if item.quality > 0
|
||||
if item.name != "Sulfuras, Hand of Ragnaros"
|
||||
item.quality = item.quality - 1
|
||||
end
|
||||
end
|
||||
case item.name
|
||||
when "Aged Brie"
|
||||
update_aged_brie(item)
|
||||
when "Backstage passes to a TAFKAL80ETC concert"
|
||||
update_backstage_passes(item)
|
||||
when "Sulfuras, Hand of Ragnaros"
|
||||
next # Sulfuras remains unchanged, move to the next item
|
||||
when "Conjured Mana Cake"
|
||||
update_conjured(item)
|
||||
else
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
if item.name == "Backstage passes to a TAFKAL80ETC concert"
|
||||
if item.sell_in < 11
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
end
|
||||
end
|
||||
if item.sell_in < 6
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if item.name != "Sulfuras, Hand of Ragnaros"
|
||||
item.sell_in = item.sell_in - 1
|
||||
end
|
||||
if item.sell_in < 0
|
||||
if item.name != "Aged Brie"
|
||||
if item.name != "Backstage passes to a TAFKAL80ETC concert"
|
||||
if item.quality > 0
|
||||
if item.name != "Sulfuras, Hand of Ragnaros"
|
||||
item.quality = item.quality - 1
|
||||
end
|
||||
end
|
||||
else
|
||||
item.quality = item.quality - item.quality
|
||||
end
|
||||
else
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
end
|
||||
end
|
||||
update_normal_item(item)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_aged_brie(item)
|
||||
decrease_sell_in(item)
|
||||
increase_quality(item, 2)
|
||||
end
|
||||
|
||||
def update_backstage_passes(item)
|
||||
decrease_sell_in(item)
|
||||
if item.sell_in > 0
|
||||
if item.sell_in <= 5
|
||||
increase_quality(item, 3)
|
||||
elsif item.sell_in <= 10
|
||||
increase_quality(item, 2)
|
||||
else
|
||||
increase_quality(item, 1)
|
||||
end
|
||||
else
|
||||
item.quality = 0
|
||||
end
|
||||
end
|
||||
|
||||
def increase_quality(item, amount)
|
||||
item.quality = [item.quality + amount, 50].min
|
||||
end
|
||||
|
||||
def update_normal_item(item)
|
||||
decrease_quality(item)
|
||||
decrease_quality(item) if expired?(item)
|
||||
decrease_sell_in(item)
|
||||
end
|
||||
|
||||
def update_conjured(item)
|
||||
decrease_quality(item, 2)
|
||||
decrease_quality(item, 2) if expired?(item)
|
||||
decrease_sell_in(item)
|
||||
end
|
||||
|
||||
def decrease_quality(item, rate = 1)
|
||||
item.quality -= rate if item.quality > 0
|
||||
end
|
||||
|
||||
def decrease_sell_in(item)
|
||||
item.sell_in -= 1
|
||||
end
|
||||
|
||||
def expired?(item)
|
||||
item.sell_in <= 0
|
||||
end
|
||||
end
|
||||
|
||||
class Item
|
||||
@ -62,7 +80,7 @@ class Item
|
||||
@quality = quality
|
||||
end
|
||||
|
||||
def to_s()
|
||||
def to_s
|
||||
"#{@name}, #{@sell_in}, #{@quality}"
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user