From c1f9242acfb089a8c580acb6743dd8907160c1ac Mon Sep 17 00:00:00 2001 From: pbonaldy Date: Mon, 20 Sep 2021 12:03:48 -0300 Subject: [PATCH] resolve kata --- python/gilded_rose.py | 39 +--------------------- python/item.py | 67 ++++++++++++++++++++++++++++++++++++++ python/test_gilded_rose.py | 7 ++-- python/texttest_fixture.py | 19 ++++++----- 4 files changed, 82 insertions(+), 50 deletions(-) create mode 100644 python/item.py diff --git a/python/gilded_rose.py b/python/gilded_rose.py index 4f21ea64..8f0869f0 100755 --- a/python/gilded_rose.py +++ b/python/gilded_rose.py @@ -1,46 +1,9 @@ # -*- coding: utf-8 -*- class GildedRose(object): - def __init__(self, items): self.items = items 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 - else: - if item.quality < 50: - item.quality = item.quality + 1 - - -class Item: - def __init__(self, name, sell_in, quality): - self.name = name - self.sell_in = sell_in - self.quality = quality - - def __repr__(self): - return "%s, %s, %s" % (self.name, self.sell_in, self.quality) + item.evaluate() diff --git a/python/item.py b/python/item.py new file mode 100644 index 00000000..864ade6b --- /dev/null +++ b/python/item.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +from abc import ABC, abstractmethod + + +class Item: + def __init__(self, name, sell_in, quality): + self.name = name + self.sell_in = sell_in + self.quality = quality + + def __repr__(self): + return "%s, %s, %s" % (self.name, self.sell_in, self.quality) + + @abstractmethod + def evaluate(self): + pass + + def increment(self): + self.quality += 1 + + def decrement(self): + self.quality -= 1 + + def sold(self): + self.sell_in -= 1 + + +class AgedBrie(Item): + def evaluate(self): + if self.quality < 50: + self.increment() + self.sold() + if self.sell_in < 0 and self.quality < 50: + self.increment() + + +class Backstage(Item): + def evaluate(self): + if self.quality < 50: + self.increment() + if self.sell_in < 11: + if self.quality < 50: + self.increment() + if self.sell_in < 6: + if self.quality < 50: + self.increment() + self.sold() + if self.sell_in < 0 and self.quality < 50: + self.increment() + + +class Sulfuras(Item): + def evaluate(self): + if self.quality < 50: + self.increment() + if self.sell_in < 0 and self.quality < 50: + self.increment() + + +class General(Item): + def evaluate(self): + if self.quality > 0: + self.decrement() + self.sold() + if self.sell_in < 0: + if self.quality > 0: + self.decrement() diff --git a/python/test_gilded_rose.py b/python/test_gilded_rose.py index 616934e0..574c24ad 100644 --- a/python/test_gilded_rose.py +++ b/python/test_gilded_rose.py @@ -1,15 +1,16 @@ # -*- coding: utf-8 -*- import unittest -from gilded_rose import Item, GildedRose +from gilded_rose import * +from item import * class GildedRoseTest(unittest.TestCase): def test_foo(self): - items = [Item("foo", 0, 0)] + items = [General("foo", 0, 0)] gilded_rose = GildedRose(items) gilded_rose.update_quality() - self.assertEquals("fixme", items[0].name) + self.assertEqual("foo", items[0].name) if __name__ == '__main__': diff --git a/python/texttest_fixture.py b/python/texttest_fixture.py index 86af5ef7..e732a68d 100644 --- a/python/texttest_fixture.py +++ b/python/texttest_fixture.py @@ -2,19 +2,20 @@ from __future__ import print_function from gilded_rose import * +from item import * if __name__ == "__main__": print ("OMGHAI!") items = [ - Item(name="+5 Dexterity Vest", sell_in=10, quality=20), - Item(name="Aged Brie", sell_in=2, quality=0), - Item(name="Elixir of the Mongoose", sell_in=5, quality=7), - Item(name="Sulfuras, Hand of Ragnaros", sell_in=0, quality=80), - Item(name="Sulfuras, Hand of Ragnaros", sell_in=-1, quality=80), - Item(name="Backstage passes to a TAFKAL80ETC concert", sell_in=15, quality=20), - Item(name="Backstage passes to a TAFKAL80ETC concert", sell_in=10, quality=49), - Item(name="Backstage passes to a TAFKAL80ETC concert", sell_in=5, quality=49), - Item(name="Conjured Mana Cake", sell_in=3, quality=6), # <-- :O + General(name="+5 Dexterity Vest", sell_in=10, quality=20), + AgedBrie(name="Aged Brie", sell_in=2, quality=0), + General(name="Elixir of the Mongoose", sell_in=5, quality=7), + Sulfuras(name="Sulfuras, Hand of Ragnaros", sell_in=0, quality=80), + Sulfuras(name="Sulfuras, Hand of Ragnaros", sell_in=-1, quality=80), + Backstage(name="Backstage passes to a TAFKAL80ETC concert", sell_in=15, quality=20), + Backstage(name="Backstage passes to a TAFKAL80ETC concert", sell_in=10, quality=49), + Backstage(name="Backstage passes to a TAFKAL80ETC concert", sell_in=5, quality=49), + General(name="Conjured Mana Cake", sell_in=3, quality=6), # <-- :O ] days = 2