mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 09:11:39 +00:00
Refactor update_quality for readability
This commit is contained in:
parent
605b66eecc
commit
30bbbd084b
@ -7,34 +7,65 @@ class GildedRose(object):
|
|||||||
|
|
||||||
def update_quality(self):
|
def update_quality(self):
|
||||||
for item in self.items:
|
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
|
|
||||||
|
|
||||||
|
# Legendary item: no change
|
||||||
|
if item.name == "Sulfuras, Hand of Ragnaros":
|
||||||
|
continue
|
||||||
|
|
||||||
|
# STEP 1 — Update quality before sell-in
|
||||||
|
if item.name == "Aged Brie":
|
||||||
|
self._increase_quality(item)
|
||||||
|
|
||||||
|
elif item.name == "Backstage passes to a TAFKAL80ETC concert":
|
||||||
|
self._update_backstage(item)
|
||||||
|
|
||||||
|
elif "Conjured" in item.name:
|
||||||
|
# Conjured before expiry = -2
|
||||||
|
self._decrease_quality(item)
|
||||||
|
self._decrease_quality(item)
|
||||||
|
|
||||||
|
else:
|
||||||
|
self._decrease_quality(item)
|
||||||
|
|
||||||
|
# STEP 2 — decrease sell_in
|
||||||
|
item.sell_in -= 1
|
||||||
|
|
||||||
|
# STEP 3 — Handle expired items
|
||||||
|
if item.sell_in < 0:
|
||||||
|
|
||||||
|
if item.name == "Aged Brie":
|
||||||
|
self._increase_quality(item)
|
||||||
|
|
||||||
|
elif item.name == "Backstage passes to a TAFKAL80ETC concert":
|
||||||
|
item.quality = 0
|
||||||
|
|
||||||
|
elif "Conjured" in item.name:
|
||||||
|
# Conjured after expiry = -4 total
|
||||||
|
self._decrease_quality(item)
|
||||||
|
self._decrease_quality(item)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Normal items degrade -2 after expiry
|
||||||
|
self._decrease_quality(item)
|
||||||
|
|
||||||
|
# --------------------------
|
||||||
|
# Helper methods
|
||||||
|
# --------------------------
|
||||||
|
|
||||||
|
def _increase_quality(self, item):
|
||||||
|
if item.quality < 50:
|
||||||
|
item.quality += 1
|
||||||
|
|
||||||
|
def _decrease_quality(self, item):
|
||||||
|
if item.quality > 0:
|
||||||
|
item.quality -= 1
|
||||||
|
|
||||||
|
def _update_backstage(self, item):
|
||||||
|
self._increase_quality(item)
|
||||||
|
if item.sell_in < 11:
|
||||||
|
self._increase_quality(item)
|
||||||
|
if item.sell_in < 6:
|
||||||
|
self._increase_quality(item)
|
||||||
|
|
||||||
class Item:
|
class Item:
|
||||||
def __init__(self, name, sell_in, quality):
|
def __init__(self, name, sell_in, quality):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user