-
Notifications
You must be signed in to change notification settings - Fork 0
/
expensetracker.py
82 lines (71 loc) · 2.49 KB
/
expensetracker.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
import json
import os
# Function to load existing data from file
def load_data(file_name):
if os.path.exists(file_name):
with open(file_name, 'r') as file:
return json.load(file)
else:
return {'income': 0, 'expenses': []}
# Function to save data to file
def save_data(data, file_name):
with open(file_name, 'w') as file:
json.dump(data, file)
# Function to add income
def add_income(data):
income = float(input("Enter income amount: "))
data['income'] += income
print(f"Income of ${income} added successfully.")
# Function to add expense
def add_expense(data):
category = input("Enter expense category: ")
amount = float(input("Enter expense amount: "))
data['expenses'].append({'category': category, 'amount': amount})
print(f"Expense of ${amount} added successfully.")
# Function to calculate remaining budget
def calculate_budget(data):
total_expenses = sum(expense['amount'] for expense in data['expenses'])
remaining_budget = data['income'] - total_expenses
return remaining_budget
# Function to analyze expenses
def analyze_expenses(data):
categories = {}
for expense in data['expenses']:
category = expense['category']
amount = expense['amount']
if category in categories:
categories[category] += amount
else:
categories[category] = amount
print("Expense Analysis:")
for category, amount in categories.items():
print(f"{category}: ${amount}")
# Main function
def main():
file_name = 'budget_data.json'
data = load_data(file_name)
while True:
print("\n===== Budget Tracker =====")
print("1. Add Income")
print("2. Add Expense")
print("3. Calculate Remaining Budget")
print("4. Analyze Expenses")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == '1':
add_income(data)
elif choice == '2':
add_expense(data)
elif choice == '3':
remaining_budget = calculate_budget(data)
print(f"Remaining Budget: ${remaining_budget}")
elif choice == '4':
analyze_expenses(data)
elif choice == '5':
save_data(data, file_name)
print("Budget data saved. Exiting...")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()