-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyMoney03.py
More file actions
234 lines (190 loc) · 7.46 KB
/
Copy pathPyMoney03.py
File metadata and controls
234 lines (190 loc) · 7.46 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import sys
RECORD_FILE = 'records.txt'
class Record:
def __init__(self, category, description, amount):
self._category = category
self._description = description
self._amount = amount
@property
def category(self): return self._category
@property
def description(self): return self._description
@property
def amount(self): return self._amount
def __str__(self): return f"{self._category} {self._description} {self._amount}"
class Records:
# INIT
def __init__(self):
self._records = []
self._initial_money = 0
try:
with open(RECORD_FILE, 'r') as f:
try: self._initial_money = int(f.readline().strip())
except ValueError:
sys.stderr.write(f"Invalid format in {RECORD_FILE}. Deleting contents.\n")
return
for line in f.readlines():
parts = line.split()
if len(parts) != 3:
sys.stderr.write(f"Invalid format in {RECORD_FILE}. Deleting contents.\n")
self._records = []
return
cat, desc, val = parts
try: val = int(val)
except ValueError:
sys.stderr.write(f"Invalid value in {RECORD_FILE}. Deleting contents.\n")
self._records = []
return
self._records.append(Record(cat, desc, val))
print("Welcome back!")
except FileNotFoundError:
try: self._initial_money = int(input("How much money do you have? "))
except ValueError:
sys.stderr.write("Invalid value for money. Set to 0 by default.\n\n")
self._initial_money = 0
# ADD
def add(self, raw_input, categories):
entries = [x.strip() for x in raw_input.split(",")]
for entry in entries:
parts = entry.split()
if len(parts) != 3:
print("The format should be: meal breakfast -50")
print("Fail to add a record.\n")
return
cat, desc, val = parts
if not val.lstrip("+-").isdigit():
print("Invalid amount.")
print("Fail to add a record.\n")
return
if not categories.is_category_valid(cat):
print("The specified category is not in the category list.")
print('You can check the category list by command "view categories".')
print("Fail to add a record.\n")
return
for entry in entries:
cat, desc, val = entry.split()
val = int(val)
self._initial_money += val
self._records.append(Record(cat, desc, val))
print()
# VIEW
def view(self):
print("Here's your expense and income records:")
print(f"{'Category':15s} {'Description':20s} {'Amount':>6s}")
print(("="*15) + " " + ("="*20) + " " + ("="*6))
for r in self._records:print(f"{r.category:15s} {r.description:20s} {r.amount:>6d}")
print("=" * 43)
print(f"Now you have {self._initial_money} dollars.\n")
# DELETE
def delete(self, record_str):
parts = record_str.split()
if len(parts) != 3:
print("The format should be: meal breakfast -50.\n")
return
cat, desc, val = parts
for i in range(len(self._records) - 1, -1, -1):
r = self._records[i]
if r.category == cat and r.description == desc and str(r.amount) == val:
self._initial_money -= r.amount
self._records.pop(i)
print()
return
print(f"There's no record with {record_str}. Fail to delete.\n")
# FIND
def find(self, subcategories):
print(f"Here's your expense and income records under category \"{subcategories[0]}\":")
print(f"{'Category':15s} {'Description':20s} {'Amount':>6s}")
print(("="*15) + " " + ("="*20) + " " + ("="*6))
total = 0
for r in self._records:
if r.category in subcategories:
print(f"{r.category:15s} {r.description:20s} {r.amount:>6d}")
total += r.amount
print("=" * 43)
print(f"The total amount above is {total}.\n")
# SAVE
def save(self):
with open(RECORD_FILE, 'w') as f:
f.write(f"{self._initial_money}\n")
for r in self._records:
f.write(str(r) + "\n")
class Categories:
# INIT
def __init__(self):
self._categories = [
"expense",
[
"food",
[
"meal",
"snack",
"drink"
],
"transport",
[
"bus",
"railway"
]
],
"income",
[
"salary",
"bonus"
]
]
# VIEW
def view(self, categories=None, level=0):
if categories is None:
categories = self._categories
for item in categories:
if isinstance(item, list):
self.view(item, level + 1)
else:
print(" " * level + "- " + item)
# IS VALID
def is_category_valid(self, category, categories=None):
if categories is None:
categories = self._categories
for item in categories:
if isinstance(item, list):
if self.is_category_valid(category, item):
return True
else:
if item == category:
return True
return False
# FIND
def find_subcategories(self, category):
def find_subcategories_gen(category, categories, found=False):
if isinstance(categories, list):
for index, child in enumerate(categories):
yield from find_subcategories_gen(category, child, found)
if (child == category and index + 1 < len(categories) and isinstance(categories[index + 1], list)):
yield from find_subcategories_gen(category,categories[index + 1], True)
else:
if categories == category or found: yield categories
return list(find_subcategories_gen(category, self._categories))
# MAIN
categories = Categories()
records = Records()
while True:
command = input("\nWhat do you want to do (add / view / delete / view categories / find / exit)? ")
if command == 'add':
record_str = input("Add records (cat desc amount), separated by commas:\ncat1 desc1 amt1, cat2 desc2 amt2, ...\n")
records.add(record_str, categories)
elif command == 'view':
records.view()
elif command == 'delete':
delete_str = input("Which record do you want to delete? ")
records.delete(delete_str)
elif command == 'view categories':
categories.view()
elif command == 'find':
cat = input("Which category do you want to find? ")
subcats = categories.find_subcategories(cat)
if not subcats: print(f'No such category "{cat}". Check with "view categories".\n')
else: records.find(subcats)
elif command == 'exit':
records.save()
break
else: sys.stderr.write("Invalid command. Try again.\n")