mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 09:11:39 +00:00
added abstract factory pattern to improve the maintainability of the code
This commit is contained in:
parent
d61787495e
commit
bca075ea11
@ -1,12 +1,17 @@
|
||||
from abc import abstractmethod, ABC
|
||||
from ..models import Items
|
||||
|
||||
|
||||
class Item:
|
||||
def __init__(self, name, sell_in, quality):
|
||||
self.name = name
|
||||
self.sell_in = sell_in
|
||||
self.quality = quality
|
||||
|
||||
class GildedRose(ABC):
|
||||
def __init__(self, item: str, sell_in: int, quality: int):
|
||||
self.item = item
|
||||
self.quality = quality
|
||||
self.sell_in = sell_in
|
||||
def __init__(self, items: Item):
|
||||
self.name = items.name
|
||||
self.quality = items.quality
|
||||
self.sell_in = items.sell_in
|
||||
|
||||
@abstractmethod
|
||||
def update_quality(self):
|
||||
|
||||
16
python/src/aged_brie.py
Normal file
16
python/src/aged_brie.py
Normal file
@ -0,0 +1,16 @@
|
||||
from python.components.gilded_rose import GildedRose, Item
|
||||
|
||||
|
||||
class AgedBrieLogic(GildedRose):
|
||||
|
||||
def __init__(self, name, quality, sell_in):
|
||||
super().__init__(Item(name, quality, sell_in))
|
||||
|
||||
def update_quality(self):
|
||||
if self.quality < 50:
|
||||
self.quality += 1
|
||||
self.sell_in -= 1
|
||||
if self.sell_in < 0 and self.quality < 50:
|
||||
self.quality += 1
|
||||
|
||||
return self.quality, self.sell_in
|
||||
19
python/src/backstage_passes.py
Normal file
19
python/src/backstage_passes.py
Normal file
@ -0,0 +1,19 @@
|
||||
from python.components.gilded_rose import GildedRose, Item
|
||||
|
||||
|
||||
class BackstagePassesLogic(GildedRose):
|
||||
def __init__(self, name, quality, sell_in):
|
||||
super().__init__(Item(name, quality, sell_in))
|
||||
|
||||
def update_quality(self):
|
||||
if self.quality < 50:
|
||||
self.quality += 1
|
||||
if self.sell_in < 11 and self.quality < 50:
|
||||
self.quality += 1
|
||||
if self.sell_in < 6 and self.quality < 50:
|
||||
self.quality += 1
|
||||
self.sell_in -= 1
|
||||
if self.sell_in < 0:
|
||||
self.quality = 0
|
||||
|
||||
return self.quality, self.sell_in
|
||||
17
python/src/conjured.py
Normal file
17
python/src/conjured.py
Normal file
@ -0,0 +1,17 @@
|
||||
from python.components.gilded_rose import GildedRose, Item
|
||||
|
||||
|
||||
|
||||
|
||||
class ConjuredItem(GildedRose):
|
||||
|
||||
def __init__(self, name, quality, sell_in):
|
||||
super().__init__(Item(name, quality, sell_in))
|
||||
|
||||
def update_quality(self):
|
||||
if self.quality > 0:
|
||||
self.quality -= 2
|
||||
if self.sell_in <= 0 and self.quality > 0:
|
||||
self.quality -= 2
|
||||
self.sell_in -= 1
|
||||
return self.quality, self.sell_in
|
||||
18
python/src/main.py
Normal file
18
python/src/main.py
Normal file
@ -0,0 +1,18 @@
|
||||
from python.src.aged_brie import AgedBrieLogic
|
||||
from python.src.backstage_passes import BackstagePassesLogic
|
||||
from python.src.normal_item_logic import NormalItemLogic
|
||||
from python.src.sulfuras import SulfurasLogic
|
||||
|
||||
|
||||
class UpdateItemLogic:
|
||||
@staticmethod
|
||||
def update_items(items):
|
||||
for item in items:
|
||||
if item.name == "Aged Brie":
|
||||
AgedBrieLogic.update_quality(item)
|
||||
elif item.name == "Backstage passes to a TAFKAL80ETC concert":
|
||||
BackstagePassesLogic.update_quality(item)
|
||||
elif item.name == "Sulfuras, Hand of Ragnaros":
|
||||
SulfurasLogic.update_quality(item)
|
||||
else:
|
||||
NormalItemLogic.update_quality(item)
|
||||
15
python/src/normal_item_logic.py
Normal file
15
python/src/normal_item_logic.py
Normal file
@ -0,0 +1,15 @@
|
||||
from python.components.gilded_rose import GildedRose, Item
|
||||
|
||||
|
||||
class NormalItemLogic(GildedRose):
|
||||
def __init__(self, name, quality, sell_in):
|
||||
super().__init__(Item(name, quality, sell_in))
|
||||
|
||||
def update_quality(self):
|
||||
if self.quality > 0:
|
||||
self.quality -= 1
|
||||
self.sell_in -= 1
|
||||
if self.sell_in < 0 and self.quality > 0:
|
||||
self.quality -= 1
|
||||
|
||||
return self.quality, self.sell_in
|
||||
10
python/src/sulfuras.py
Normal file
10
python/src/sulfuras.py
Normal file
@ -0,0 +1,10 @@
|
||||
from python.components.gilded_rose import GildedRose, Item
|
||||
|
||||
|
||||
class SulfurasLogic(GildedRose):
|
||||
def __init__(self, name, quality, sell_in):
|
||||
super().__init__(Item(name, quality, sell_in))
|
||||
|
||||
def update_quality(self):
|
||||
# "Sulfuras" does not change in quality or sell_in
|
||||
return self.quality, self.sell_in
|
||||
Loading…
Reference in New Issue
Block a user