-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpense_tracker.py
More file actions
125 lines (100 loc) · 4.24 KB
/
Copy pathexpense_tracker.py
File metadata and controls
125 lines (100 loc) · 4.24 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
from expense import Expense
import calendar
import datetime
import sys
import os
def main():
expense_file_path = 'expenses.csv'
budget_file_path = 'budget.txt' # File to store the budget
args = sys.argv
if len(args) == 2 and args[1] == 'log': #in order to log a new expense
#user input
expense = get_user_expense()
print(expense)
#write their responses into a file
save_to_file(expense, expense_file_path)
if len(args) == 2 and args[1] == 'start': #intended to be run when a new budget it set
budget = start(budget_file_path)
if len(args) == 2 and args[1] == 'summarize': #run to get the summaries of you spending, to be run if you have set a budget
#read file and summarize it
budget = load_budget(budget_file_path)
if budget is None:
print("No budget set. Please run the program with 'start' to set a budget.")
else:
summarize_expenses(expense_file_path, budget)
def start(budget_file_path):
# Get budget from the user
budget = int(input("What is your budget: "))
# Save the budget to the budget_file_path file
with open(budget_file_path, 'w') as f:
f.write(str(budget))
print(f"Budget of ${budget} recorded.")
return budget
def load_budget(budget_file_path):
# Check if the budget_file_path file exists
if not os.path.exists(budget_file_path):
return None
# Read the budget from the file
with open(budget_file_path, 'r') as f:
budget = int(f.read().strip())
return budget
def get_user_expense():
print(f"🎯 get user expenses")
expense_name = input("Enter the Expense name: ")
expense_amount = float(input("Enter the Expense Amount: "))
expense_category = [
"🍟 Food",
"🔟 Tithe",
"🚗 Transportation",
"📚 Schooling",
"💩 Unnessesary"
]
while True:
print("Select a Category: ")
for i, category_name in enumerate(expense_category): #lists out the categories numerically
print( f" {i + 1}. {category_name}") #adds 1 because of zero-based indexing
value_range = f"[1 - {len(expense_category)}]"
selected_index = int(input(f" Select a category number {value_range} : ")) - 1 # minus 1 to the users's chosen number to match indexes
if selected_index in range(len(expense_category)):
selected_category = expense_category[selected_index]
new_expense = Expense(name = expense_name, category = selected_category, amount = expense_amount)
return new_expense
else:
print(" Invalid Category, please try again")
def save_to_file(expense : Expense, expense_file_path):
print(f"🎯 Expense Saved 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(f"🎯 summarize spending")
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
for key, amount in amount_by_category.items():
print(f" {key}: ${amount:.2f}")
total_spent = sum([x.amount for x 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(f"👉 Budget Per Day: ${daily_budget:.2f}")
if __name__ == "__main__":
main()