-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpense_tracker.py
More file actions
100 lines (77 loc) · 3.18 KB
/
expense_tracker.py
File metadata and controls
100 lines (77 loc) · 3.18 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
from expense import Expense
import calendar
import datetime
def main():
print("Running expense tracker!")
expense_file_path = "expenses.csv"
budget = 2000
# Get the user to input their expenses.
expense = get_user_expense()
# Write the expenses to a file.
save_expense_to_file(expense, expense_file_path)
# Read the file and summarize the expenses.
summarize_expenses(expense_file_path, budget)
def get_user_expense():
print("Getting user expense.")
expense_name = input("Enter the expense name: ")
expense_amount = float(input("Enter the expense amount: "))
expense_categories = [
"Food",
"Rent",
"Work",
"Fun",
"Miscellaneous"
]
while True:
print("Select a category: ")
for i, category_name in enumerate(expense_categories):
print(f" {i + 1}. {category_name}")
value_range = f"[1 - {len(expense_categories)}]"
selected_index = int(input(f"Enter a category number {value_range}: "))
if selected_index in range(1, len(expense_categories) + 1):
selected_category = expense_categories[selected_index - 1]
new_expense = Expense(
name = expense_name, category = selected_category, amount = expense_amount)
return new_expense
else:
print("Invalid category. Please try again.")
def save_expense_to_file(expense, expense_file_path):
print(f"Saving user expense: {expense} to {expense_file_path}.")
with open(expense_file_path, "a") as f:
f.write(f"{expense.name},{expense.amount},{expense.category}\n")
def summarize_expenses(expense_file_path, budget):
print("Summarizing user expense.")
expenses: list[Expense] = []
with open(expense_file_path, "r") as f:
lines = f.readlines()
for line in lines:
expense_name, expense_amount, expense_category = line.strip().split(",")
line_expense = Expense(
name=expense_name,
amount=float(expense_amount),
category=expense_category
)
expenses.append(line_expense)
amount_by_category = {}
for expense in expenses:
key = expense.category
if key in amount_by_category:
amount_by_category[key] += expense.amount
else:
amount_by_category[key] = expense.amount
print("Expenses By Category:")
for key, amount in amount_by_category.items():
print(f" {key}: ${amount:.2f}")
total_spent = sum([ex.amount for ex in expenses])
print(f"Total spent: ${total_spent:.2f}")
remaining_budget = budget - total_spent
print(f"Budget Remaining: ${remaining_budget:.2f}")
now = datetime.datetime.now()
days_in_month = calendar.monthrange(now.year, now.month)[1]
remaining_days = days_in_month - now.day
daily_budget = remaining_budget / remaining_days
print(green(f"Budget per day: ${daily_budget:.2f}"))
def green(text):
return f"\033[92m{text}\033[0m"
if __name__ == "__main__":
main()