Repository navigation
Expand file tree
/
Copy pathexpense_manager.py
More file actions
353 lines (301 loc) · 12.9 KB
/
Copy pathexpense_manager.py
File metadata and controls
353 lines (301 loc) · 12.9 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
import os
import datetime as dt
from prettytable import PrettyTable, prettytable
from termcolor import colored
from dateutil import parser
import json
TODAY = dt.datetime.today().date()
def clear_screen():
# For Windows
if os.name == 'nt':
_ = os.system('cls')
# For Unix/Linux/MacOS
else:
_ = os.system('clear')
class ExpenseManager:
def __init__(self, user):
"""
Initializes a new ExpenseManager object.
Parameters:
user (str): The username of the current user.
Returns:
None
Method initializes the attributes of the ExpenseManager object.
"""
self.user = user
self.expenses_table = PrettyTable()
self.expenses_table.hrules = prettytable.ALL
self.expenses_table.field_names = ["Category", "Command"]
self.expenses_table.padding_width = 2
self.expenses_table.align["Command"] = 'c'
self.expenses_table.align["Category"] = 'l'
self.expenses_table.add_rows([
['Food (including groceries, dining out, and takeout)', 1],
['Housing (rent or mortgage payments, utilities, maintenance)', 2],
['Transportation (gasoline, public transit, vehicle maintenance)', 3],
['Health and wellness (healthcare expenses, gym memberships, medications)', 4],
['Entertainment (movies, concerts, streaming services)', 5],
['Shopping (clothing, electronics, personal care products)', 6],
['Travel (flights, hotels, vacation activities)', 7],
['Utilities (electricity, water, internet, phone)', 8],
['Education (tuition, books, supplies)', 9],
['Debt payments (credit card bills, loans)', 10],
['Insurance (health, auto, home)', 11],
['Personal care (haircuts, spa treatments, grooming products)', 12],
['Gifts and donations (birthday presents, charity donations)', 13],
['Household supplies (cleaning products, toiletries)', 14],
['Other Expenses', 15],
['Cancel operation', 'cancel']
])
self.expenses = [
'Food',
'Housing',
'Transportation',
'Health and wellness',
'Entertainment',
'Shopping',
'Travel',
'Utilities',
'Education',
'Debt payments',
'Insurance',
'Personal care',
'Gifts and donations',
'Household supplies',
'Other Expenses'
]
@staticmethod
def get_date():
while True:
date_input = input("Enter date you want to add expenses to (e.g., '2024-05-31'): ")
if date_input.lower().strip() == "exit":
break
try:
date_obj = parser.parse(date_input).date()
if date_obj > TODAY:
clear_screen()
print(f"Date {date_obj} is in the future. Please enter past or present date.")
elif (TODAY - date_obj).days > 365:
clear_screen()
print(f"Date {date_obj} was more than a year ago. Please enter a recent date.")
else:
return date_obj.strftime("%Y-%m-%d")
except ValueError:
clear_screen()
print("Invalid date format!".upper())
print("Please enter a valid date in format YYYY-MM-DD.")
print("If you want to cancel operation, enter 'exit'\n")
@staticmethod
def format_date(date):
date_obj = dt.datetime.strptime(date, "%Y-%m-%d")
return date_obj.strftime("%d %B %Y")
@staticmethod
def enter_amount():
while True:
try:
amount = float(input("Enter amount of money you've spent: $").strip().lower())
except ValueError:
print("You should enter a number! (e.g - 8.50)\n")
continue
if amount < 0:
print("Money you've spent should be a positive number!\n")
continue
return amount
@staticmethod
def check_command(comm):
try:
comm = int(comm)
except ValueError:
print("Command is not valid. Please enter a number from 1 to 15 or 'cancel' to cancel operation")
return False
if 1 <= comm <= 15:
return comm
print("There is only 15 commands. Enter a number from 1 to 15 or 'cancel' to cancel operation")
return False
def initialize_file_with_format(self):
"""
Initialize the JSON file with a predefined format.
Returns:
None
Method initializes the JSON file associated with the user with a predefined format.
"""
with open(f'users/{self.user}.json', 'w') as json_file:
data = {
'date': {},
'month': {}
}
json.dump(data, json_file, indent=4)
def check_emptiness(self):
"""
Check if the JSON file is empty or not in the desired format.
Returns:
None
Method checks if the JSON file associated with the user is empty or not in the desired format.
"""
try:
with open(f'users/{self.user}.json', 'r') as json_file:
try:
loaded_data = json.load(json_file)
if 'date' in loaded_data and 'month' in loaded_data \
and isinstance(loaded_data['date'], dict) \
and isinstance(loaded_data['month'], dict):
pass # Data is in the desired format
else:
self.initialize_file_with_format() # Reinitialize the file
except json.JSONDecodeError:
self.initialize_file_with_format() # File is empty or not valid JSON
except FileNotFoundError:
self.initialize_file_with_format() # File doesn't exist, create with format
def logo_table_expenses(self, date):
"""
Display the logo and table of expenses.
Parameters:
date (str): The selected date.
Returns:
None
Method displays the logo of the user and the table of expenses for the selected date.
"""
clear_screen()
print(self.expenses_table)
print(f"SELECTED DATE - {date}")
logo = colored(self.user, attrs={"bold"})
print(f"Username: {logo}\n")
def save_expense(self, expense, amount, date):
"""
Save the expense to the JSON file.
Parameters:
expense (str): The expense category.
amount (float): The amount spent.
date (str): The date of the expense.
Returns:
None
Method saves the expense and amount to the JSON file associated with the user.
"""
# Read existing data from the JSON file
try:
with open(f"users/{self.user}.json", "r") as file:
data = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
data = {}
# Extract month and year from the date
date_obj = dt.datetime.strptime(date, "%Y-%m-%d")
month_year = date_obj.strftime("%B %Y")
# Update data for the specific date
if 'date' not in data:
data['date'] = {}
if date in data['date']:
# If expense already exists for the date, update the amount
if expense in data['date'][date]:
data['date'][date][expense] += amount
else:
# If expense doesn't exist for the date, add it
data['date'][date][expense] = amount
else:
# If there is no existing data for the date, create a new entry
data['date'][date] = {expense: amount}
# Update data for the specific month
if 'month' not in data:
data['month'] = {}
if month_year in data['month']:
if 'expenses' in data['month'][month_year] and expense in data['month'][month_year]['expenses']:
# If the expense exists for the month, update its amount
data['month'][month_year]['expenses'][expense] += amount
else:
# If the expense doesn't exist for the month, create a new entry
data['month'][month_year]['expenses'][expense] = amount
else:
# If month doesn't exist, create a new entry
data['month'][month_year] = {'limit': None, 'expenses': {expense: amount}}
# Write the updated data back to the JSON file
with open(f"users/{self.user}.json", "w") as file:
json.dump(data, file, indent=4)
def add_expenses(self, date=""):
"""
Add expenses for a specific date.
Parameters:
date (str, optional): The date to add expenses for. Defaults to "".
Returns:
None
Method allows the user to add expenses for a specific date.
"""
if date == "":
date = self.get_date()
if date is None:
clear_screen()
print("You have successfully canceled operation!\n")
input("Press to continue...")
return
f_date = self.format_date(date)
continue_adding = True
while continue_adding:
self.logo_table_expenses(f_date)
correct_input = False
while not correct_input:
exp_choice = input("Enter which expense you want to add: ").lower().strip()
if exp_choice == 'cancel':
return
correct_input = self.check_command(exp_choice)
clear_screen()
print(f"You have selected {self.expenses[correct_input - 1]} expense.\n")
amount = self.enter_amount()
clear_screen()
choice = input(f"Do you want to add {self.expenses[correct_input - 1]} "
f"expense with ${amount} of money spent at {f_date}? (y/n) ").lower().strip()
while choice != 'y' and choice != 'n':
print("You should enter only 'y' to save expense or 'n' to cancel it!")
choice = input(f"Do you want to add {self.expenses[correct_input - 1]} "
f"expense with ${amount} of money spent at {f_date}?").lower().strip()
if choice == 'n':
print("Expense wasn't saved to your list!\n")
continue
self.save_expense(self.expenses[correct_input - 1], amount, date)
print()
choice = input("Do you want to add another expense? (y/n) ").strip().lower()
while choice != 'y' and choice != 'n':
print("Invalid input! Enter 'y' if you want to add another expense and 'n' if don't.")
choice = input("Enter your choice: ").strip().lower()
if choice == 'n':
continue_adding = False
continue
else:
print()
def set_limit(self, select_month_func):
"""
Set a spending limit for a specific month.
Parameters:
select_month_func (function): A function to select the month.
Returns:
None
Method allows the user to set a spending limit for a specific month.
"""
# Load data from JSON file
with open(f"users/{self.user}.json", "r") as file:
data = json.load(file)
clear_screen()
# Get the current month
selected_month = select_month_func("set a limit for")
clear_screen()
while True:
clear_screen()
if 'limit' in data['month'].get(selected_month, {}):
print(f"The limit for {selected_month} is ${data['month'][selected_month]['limit']}\n")
limit_input = input(f"Enter the new limit for {selected_month} (type 'cancel' to cancel): ")
if limit_input.lower() == 'cancel':
print("Operation cancelled.")
return
try:
new_limit = float(limit_input)
except ValueError:
clear_screen()
print("Invalid input! Please enter a number.")
input("Press to continue...")
continue # Restart the loop to prompt the user again
# Add or update the limit for the selected month
data['month'].setdefault(selected_month, {})['limit'] = new_limit
# Check if there are expenses for the selected month
if 'expenses' not in data['month'][selected_month]:
data['month'][selected_month]['expenses'] = {}
# Write the updated data back to the JSON file
with open(f"users/{self.user}.json", "w") as file:
json.dump(data, file, indent=4)
break # Exit the loop if input is valid