mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 09:11:39 +00:00
4.Refactor to use constants and enums for all magic values
This commit is contained in:
parent
4dafc71bea
commit
7c52f599a3
@ -1,9 +1,23 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
class ITEM_NAME:
|
||||
AGED_BRIE = "Aged Brie"
|
||||
BACKSTAGE_PASS = "Backstage passes to a TAFKAL80ETC concert"
|
||||
SULFURAS = "Sulfuras, Hand of Ragnaros"
|
||||
CONJURED = "Conjured"
|
||||
|
||||
|
||||
class GildedRose(object):
|
||||
MAX_QUALITY = 50
|
||||
LEGENDARY_QUALITY = 80
|
||||
LEGENDARY_ITEM = ITEM_NAME.SULFURAS
|
||||
BACKSTAGE_PASS_DOUBLE_THRESHOLD = 10 # Quality increases by 2 when ≤ this
|
||||
BACKSTAGE_PASS_TRIPLE_THRESHOLD = 5 # Quality increases by 3 when ≤ this
|
||||
|
||||
def __init__(self, items):
|
||||
self.items = items
|
||||
|
||||
|
||||
def update_quality(self):
|
||||
for item in self.items:
|
||||
self.update_item_quality(item)
|
||||
@ -12,13 +26,13 @@ class GildedRose(object):
|
||||
self.handle_expired_item(item)
|
||||
|
||||
def update_item_quality(self, item):
|
||||
if item.name == "Aged Brie":
|
||||
if item.name == ITEM_NAME.AGED_BRIE:
|
||||
self.update_aged_brie(item)
|
||||
elif item.name == "Backstage passes to a TAFKAL80ETC concert":
|
||||
elif item.name == ITEM_NAME.BACKSTAGE_PASS:
|
||||
self.update_backstage_pass(item)
|
||||
elif "Conjured" in item.name:
|
||||
elif ITEM_NAME.CONJURED in item.name:
|
||||
self.update_conjured_item(item)
|
||||
elif item.name != "Sulfuras, Hand of Ragnaros":
|
||||
elif item.name != ITEM_NAME.SULFURAS:
|
||||
self.update_normal_item(item)
|
||||
|
||||
def update_conjured_item(self, item):
|
||||
@ -26,18 +40,18 @@ class GildedRose(object):
|
||||
self.decrease_quality(item)
|
||||
|
||||
def update_sell_in(self, item):
|
||||
if item.name != "Sulfuras, Hand of Ragnaros":
|
||||
if item.name != ITEM_NAME.SULFURAS:
|
||||
item.sell_in -= 1
|
||||
|
||||
def handle_expired_item(self, item):
|
||||
if item.name == "Aged Brie":
|
||||
if item.name == ITEM_NAME.AGED_BRIE:
|
||||
self.increase_quality(item)
|
||||
elif item.name == "Backstage passes to a TAFKAL80ETC concert":
|
||||
elif item.name == ITEM_NAME.BACKSTAGE_PASS:
|
||||
item.quality = 0
|
||||
elif "Conjured" in item.name:
|
||||
elif ITEM_NAME.CONJURED in item.name:
|
||||
self.decrease_quality(item)
|
||||
self.decrease_quality(item)
|
||||
elif item.name != "Sulfuras, Hand of Ragnaros":
|
||||
elif item.name != ITEM_NAME.SULFURAS:
|
||||
self.decrease_quality(item)
|
||||
|
||||
def update_aged_brie(self, item):
|
||||
@ -45,22 +59,23 @@ class GildedRose(object):
|
||||
|
||||
def update_backstage_pass(self, item):
|
||||
self.increase_quality(item)
|
||||
if item.sell_in < 11:
|
||||
if item.sell_in <= self.BACKSTAGE_PASS_DOUBLE_THRESHOLD:
|
||||
self.increase_quality(item)
|
||||
if item.sell_in < 6:
|
||||
if item.sell_in <= self.BACKSTAGE_PASS_TRIPLE_THRESHOLD:
|
||||
self.increase_quality(item)
|
||||
|
||||
def update_normal_item(self, item):
|
||||
self.decrease_quality(item)
|
||||
|
||||
def increase_quality(self, item):
|
||||
if item.quality < 50:
|
||||
if item.quality < self.MAX_QUALITY:
|
||||
item.quality += 1
|
||||
|
||||
def decrease_quality(self, item):
|
||||
if item.quality > 0:
|
||||
item.quality -= 1
|
||||
|
||||
|
||||
class Item:
|
||||
def __init__(self, name, sell_in, quality):
|
||||
self.name = name
|
||||
|
||||
Loading…
Reference in New Issue
Block a user