Updated inventory modification code

This commit is contained in:
pwnkmrshah 2023-12-08 14:26:21 +05:30
parent 318218e3bc
commit 2f87ffd74e

View File

@ -1,56 +1,74 @@
class GildedRose class GildedRose
def initialize(items) def initialize(items)
@items = items @items = items
end end
def update_quality() def update_quality
@items.each do |item| @items.each do |item|
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert" case item.name
if item.quality > 0 when "Aged Brie"
if item.name != "Sulfuras, Hand of Ragnaros" update_aged_brie(item)
item.quality = item.quality - 1 when "Backstage passes to a TAFKAL80ETC concert"
end update_backstage_passes(item)
end when "Sulfuras, Hand of Ragnaros"
next # Sulfuras remains unchanged, move to the next item
when "Conjured Mana Cake"
update_conjured(item)
else else
if item.quality < 50 update_normal_item(item)
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
end end
end 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 end
class Item class Item
@ -62,7 +80,7 @@ class Item
@quality = quality @quality = quality
end end
def to_s() def to_s
"#{@name}, #{@sell_in}, #{@quality}" "#{@name}, #{@sell_in}, #{@quality}"
end end
end end