mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 09:11:39 +00:00
Refactoring
Signed-off-by: Jean-Maxime <jm.fillau@gmail.com>
This commit is contained in:
parent
d7d0603cb9
commit
5ddd6ec0fb
@ -1,5 +1,8 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import ClassVar, Dict, Type
|
||||||
|
|
||||||
class GildedRose(object):
|
class GildedRose(object):
|
||||||
|
|
||||||
def __init__(self, items):
|
def __init__(self, items):
|
||||||
@ -7,34 +10,8 @@ class GildedRose(object):
|
|||||||
|
|
||||||
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 = ItemUpdater.update_for(item)
|
||||||
if item.quality > 0:
|
updater.update()
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class Item:
|
class Item:
|
||||||
def __init__(self, name, sell_in, quality):
|
def __init__(self, name, sell_in, quality):
|
||||||
@ -44,3 +21,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)
|
||||||
|
|
||||||
|
@dataclass()
|
||||||
|
class ItemUpdater:
|
||||||
|
|
||||||
|
item: Item
|
||||||
|
|
||||||
|
_registry: ClassVar[Dict[str, Type["ItemUpdater"]]] = {}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def register(cls, name: str):
|
||||||
|
def deco(subcls: Type["ItemUpdater"]) -> Type["ItemUpdater"]:
|
||||||
|
cls._registry[name] = subcls
|
||||||
|
return subcls
|
||||||
|
return deco
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def update_for(cls, item: Item) -> "ItemUpdater":
|
||||||
|
updater_cls = cls._registry.get(item.name, cls)
|
||||||
|
return updater_cls(item)
|
||||||
|
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.item.sell_in = self.item.sell_in - 1
|
||||||
|
|
||||||
|
if self.item.quality > 0:
|
||||||
|
self.item.quality = self.item.quality - 1
|
||||||
|
|
||||||
|
if self.item.sell_in < 0 and self.item.quality > 0:
|
||||||
|
self.item.quality = self.item.quality - 1
|
||||||
|
|
||||||
|
|
||||||
|
@ItemUpdater.register("Aged Brie")
|
||||||
|
class AgedBrieUpdater(ItemUpdater):
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
if self.item.quality < 50:
|
||||||
|
self.item.quality = self.item.quality + 1
|
||||||
|
|
||||||
|
self.item.sell_in = self.item.sell_in - 1
|
||||||
|
|
||||||
|
if self.item.sell_in < 0:
|
||||||
|
if self.item.quality < 50:
|
||||||
|
self.item.quality = self.item.quality + 1
|
||||||
|
|
||||||
|
@ItemUpdater.register("Sulfuras, Hand of Ragnaros")
|
||||||
|
class SulfurasUpdater(ItemUpdater):
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
pass # Legendary item, never changes
|
||||||
|
|
||||||
|
@ItemUpdater.register("Backstage passes to a TAFKAL80ETC concert")
|
||||||
|
class BackstagePassUpdater(ItemUpdater):
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
if self.item.sell_in < 1:
|
||||||
|
self.item.quality = 0 # Concert passed, quality drops to 0
|
||||||
|
elif self.item.sell_in < 6:
|
||||||
|
self.item.quality = min(50, self.item.quality + 3) # 5 days or less: +3
|
||||||
|
elif self.item.sell_in < 11:
|
||||||
|
self.item.quality = min(50, self.item.quality + 2) # 10 days or less: +2
|
||||||
|
else:
|
||||||
|
self.item.quality = min(50, self.item.quality + 1) # More than 10 days: +1
|
||||||
|
|
||||||
|
self.item.sell_in = self.item.sell_in - 1
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user