From 2947ff0f1bc23217338713f305116ca87d5fb72a Mon Sep 17 00:00:00 2001 From: Kunnal-chawla Date: Sun, 28 Dec 2025 10:02:09 +0530 Subject: [PATCH] Refactored GlidedRose class --- python/gilded_rose.py | 80 ++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 27 deletions(-) diff --git a/python/gilded_rose.py b/python/gilded_rose.py index 4f21ea64..90ac4767 100755 --- a/python/gilded_rose.py +++ b/python/gilded_rose.py @@ -1,4 +1,47 @@ -# -*- coding: utf-8 -*- +# utf-8 + +class Updater(object): + + MIN_QUALITY = 0 + MAX_QUALITY = 50 + + def normal_item(self, item): + if item.sell_in > 0: + depreciation = -1 + else: + depreciation = -2 + item.quality = max((item.quality + depreciation), self.MIN_QUALITY) + item.sell_in += -1 + + def aged_brie(self, item): + if item.sell_in > 0: + appreciation = 1 + else: + appreciation = 2 + item.quality = min((item.quality + appreciation), self.MAX_QUALITY) + item.sell_in += -1 + + def sulfuras(self, item): + pass + + def backstage_passes(self, item): + + def get_quality(item, appreciation): + item.quality = min(item.quality + appreciation , self.MAX_QUALITY) + + if item.sell_in > 10: + appreciation = 1 + get_quality(item, appreciation) + elif item.sell_in > 5: + appreciation = 2 + get_quality(item, appreciation) + elif item.sell_in > 0: + appreciation = 3 + get_quality(item, appreciation) + else: + item.quality = 0 + item.sell_in += -1 + class GildedRose(object): @@ -6,34 +49,17 @@ class GildedRose(object): self.items = items def update_quality(self): + updater = Updater() + for item in self.items: - 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 + if item.name == 'Aged Brie': + updater.aged_brie(item) + elif item.name == 'Backstage passes to a TAFKAL80ETC concert': + updater.backstage_passes(item) + elif item.name == 'Sulfuras, Hand of Ragnaros': + updater.sulfuras(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 - if item.sell_in < 6: - if item.quality < 50: - item.quality = item.quality + 1 - if item.name != "Sulfuras, Hand of Ragnaros": - item.sell_in = item.sell_in - 1 - 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 - else: - item.quality = item.quality - item.quality - else: - if item.quality < 50: - item.quality = item.quality + 1 + updater.normal_item(item) class Item: