-
Notifications
You must be signed in to change notification settings - Fork 113
/
expense_tracker.py
88 lines (75 loc) · 2.63 KB
/
expense_tracker.py
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
import os
import json
from datetime import datetime
# File to store expense data
EXPENSE_FILE = "expenses.json"
expense_data = []
def load_expenses():
global expense_data
if os.path.exists(EXPENSE_FILE):
with open(EXPENSE_FILE, "r") as file:
expense_data = json.load(file)
print("Loaded expense data.")
else:
print("No expense data to load.")
def save_expenses_to_file():
with open(EXPENSE_FILE, "w") as file:
json.dump(expense_data, file, indent=4)
def show_expenses():
if expense_data:
print("Expense History:")
for entry in expense_data:
date = entry['date']
amount = entry['amount']
category = entry['category']
description = entry['description']
print(f"{date}: {amount} | Category: {category} | Description: {description}")
else:
print("No expense entries available!")
def submit_expense():
amount = float(input("Enter the expense amount: "))
category = input("Enter the category of the expense (e.g., food, transport, bills): ")
description = input("Enter a brief description of the expense: ")
entry = {
"date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"amount": amount,
"category": category,
"description": description
}
expense_data.append(entry)
print("Expense logged!")
save_expenses_to_file()
def analyze_expenses():
if expense_data:
category_totals = {}
for entry in expense_data:
category = entry['category']
amount = entry['amount']
category_totals[category] = category_totals.get(category, 0) + amount
print("Expense Analysis:")
for category, total in category_totals.items():
print(f"{category}: ${total:.2f}")
else:
print("No expenses to analyze!")
def expense_tracker_system():
load_expenses()
while True:
print("\nExpense Tracker System")
print("1. Log Expense")
print("2. Show Expense History")
print("3. Analyze Expenses")
print("4. Exit")
choice = input("Please select an option (1-4): ")
if choice == "1":
submit_expense()
elif choice == "2":
show_expenses()
elif choice == "3":
analyze_expenses()
elif choice == "4":
print("Exiting expense tracker system.")
break
else:
print("Invalid choice, please try again.")
if __name__ == "__main__":
expense_tracker_system()