mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-14 06:01:39 +00:00
final cleaned code with polymorphic classes to handle regular items and special items , i add a function for a new item which is called kitkat
This commit is contained in:
parent
45c7cd85c6
commit
69c5ce701d
@ -6,34 +6,21 @@ class GildedRose(object):
|
|||||||
self.items = items
|
self.items = items
|
||||||
|
|
||||||
def update_quality(self):
|
def update_quality(self):
|
||||||
for item in self.items:
|
for item in self.items:
|
||||||
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert":
|
updater = self.run_the_logic_with_classes_safely(item)
|
||||||
if item.quality > 0:
|
updater.update()
|
||||||
if item.name != "Sulfuras, Hand of Ragnaros":
|
|
||||||
item.quality = item.quality - 1
|
def run_the_logic_with_classes_safely(self, item):
|
||||||
else:
|
if item.name == "Aged Brie":
|
||||||
if item.quality < 50:
|
return AgedBrieUpdater(item)
|
||||||
item.quality = item.quality + 1
|
elif item.name == "Sulfuras, Hand of Ragnaros":
|
||||||
if item.name == "Backstage passes to a TAFKAL80ETC concert":
|
return SulfurasUpdater(item)
|
||||||
if item.sell_in < 11:
|
elif item.name == "Backstage passes to a TAFKAL80ETC concert":
|
||||||
if item.quality < 50:
|
return BackstagePassUpdater(item)
|
||||||
item.quality = item.quality + 1
|
elif item.name == "Conjured kitkats":
|
||||||
if item.sell_in < 6:
|
return ConjuredItemUpdater(item)
|
||||||
if item.quality < 50:
|
else:
|
||||||
item.quality = item.quality + 1
|
return ItemUpdater(item)
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class Item:
|
class Item:
|
||||||
@ -44,3 +31,67 @@ class Item:
|
|||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "%s, %s, %s" % (self.name, self.sell_in, self.quality)
|
return "%s, %s, %s" % (self.name, self.sell_in, self.quality)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#__________________________________________ class to safely add items to the class item instead of if statements__________________________________________________________________________________
|
||||||
|
|
||||||
|
class ItemUpdater:
|
||||||
|
def __init__(self, item):
|
||||||
|
self.item = item
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.decrease_quality()
|
||||||
|
self.decrease_sell_in()
|
||||||
|
if self.item.sell_in < 0:
|
||||||
|
self.decrease_quality()
|
||||||
|
|
||||||
|
def decrease_quality(self):
|
||||||
|
if self.item.quality > 0:
|
||||||
|
self.item.quality -= 1
|
||||||
|
|
||||||
|
def increase_quality(self):
|
||||||
|
if self.item.quality < 50:
|
||||||
|
self.item.quality += 1
|
||||||
|
|
||||||
|
def decrease_sell_in(self):
|
||||||
|
self.item.sell_in -= 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#__________________________________________ classes to update the special cases in update quality function __________________________________________________________________________________
|
||||||
|
|
||||||
|
|
||||||
|
class AgedBrieUpdater(ItemUpdater):
|
||||||
|
def update(self):
|
||||||
|
self.increase_quality()
|
||||||
|
self.decrease_sell_in()
|
||||||
|
if self.item.sell_in < 0:
|
||||||
|
self.increase_quality()
|
||||||
|
|
||||||
|
class SulfurasUpdater(ItemUpdater):
|
||||||
|
def update(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class BackstagePassUpdater(ItemUpdater):
|
||||||
|
def update(self):
|
||||||
|
self.increase_quality()
|
||||||
|
if self.item.sell_in < 11:
|
||||||
|
self.increase_quality()
|
||||||
|
if self.item.sell_in < 6:
|
||||||
|
self.increase_quality()
|
||||||
|
|
||||||
|
self.decrease_sell_in()
|
||||||
|
|
||||||
|
if self.item.sell_in < 0:
|
||||||
|
self.item.quality = 0
|
||||||
|
|
||||||
|
#__________________________________________ class for the new item with a diffrent rule which is decrease twice as fast __________________________________________________________________________________
|
||||||
|
|
||||||
|
|
||||||
|
class ConjuredItemUpdater(ItemUpdater):
|
||||||
|
def decrease_quality(self):
|
||||||
|
if self.item.quality > 0:
|
||||||
|
self.item.quality -= 2
|
||||||
|
if self.item.quality < 0:
|
||||||
|
self.item.quality = 0
|
||||||
Loading…
Reference in New Issue
Block a user