From a21bde290f3eaebf6d17a13855b527795e9ab97a Mon Sep 17 00:00:00 2001 From: "chandan.goyal" Date: Mon, 10 Feb 2025 20:20:11 +0530 Subject: [PATCH] Refactored the code to make it more readable --- python/gilded_rose.py | 56 ++++++++++++++++++++------------------ python/test_gilded_rose.py | 8 ++++++ 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/python/gilded_rose.py b/python/gilded_rose.py index 4f21ea64..aa7f9552 100755 --- a/python/gilded_rose.py +++ b/python/gilded_rose.py @@ -1,5 +1,19 @@ # -*- coding: utf-8 -*- +def decrease_quality(item, decrease_quality_by=None): + if not decrease_quality_by: + if item.sell_in <= 0: + decrease_quality_by = 2 + else: + decrease_quality_by = 1 + + item.quality = max(item.quality - decrease_quality_by, 0) + + +def increase_quality(item, increase_quality_by=1): + item.quality = min(item.quality + increase_quality_by, 50) + + class GildedRose(object): def __init__(self, items): @@ -7,33 +21,23 @@ class GildedRose(object): def update_quality(self): for item in self.items: - if item.name != "Aged Brie" and 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: - 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 + if item.name == "Sulfuras, Hand of Ragnaros": + continue + elif item.name == "Aged Brie": + increase_quality(item) + elif item.name == "Backstage passes to a TAFKAL80ETC concert": + if item.sell_in <= 0: + item.quality = 0 + elif item.sell_in <= 5: + increase_quality(item, 3) + elif item.sell_in <= 10: + increase_quality(item, 2) else: - if item.quality < 50: - item.quality = item.quality + 1 + increase_quality(item) + else: + decrease_quality(item) + + item.sell_in = item.sell_in - 1 class Item: diff --git a/python/test_gilded_rose.py b/python/test_gilded_rose.py index c342f8aa..0769edd3 100644 --- a/python/test_gilded_rose.py +++ b/python/test_gilded_rose.py @@ -19,6 +19,14 @@ class GildedRoseTest(unittest.TestCase): # Quality should never go below 0 self.assertEqual(0, item.quality) + def test_normal_item_quality_if_sell_in_is_0(self): + item = Item(name="foo", sell_in=0, quality=4) + gilded_rose = GildedRose([item]) + gilded_rose.update_quality() + + # Quality will degrade by 2 once sell in date passed + self.assertEqual(2, item.quality) + def test_aged_brie_quality_exceeded(self): item = Item(name="Aged Brie", sell_in=0, quality=50) gilded_rose = GildedRose([item])