From 8d30344e643c61a4e3c8b6d833aa647dbb9b9850 Mon Sep 17 00:00:00 2001 From: Denis Date: Sun, 23 Apr 2023 16:09:37 +0300 Subject: [PATCH] refactoring python code --- python/gilded_rose.py | 49 +++++++++++++++++++------------------- python/test_gilded_rose.py | 4 ++-- python/texttest_fixture.py | 2 +- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/python/gilded_rose.py b/python/gilded_rose.py index 4f21ea64..60f451fe 100755 --- a/python/gilded_rose.py +++ b/python/gilded_rose.py @@ -1,4 +1,8 @@ # -*- coding: utf-8 -*- +SULFURAS = "Sulfuras, Hand of Ragnaros" +BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert" +AGED_BRIE = "Aged Brie" + class GildedRose(object): @@ -7,33 +11,30 @@ 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 + increment = 1 + if item.name != AGED_BRIE and item.name != BACKSTAGE_PASSES and item.name != SULFURAS: + self.regulateQuality(-increment, item) 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 + self.regulateQuality(increment, item) + if item.name == BACKSTAGE_PASSES: + if item.sell_in < 11 or item.sell_in < 6: + self.regulateQuality(increment, item) + + if item.name != SULFURAS: + item.sell_in = item.sell_in - increment 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 != AGED_BRIE and item.name != BACKSTAGE_PASSES and item.name != SULFURAS: + self.regulateQuality(-increment, item) else: - if item.quality < 50: - item.quality = item.quality + 1 + if item.name == BACKSTAGE_PASSES: + item.quality = item.quality - item.quality + elif item.name == AGED_BRIE: + self.regulateQuality(increment, item) + + def regulateQuality(self, increment, item): + newQuality = item.quality + increment + if 0 <= newQuality <= 50: + item.quality = newQuality class Item: diff --git a/python/test_gilded_rose.py b/python/test_gilded_rose.py index 616934e0..2392854f 100644 --- a/python/test_gilded_rose.py +++ b/python/test_gilded_rose.py @@ -9,8 +9,8 @@ class GildedRoseTest(unittest.TestCase): items = [Item("foo", 0, 0)] gilded_rose = GildedRose(items) gilded_rose.update_quality() - self.assertEquals("fixme", items[0].name) + self.assertEquals("foo", items[0].name) + - if __name__ == '__main__': unittest.main() diff --git a/python/texttest_fixture.py b/python/texttest_fixture.py index 86af5ef7..9cce5833 100644 --- a/python/texttest_fixture.py +++ b/python/texttest_fixture.py @@ -4,7 +4,7 @@ from __future__ import print_function from gilded_rose import * if __name__ == "__main__": - print ("OMGHAI!") + print("OMGHAI!") items = [ Item(name="+5 Dexterity Vest", sell_in=10, quality=20), Item(name="Aged Brie", sell_in=2, quality=0),