-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcampsite.py
More file actions
84 lines (66 loc) · 2.45 KB
/
campsite.py
File metadata and controls
84 lines (66 loc) · 2.45 KB
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
class Item:
def __init__(self, name, expiry, quality):
self.name = name
self.expiry = expiry
self.quality = quality
class StoreInventory:
def __init__(self, items=None):
if items is None:
items = []
self.items = items
def update_quality(self):
for item in self.items:
if item.name == 'Instant Ramen':
continue
if item.name == 'Cheddar Cheese':
if item.quality < 50:
item.quality += 1
item.expiry -= 1
if item.name != 'Cheddar Cheese' and item.name != 'Instant Ramen':
item.expiry -= 1
if item.expiry > 0 and item.quality > 0:
item.quality -= 1
if item.expiry < 0:
if item.quality > 1:
item.quality -= 2
elif item.quality == 1:
item.quality -= 1
return self.items
items = [
Item("Apple", 10, 10),
Item("Banana", 7, 9),
Item("Strawberry", 5, 10),
Item("Cheddar Cheese", 10, 16),
Item("Instant Ramen", 0, 5),
# this Organic item does not work properly yet
Item("Organic Avocado", 5, 16)
]
store_inventory = StoreInventory(items)
days = 2
for i in range(days):
print(f"Day {i} ---------------------------------")
print(" name expiry quality")
data = [(element.name, element.expiry, element.quality) for element in items]
for item in data:
print(item)
print()
store_inventory.update_quality()
# Unit Tests
# Python's built-in unittest module can be used for writing tests
# It's not exactly the same as the original TypeScript test code, but serves the same purpose
import unittest
class TestItem(unittest.TestCase):
def test_quality_decreases_daily(self):
print("Running test: Quality decreases daily")
test_items = [Item("test", 10, 10)]
test_inventory = StoreInventory(test_items)
test_inventory.update_quality()
self.assertEqual(test_items[0].quality, 9)
def test_cheddar_cheese_quality_increases_daily(self):
print("Running test: Cheddar Cheese increases daily")
test_items = [Item("Cheddar Cheese", 10, 10)]
test_inventory = StoreInventory(test_items)
test_inventory.update_quality()
self.assertEqual(test_items[0].quality, 11)
if __name__ == '__main__':
unittest.main()