-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
355 lines (279 loc) · 9.87 KB
/
main.py
File metadata and controls
355 lines (279 loc) · 9.87 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import csv
import time
from src.Expense import Expense
from utils.constants import category_types, app_header_art, printMenu
def validate_expenses(existing_expense=None):
expense = existing_expense or Expense()
#Name
while True:
name = input("Enter the name/description of the expense: ").strip()
if existing_expense and not name: # Allow keeping old value
break
try:
expense.set_name(name)
break
except Exception as e:
print(f"Error: {e}")
#Category
while True:
print(f"Available categories: {category_types}")
category = input("Enter the category of the expense: ").strip()
if existing_expense and not category:
break
try:
expense.set_category(category)
break
except Exception as e:
print(f"Error: {e}")
#Amount
while True:
amount = input("Enter the amount of the expense: ").strip()
if existing_expense and not amount:
break
try:
expense.set_amount(amount)
break
except Exception as e:
print(f"Error: {e}")
#Date
while True:
date = input("Enter the date of the expense: ").strip()
if existing_expense and not date:
break
try:
expense.set_date(date)
break
except Exception as e:
print(f"Error: {e}")
#Notes
while True:
notes = input("Enter notes for the expense: ").strip()
if existing_expense and not notes:
break
try:
expense.set_notes(notes)
break
except Exception as e:
print(f"Error: {e}")
return expense
def insertExpense():
# Validate and save new expense
expense = validate_expenses()
expense.save_expense()
# Confirmation print
print("\nExpense created successfully:")
print(f" Name : {expense.name}")
print(f" Category : {expense.category}")
print(f" Amount : {expense.amount}")
print(f" Date : {expense.date}")
print(f" Notes : {expense.notes}")
print(f" Unique ID : {expense.unique_id}")
print(f" Recorded At: {expense.recorded_at}\n")
def showMenu():
print(app_header_art)
printMenu()
while True:
# Get main menu choice
choice = input("Select an option (1-6): ").strip()
if not choice.isdigit():
print("Invalid choice. Please enter a number.")
continue
choice_int = int(choice)
if choice_int < 1 or choice_int > 6:
print("Invalid choice. Please enter a number between 1 and 6.")
continue
print("\n")
# check menu options
if choice_int == 1:
insertExpense()
elif choice_int == 2:
# Submenu for viewing expenses
while True:
sub_choice = input("Would you like to view all expenses (1) or a specific expense (2)? ").strip()
if not sub_choice.isdigit():
print("Invalid choice. Please enter a number.")
continue
sub_choice_int = int(sub_choice)
if sub_choice_int == 1:
viewExpenses()
break
elif sub_choice_int == 2:
view_unique_expense()
break
else:
print("Invalid choice. Please enter 1 or 2.")
elif choice_int == 3:
spendingAnalysis()
elif choice_int == 4:
editExpense()
elif choice_int == 5:
deleteExpense()
elif choice_int == 6:
print("Thank you for using this program.")
break
def viewExpenses():
with open("outputs/expenses.csv", "r") as f:
reader = csv.reader(f)
print("\n" + "=" * 50)
print(" YOUR EXPENSES")
print("=" * 50)
for row in reader:
if len(row) < 7:
continue
print(f"Name : {row[1]}")
print(f"Category : {row[3]}")
print(f"Amount : ${row[2]}")
print(f"Date : {row[4]}")
print(f"Notes : {row[5]}")
print(f"Unique ID : {row[0]}")
print(f"Recorded At : {row[6]}")
print("-" * 50)
print("\n")
def view_unique_expense():
unique_id = input("Please enter the unique ID of the expense you would like to view: ").strip()
expense_found = False
with open("outputs/expenses.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
if len(row) < 7:
continue
if row[0] == unique_id:
expense = Expense(
name=row[1],
category=row[3],
amount=float(row[2]),
date=row[4],
notes=row[5]
)
expense.unique_id = row[0]
expense.recorded_at = row[6]
print("\n" + "=" * 50)
print(" YOUR EXPENSE")
print("=" * 50)
print(f"Name : {expense.name}")
print(f"Category : {expense.category}")
print(f"Amount : ${expense.amount:.2f}")
print(f"Date : {expense.date}")
print(f"Notes : {expense.notes}")
print(f"Unique ID : {expense.unique_id}")
print(f"Recorded At : {expense.recorded_at}")
print("-" * 50 + "\n")
expense_found = True
break
if not expense_found:
print("No expense found with that unique ID.\n")
def spendingAnalysis():
with open("outputs/expenses.csv", "r") as f:
reader = csv.reader(f)
highestExpense = 0
lowestExpense = None
highestRecorded = ""
lowestRecorded = ""
categories = []
categorySpent = []
totalSpent = 0
for r in reader:
if len(r) < 7:
continue
amount = float(r[2])
totalSpent += amount
category = r[3].strip().lower()
if amount > highestExpense:
highestExpense = amount
highestRecorded = r[6]
if lowestExpense is None or lowestExpense > amount:
lowestExpense = amount
lowestRecorded = r[6]
if category not in categories:
categories.append(category)
categorySpent.append(amount)
else:
i = categories.index(category)
categorySpent[i] += amount
print("=" * 50)
print(" SPENDING ANALYSIS")
print("=" * 50)
for i in range(len(categories)):
percent = int((categorySpent[i] / totalSpent) * 100)
print(categories[i] + ": $" + str(categorySpent[i]) +
" (%" + str(percent) + ")")
print()
print("-" * 50)
print()
print("The highest expense is: $" + str(highestExpense) +
", recorded on " + highestRecorded)
print("The lowest expense is: $" + str(lowestExpense) +
", recorded on " + lowestRecorded + "\n")
def editExpense():
choose_edit = input("Please enter the unique ID of the expense you would like to edit: ")
expenseFound = False
rows = []
with open("outputs/expenses.csv", "r") as f:
print("\n")
reader = csv.reader(f)
for r in reader:
if len(r) < 7:
rows.append(r)
continue
if r[0] == choose_edit:
expenseFound = True
print("\nEnter new values (Leave blank to keep current values):")
expense = Expense(
name=r[1],
category=r[3],
amount=float(r[2]),
date=r[4],
notes=r[5]
)
expense = validate_expenses(existing_expense=expense)
rows.append([
r[0],
expense.name,
expense.amount,
expense.category,
expense.date,
expense.notes,
r[6]
])
else:
rows.append(r)
if not expenseFound:
print("No expense found with that unique ID.")
return
with open("outputs/expenses.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(rows)
print("Expense has been updated successfully.\n")
def deleteExpense():
choose_del = input("Please enter the unique ID of the expense you would like to delete: ").strip()
print("\n")
expenseFound = False
rows = []
with open("outputs/expenses.csv", "r") as f:
reader = csv.reader(f)
for r in reader:
if len(r) < 7:
rows.append(r)
continue
if r[0] == choose_del:
expenseFound = True
print(f"Expense {r[0]} deleted\n")
else:
rows.append(r)
if not expenseFound:
print("No expense found with that unique ID.\n")
return
with open("outputs/expenses.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(rows)
if __name__ == '__main__':
# TODO: ADD MENU FUNCTIONALITY
# 1. Add expense
# a. validate inputs from user one by one and validate them in the Expense class individaully AND CLEAN THE CODE
# 2. View expense
# View All expenses
# View by unique id (find unique id, get expense, fill into expense object and print it out)
# 3. Spending analysis
# 4. Edit/Delete expense
# 5. Exit
showMenu()