-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.py
63 lines (48 loc) · 1.53 KB
/
inventory.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
from typing import Any
class Item():
def __init__(self, name:str, quantity:int=1) -> None:
self.name = name
self.n = quantity
def __repr__(self) -> str:
return f"{self.n}x{self.name}"
def __add__(self, other):
self.n += other
return self
def __sub__(self, other):
self.n -= other
return self
def get_name(self):
return self.name
def get_quantity(self):
return self.n
class Inventory():
pass
class Inventory():
def __init__(self) -> None:
self.content = []
def __repr__(self) -> str:
return "{\n" + str(self.content).replace(", ", "\n").removeprefix("[").removesuffix("]") + "\n}\n"
def sub(self, item:Item) -> None:
name = item.get_name()
for e in self.content:
if type(e) == Item and e.get_name() == name:
e -= item.get_quantity()
return
self.content.append(Item(name, -item.get_quantity()))
def add(self, item:Item) -> None:
name = item.get_name()
for e in self.content:
if type(e) == Item and e.get_name() == name:
e += item.get_quantity()
return
self.content.append(item)
def __add__(self, other:Inventory) -> Inventory:
for i in other:
self.add(i)
return self
def __sub__(self, other:Inventory) -> Inventory:
for i in other:
self.sub(i)
return self
def __iter__(self):
return self.content.__iter__()