From bca075ea1151c7fbe87c07fd93e81859f76c28a6 Mon Sep 17 00:00:00 2001 From: Naren2055 Date: Sun, 16 Feb 2025 18:54:41 +0530 Subject: [PATCH] added abstract factory pattern to improve the maintainability of the code --- python/components/gilded_rose.py | 15 ++++++++++----- python/src/aged_brie.py | 16 ++++++++++++++++ python/src/backstage_passes.py | 19 +++++++++++++++++++ python/src/conjured.py | 17 +++++++++++++++++ python/src/main.py | 18 ++++++++++++++++++ python/src/normal_item_logic.py | 15 +++++++++++++++ python/src/sulfuras.py | 10 ++++++++++ 7 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 python/src/aged_brie.py create mode 100644 python/src/backstage_passes.py create mode 100644 python/src/conjured.py create mode 100644 python/src/main.py create mode 100644 python/src/normal_item_logic.py create mode 100644 python/src/sulfuras.py diff --git a/python/components/gilded_rose.py b/python/components/gilded_rose.py index 723e03f3..be2b14d7 100644 --- a/python/components/gilded_rose.py +++ b/python/components/gilded_rose.py @@ -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): diff --git a/python/src/aged_brie.py b/python/src/aged_brie.py new file mode 100644 index 00000000..b0bb4ef9 --- /dev/null +++ b/python/src/aged_brie.py @@ -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 \ No newline at end of file diff --git a/python/src/backstage_passes.py b/python/src/backstage_passes.py new file mode 100644 index 00000000..8481680c --- /dev/null +++ b/python/src/backstage_passes.py @@ -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 \ No newline at end of file diff --git a/python/src/conjured.py b/python/src/conjured.py new file mode 100644 index 00000000..e0ce5109 --- /dev/null +++ b/python/src/conjured.py @@ -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 \ No newline at end of file diff --git a/python/src/main.py b/python/src/main.py new file mode 100644 index 00000000..870e2a62 --- /dev/null +++ b/python/src/main.py @@ -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) \ No newline at end of file diff --git a/python/src/normal_item_logic.py b/python/src/normal_item_logic.py new file mode 100644 index 00000000..f29c51f5 --- /dev/null +++ b/python/src/normal_item_logic.py @@ -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 \ No newline at end of file diff --git a/python/src/sulfuras.py b/python/src/sulfuras.py new file mode 100644 index 00000000..5ecd1c08 --- /dev/null +++ b/python/src/sulfuras.py @@ -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 \ No newline at end of file