From 320445b08bd6396dc45d8f0706698d0b9f962315 Mon Sep 17 00:00:00 2001 From: abhishekpixo07 <151621651+abhishekpixo07@users.noreply.github.com> Date: Thu, 7 Dec 2023 15:12:37 +0530 Subject: [PATCH] Update gilded_rose.rb --- ruby/gilded_rose.rb | 111 +++++++++++++++++++++++++------------------- 1 file changed, 63 insertions(+), 48 deletions(-) diff --git a/ruby/gilded_rose.rb b/ruby/gilded_rose.rb index e177a497..aec5faa2 100644 --- a/ruby/gilded_rose.rb +++ b/ruby/gilded_rose.rb @@ -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