From 2f87ffd74e80de4a6c9c4dc35cdf7251961fb871 Mon Sep 17 00:00:00 2001 From: pwnkmrshah Date: Fri, 8 Dec 2023 14:26:21 +0530 Subject: [PATCH] Updated inventory modification code --- ruby/gilded_rose.rb | 106 ++++++++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 44 deletions(-) diff --git a/ruby/gilded_rose.rb b/ruby/gilded_rose.rb index e177a497..31c73beb 100644 --- a/ruby/gilded_rose.rb +++ b/ruby/gilded_rose.rb @@ -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