Applied requested changes and eliminated the 'Item' class

This commit is contained in:
adityadev92 2024-01-03 15:50:25 +05:30
parent 52d5ffc8b4
commit d04e29c319

View File

@ -5,64 +5,77 @@ class GildedRose
end end
def update_quality() def update_quality()
# Loop through each item in the list
@items.each do |item| @items.each do |item|
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert" # Check the name of the item to determine the update rules
if item.quality > 0 case item.name
if item.name != "Sulfuras, Hand of Ragnaros" when "Aged Brie"
item.quality = item.quality - 1 increase_quality(item) # Increase quality of Aged Brie
end when "Backstage passes to a TAFKAL80ETC concert"
end handle_backstage_pass(item) # Handle Backstage Passes quality
when "Sulfuras, Hand of Ragnaros"
# Sulfuras does not change in quality or sell_in, so skip to the next item
next
when "Conjured"
degrade_quality(item, 2) # Degrade quality twice as fast for Conjured items
else else
if item.quality < 50 degrade_quality(item) # Normal degradation for other items
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 end
decrease_sell_in(item) # Decrease the sell_in for all items
# Check if the item has expired
if item.sell_in < 0 if item.sell_in < 0
if item.name != "Aged Brie" handle_expired_item(item) # Handle item quality after expiration
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
end
class Item private
attr_accessor :name, :sell_in, :quality
def initialize(name, sell_in, quality) def increase_quality(item, rate = 1)
@name = name # Increase the quality of an item if it's less than 50 (maximum quality)
@sell_in = sell_in item.quality += rate if item.quality < 50
@quality = quality
end end
def to_s() def handle_backstage_pass(item)
"#{@name}, #{@sell_in}, #{@quality}" # Increase quality based on sell_in period for Backstage Passes
if item.sell_in > 10
increase_quality(item) # More than 10 days left: quality increases by 1
elsif item.sell_in > 5
increase_quality(item, 2) # 6-10 days left: quality increases by 2
elsif item.sell_in >= 0
increase_quality(item, 3) # 1-5 days left: quality increases by 3
else
item.quality = 0 # Quality drops to 0 after the concert
end
end
def degrade_quality(item, rate = 1)
# Decrease the quality of the item, accounting for Conjured items that degrade twice as fast
if item.name == "Conjured"
item.quality -= rate * 2 if item.quality > 0
else
item.quality -= rate if item.quality > 0
end
end
def decrease_sell_in(item)
# Decrease the sell_in value for all items except Sulfuras
item.sell_in -= 1 unless item.name == "Sulfuras, Hand of Ragnaros"
end
def handle_expired_item(item)
# Handle item quality after expiration based on item name
case item.name
when "Aged Brie"
increase_quality(item) # Quality increases after expiration for Aged Brie
when "Backstage passes to a TAFKAL80ETC concert"
item.quality = 0 # Quality drops to 0 after the concert
when "Conjured"
degrade_quality(item, 4) # Conjured items degrade twice as fast after expiration
else
degrade_quality(item, 2) # Normal quality degradation after expiration for other items
end
end end
end end