Update gilded_rose.rb

This commit is contained in:
abhishekpixo07 2023-12-07 15:12:37 +05:30 committed by GitHub
parent 2e30c563d8
commit 320445b08b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,56 +1,71 @@
class GildedRose
def initialize(items)
@items = items
def self.update_quality(items)
items.each do |item|
update_item_quality(item)
end
end
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
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
end
private
def self.update_item_quality(item)
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"
# Sulfuras does not change, nothing to do
when "Conjured"
update_conjured(item)
else
update_normal_item(item)
end
end
def self.update_aged_brie(item)
increase_quality(item)
decrease_sell_in(item)
increase_quality(item) if item.sell_in.negative?
end
def self.update_backstage_passes(item)
increase_quality(item)
decrease_sell_in(item)
if item.sell_in < 10
increase_quality(item)
end
if item.sell_in < 5
increase_quality(item)
end
item.quality = 0 if item.sell_in.negative?
end
def self.update_conjured(item)
decrease_quality(item, 2)
decrease_sell_in(item)
end
def self.update_normal_item(item)
decrease_quality(item)
decrease_sell_in(item)
decrease_quality(item, 1) if item.sell_in.negative?
end
def self.decrease_quality(item, amount = 1)
item.quality -= amount if item.quality.positive?
end
def self.increase_quality(item)
item.quality += 1 if item.quality < 50
end
def self.decrease_sell_in(item)
item.sell_in -= 1 unless item.name == "Sulfuras, Hand of Ragnaros"
item.sell_in = 0 if item.name == "Aged Brie" && item.sell_in.negative?
end
end
class Item