Merge pull request #2 from zoengsw/refactor-gildedrose

Refactor gildedrose
This commit is contained in:
zoengsw 2025-11-11 10:02:09 +00:00 committed by GitHub
commit 821867f148
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,39 +1,46 @@
# -*- coding: utf-8 -*-
class GildedRose(object):
BACKSTAGE = 'Backstage passes to a TAFKAL80ETC concert'
SULFURAS = 'Sulfuras, Hand of Ragnaros'
AGED_BRIE = 'Aged Brie'
MAX_QUAL = 50
def __init__(self, items):
self.items = items
def __qual_inc(self, item, inc):
item.quality = min(self.MAX_QUAL, item.quality + inc)
def __qual_dec(self, item, dec):
item.quality = max(0, item.quality - dec)
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
match item.name:
case self.AGED_BRIE:
if item.sell_in > 0:
self.__qual_inc(item, 1)
else:
item.quality = item.quality - item.quality
else:
if item.quality < 50:
item.quality = item.quality + 1
self.__qual_inc(item, 2)
case self.SULFURAS:
pass
case self.BACKSTAGE:
if item.sell_in > 10:
self.__qual_inc(item, 1)
elif item.sell_in > 5:
self.__qual_inc(item, 2)
elif item.sell_in > 0:
self.__qual_inc(item, 3)
else:
item.quality = 0
case _:
if item.sell_in > 0:
self.__qual_dec(item, 1)
else:
self.__qual_dec(item, 2)
if not item.name == self.SULFURAS:
item.sell_in -= 1
class Item: