From 3271c2459179ae2e831c65fdab706488627968f6 Mon Sep 17 00:00:00 2001 From: Vasu Bhatia Date: Sun, 7 Dec 2025 18:31:53 +0000 Subject: [PATCH] Implement specific strategy classes for all item types --- python/gilded_rose.py | 145 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/python/gilded_rose.py b/python/gilded_rose.py index 96d5424a..effb7d74 100755 --- a/python/gilded_rose.py +++ b/python/gilded_rose.py @@ -38,6 +38,151 @@ class ItemUpdateStrategy(ABC): """ pass +class NormalItemStrategy(ItemUpdateStrategy): + """Strategy for normal items that degrade in quality over time.""" + + def update_quality(self, item: Item) -> None: + """Decrease quality by 1 before sell date, by 2 after. + + Parameters + ---------- + item : Item + The item to update. + + """ + degradation: int = 2 if item.sell_in < 0 else 1 + item.quality = max(MIN_QUALITY, item.quality - degradation) + + def update_sell_in(self, item: Item) -> None: + """Decrease sell_in by 1 each day. + + Parameters + ---------- + item : Item + The item to update. + + """ + item.sell_in -= 1 + + +class AgedBrieStrategy(ItemUpdateStrategy): + """Strategy for Aged Brie that increases in quality over time.""" + + def update_quality(self, item: Item) -> None: + """Increase quality by 1 before sell date, by 2 after. + + Parameters + ---------- + item : Item + The item to update. + + """ + increase: int = 2 if item.sell_in < 0 else 1 + item.quality = min(MAX_QUALITY, item.quality + increase) + + def update_sell_in(self, item: Item) -> None: + """Decrease sell_in by 1 each day. + + Parameters + ---------- + item : Item + The item to update. + + """ + item.sell_in -= 1 + + +class SulfurasStrategy(ItemUpdateStrategy): + """Strategy for Sulfuras, a legendary item that never changes.""" + + def update_quality(self, item: Item) -> None: + """Sulfuras quality never changes. + + Parameters + ---------- + item : Item + The item to update (no actual change occurs). + + """ + pass + + def update_sell_in(self, item: Item) -> None: + """Sulfuras never needs to be sold. + + Parameters + ---------- + item : Item + The item to update (no actual change occurs). + + """ + pass + + +class BackstagePassStrategy(ItemUpdateStrategy): + """Strategy for Backstage passes with tiered quality appreciation.""" + + def update_quality(self, item: Item) -> None: + """Update quality based on days until concert. + + Quality increases by: + - 1 when more than 10 days remain + - 2 when 6-10 days remain + - 3 when 1-5 days remain + - drops to 0 after concert (sell_in < 0) + + Parameters + ---------- + item : Item + The item to update. + + """ + if item.sell_in < 0: + item.quality = 0 + elif item.sell_in <= 5: + item.quality = min(MAX_QUALITY, item.quality + 3) + elif item.sell_in <= 10: + item.quality = min(MAX_QUALITY, item.quality + 2) + else: + item.quality = min(MAX_QUALITY, item.quality + 1) + + def update_sell_in(self, item: Item) -> None: + """Decrease sell_in by 1 each day. + + Parameters + ---------- + item : Item + The item to update. + + """ + item.sell_in -= 1 + + +class ConjuredItemStrategy(ItemUpdateStrategy): + """Strategy for Conjured items that degrade twice as fast as normal items.""" + + def update_quality(self, item: Item) -> None: + """Decrease quality by 2 before sell date, by 4 after. + + Parameters + ---------- + item : Item + The item to update. + + """ + degradation = 4 if item.sell_in < 0 else 2 + item.quality = max(MIN_QUALITY, item.quality - degradation) + + def update_sell_in(self, item: Item) -> None: + """Decrease sell_in by 1 each day. + + Parameters + ---------- + item : Item + The item to update. + + """ + item.sell_in -= 1 + class GildedRose: """Manages inventory quality updates for the Gilded Rose inn.