-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgilded_rose.py
executable file
·100 lines (71 loc) · 2.48 KB
/
gilded_rose.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
class DefaultUpdater(object):
max_quality = 50
def __init__(self, item):
self.item = item
def increase_quality(self):
if self.item.quality < self.max_quality:
self.item.quality = self.item.quality + 1
def decrease_quality(self):
if self.item.quality > 0:
self.item.quality = self.item.quality - 1
def decrease_sell_in(self):
self.item.sell_in = self.item.sell_in - 1
def update_quality(self):
self.decrease_quality()
self.decrease_sell_in()
if self.item.sell_in < 0:
self.decrease_quality()
class AgedBrieUpdater(DefaultUpdater):
def update_quality(self):
self.increase_quality()
self.decrease_sell_in()
if self.item.sell_in < 0:
self.increase_quality()
class BackStageUpdater(DefaultUpdater):
days_threshold_min = 10
days_threshold_max = 5
def reset_quality(self):
self.item.quality = 0
def update_quality(self):
self.increase_quality()
if self.item.sell_in <= self.days_threshold_min:
self.increase_quality()
if self.item.sell_in <= self.days_threshold_max:
self.increase_quality()
self.decrease_sell_in()
if self.item.sell_in < 0:
self.reset_quality()
class SulfurasUpdater(DefaultUpdater):
def update_quality(self):
pass
class ConjuredUpdater(DefaultUpdater):
def decrease_quality(self):
DefaultUpdater.decrease_quality(self)
DefaultUpdater.decrease_quality(self)
class ItemUpdaterFactory(object):
class_mapping = {
"Aged Brie": AgedBrieUpdater,
"Sulfuras, Hand of Ragnaros": SulfurasUpdater,
"Backstage passes to a TAFKAL80ETC concert": BackStageUpdater,
"Conjured Mana Cake": ConjuredUpdater
}
@classmethod
def create(cls, item):
if item.name in cls.class_mapping:
return cls.class_mapping[item.name](item)
return DefaultUpdater(item)
class GildedRose(object):
def __init__(self, items):
self.items = items
def update_quality(self):
for item in self.items:
itemUpdater = ItemUpdaterFactory.create(item)
itemUpdater.update_quality()
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)