diff --git a/python/gilded_rose.py b/python/gilded_rose.py index 4f21ea64..4e800146 100755 --- a/python/gilded_rose.py +++ b/python/gilded_rose.py @@ -7,34 +7,65 @@ class GildedRose(object): def update_quality(self): 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 - 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 + # Legendary item: no change + if item.name == "Sulfuras, Hand of Ragnaros": + continue + + # STEP 1 — Update quality before sell-in + if item.name == "Aged Brie": + self._increase_quality(item) + + elif item.name == "Backstage passes to a TAFKAL80ETC concert": + self._update_backstage(item) + + elif "Conjured" in item.name: + # Conjured before expiry = -2 + self._decrease_quality(item) + self._decrease_quality(item) + + else: + self._decrease_quality(item) + + # STEP 2 — decrease sell_in + item.sell_in -= 1 + + # STEP 3 — Handle expired items + if item.sell_in < 0: + + if item.name == "Aged Brie": + self._increase_quality(item) + + elif item.name == "Backstage passes to a TAFKAL80ETC concert": + item.quality = 0 + + elif "Conjured" in item.name: + # Conjured after expiry = -4 total + self._decrease_quality(item) + self._decrease_quality(item) + + else: + # Normal items degrade -2 after expiry + self._decrease_quality(item) + + # -------------------------- + # Helper methods + # -------------------------- + + def _increase_quality(self, item): + if item.quality < 50: + item.quality += 1 + + def _decrease_quality(self, item): + if item.quality > 0: + item.quality -= 1 + + def _update_backstage(self, item): + self._increase_quality(item) + if item.sell_in < 11: + self._increase_quality(item) + if item.sell_in < 6: + self._increase_quality(item) class Item: def __init__(self, name, sell_in, quality):