From 9f7120c785ec44a3601367ee69c65c042c37dd93 Mon Sep 17 00:00:00 2001 From: Ashwin Rana <119340735+gaiters-cafes@users.noreply.github.com> Date: Wed, 27 Sep 2023 17:52:27 +0000 Subject: [PATCH] refactored code using classes for readability + conjured handler --- python/gilded_rose.py | 51 +++++++++++------------------ python/item_handler.py | 44 +++++++++++++++++++++++++ python/tests/test_backstage_pass.py | 6 ---- python/tests/test_conjured.py | 9 +++++ 4 files changed, 73 insertions(+), 37 deletions(-) create mode 100644 python/item_handler.py create mode 100644 python/tests/test_conjured.py diff --git a/python/gilded_rose.py b/python/gilded_rose.py index b488ac91..573f4b13 100755 --- a/python/gilded_rose.py +++ b/python/gilded_rose.py @@ -1,4 +1,11 @@ # -*- coding: utf-8 -*- +from item_handler import ( + AgedBrieHandler, + BackstagePassesHandler, + DefaultItemHandler, + SulfurasHandler, + ConjuredItemHandler) + class GildedRose(object): @@ -7,42 +14,24 @@ class GildedRose(object): self.AGED_BRIE = "Aged Brie" self.SULFURAS = "Sulfuras, Hand of Ragnaros" self.BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert" + self.CONJURED = "Conjured Mana Cake" def update_quality(self): for item in self.items: - self.update_each_item_quality(item) + self.__update_each_item_quality(item) # print(f'Log: {item}\n') - def update_each_item_quality(self, item): - # Update item.sell_in - if item.name != self.SULFURAS: - item.sell_in = item.sell_in - 1 - # Update item.quality - if item.name != self.AGED_BRIE and item.name != self.BACKSTAGE_PASSES: - if item.quality > 0: - if item.name != self.SULFURAS: - item.quality = item.quality - 1 - else: - if item.quality < 50: - item.quality = item.quality + 1 - if item.name == self.BACKSTAGE_PASSES: - if item.sell_in < 10: - if item.quality < 50: - item.quality = item.quality + 1 - if item.sell_in < 5: - if item.quality < 50: - item.quality = item.quality + 1 - if item.sell_in < 0: - if item.name != self.AGED_BRIE: - if item.name != self.BACKSTAGE_PASSES: - if item.quality > 0: - if item.name != self.SULFURAS: - item.quality = item.quality - 1 - else: - item.quality = item.quality - item.quality - else: - if item.quality < 50: - item.quality = item.quality + 1 + def __update_each_item_quality(self, item): + ITEM_SWITCH = { + self.AGED_BRIE: AgedBrieHandler(), + self.SULFURAS: SulfurasHandler(), + self.BACKSTAGE_PASSES: BackstagePassesHandler(), + self.CONJURED: ConjuredItemHandler() + } + item.handler = ITEM_SWITCH.get(item.name, DefaultItemHandler()) + + item.handler.update_sell_in(item) + item.handler.update_quality(item) class Item: diff --git a/python/item_handler.py b/python/item_handler.py new file mode 100644 index 00000000..e42851b1 --- /dev/null +++ b/python/item_handler.py @@ -0,0 +1,44 @@ + +class DefaultItemHandler(object): + def _decrease_item_quality(self, item, amount: int = 1): + item.quality = max(0, item.quality - amount) + + def _increase_item_quality(self, item, amount: int = 1): + item.quality = min(50, item.quality + amount) + + def update_quality(self, item): + self._decrease_item_quality(item) + if item.sell_in < 0: + self._decrease_item_quality(item) + + def update_sell_in(self, item): + item.sell_in = item.sell_in - 1 + +class AgedBrieHandler(DefaultItemHandler): + def update_quality(self, item): + self._increase_item_quality(item) + if item.sell_in < 0: + self._increase_item_quality(item) + +class BackstagePassesHandler(DefaultItemHandler): + def update_quality(self, item): + self._increase_item_quality(item) + if item.sell_in < 10: + self._increase_item_quality(item) + if item.sell_in < 5: + self._increase_item_quality(item) + if item.sell_in < 0: + item.quality = 0 + +class SulfurasHandler(DefaultItemHandler): + def update_quality(self, item): + pass + + def update_sell_in(self, item): + pass + +class ConjuredItemHandler(DefaultItemHandler): + def update_quality(self, item): + self._decrease_item_quality(item, 2) + if item.sell_in < 0: + self._decrease_item_quality(item, 2) diff --git a/python/tests/test_backstage_pass.py b/python/tests/test_backstage_pass.py index 01a44559..1ba6998a 100644 --- a/python/tests/test_backstage_pass.py +++ b/python/tests/test_backstage_pass.py @@ -1,11 +1,5 @@ from gilded_rose import Item, GildedRose -''' -"Backstage passes", like aged brie, increases in Quality as its SellIn value approaches; -Quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less but -Quality drops to 0 after the concert -''' - BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert" # Backstage passes, increases in Quality as its SellIn value approaches diff --git a/python/tests/test_conjured.py b/python/tests/test_conjured.py new file mode 100644 index 00000000..693cfa52 --- /dev/null +++ b/python/tests/test_conjured.py @@ -0,0 +1,9 @@ +from gilded_rose import Item, GildedRose +CONJURED = "Conjured Mana Cake" + +# "Conjured" items degrade in Quality twice as fast as normal items +def test_conjured_item_degrades_twice(): + items = [Item(CONJURED, 1, 12)] + gilded_rose = GildedRose(items) + gilded_rose.update_quality() + assert items[0].quality == 10