refactored code using classes for readability + conjured handler

This commit is contained in:
Ashwin Rana 2023-09-27 17:52:27 +00:00
parent 0686f46d73
commit 9f7120c785
4 changed files with 73 additions and 37 deletions

View File

@ -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:

44
python/item_handler.py Normal file
View File

@ -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)

View File

@ -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

View File

@ -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