|
| 1 | +import pandas as pd |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +def track_expenses(): |
| 6 | + date = input("Enter the date (YYYY-MM-DD): ") |
| 7 | + description = input("Enter the expense description: ") |
| 8 | + amount = float(input("Enter the expense amount: ")) |
| 9 | + category = input("Enter the expense category: ") |
| 10 | + |
| 11 | + try: |
| 12 | + df = pd.read_csv("expenses.csv") |
| 13 | + except FileNotFoundError: |
| 14 | + df = pd.DataFrame(columns=["Date", "Description", "Amount", "Category"]) |
| 15 | + |
| 16 | + df = df.append({"Date": date, "Description": description, "Amount": amount, "Category": category}, ignore_index=True) |
| 17 | + df.to_csv("expenses.csv", index=False) |
| 18 | + |
| 19 | + print("Expense added successfully!") |
| 20 | + |
| 21 | +def view_expenses(): |
| 22 | + try: |
| 23 | + df = pd.read_csv("expenses.csv") |
| 24 | + print(df) |
| 25 | + except FileNotFoundError: |
| 26 | + print("No expenses found.") |
| 27 | + |
| 28 | +def generate_spending_report(): |
| 29 | + try: |
| 30 | + df = pd.read_csv("expenses.csv") |
| 31 | + total_spending = df["Amount"].sum() |
| 32 | + |
| 33 | + plt.figure(figsize=(8, 6)) |
| 34 | + plt.pie(df["Amount"], labels=df["Description"], autopct="%1.1f%%") |
| 35 | + plt.title("Spending Report") |
| 36 | + plt.show() |
| 37 | + |
| 38 | + print("Total Spending: $", total_spending) |
| 39 | + except FileNotFoundError: |
| 40 | + print("No expenses found.") |
| 41 | + |
| 42 | +def set_budget(): |
| 43 | + budget = float(input("Enter your budget amount: ")) |
| 44 | + with open("budget.txt", "w") as file: |
| 45 | + file.write(str(budget)) |
| 46 | + print("Budget set successfully!") |
| 47 | + |
| 48 | +def reset_monthly_budget(): |
| 49 | + now = datetime.now() |
| 50 | + first_day_of_month = now.replace(day=1).strftime("%Y-%m-%d") |
| 51 | + df = pd.DataFrame(columns=["Date", "Description", "Amount", "Category"]) |
| 52 | + df.to_csv("expenses.csv", index=False) |
| 53 | + |
| 54 | + print("Monthly budget and expenses reset successfully.") |
| 55 | + |
| 56 | +def check_budget(): |
| 57 | + try: |
| 58 | + with open("budget.txt", "r") as file: |
| 59 | + budget = float(file.read()) |
| 60 | + df = pd.read_csv("expenses.csv") |
| 61 | + total_spending = df["Amount"].sum() |
| 62 | + remaining_budget = budget - total_spending |
| 63 | + print("Remaining Budget: $", remaining_budget) |
| 64 | + except FileNotFoundError: |
| 65 | + print("Budget not set. Please set a budget first.") |
| 66 | + |
| 67 | +def visualize_expense_distribution(): |
| 68 | + try: |
| 69 | + df = pd.read_csv("expenses.csv") |
| 70 | + category_group = df.groupby("Category")["Amount"].sum() |
| 71 | + |
| 72 | + plt.figure(figsize=(10, 6)) |
| 73 | + plt.bar(category_group.index, category_group.values) |
| 74 | + plt.title("Expense Distribution by Category") |
| 75 | + plt.xlabel("Expense Category") |
| 76 | + plt.ylabel("Total Amount") |
| 77 | + plt.xticks(rotation=45) |
| 78 | + plt.show() |
| 79 | + except FileNotFoundError: |
| 80 | + print("No expenses found.") |
| 81 | + |
| 82 | +def main(): |
| 83 | + while True: |
| 84 | + print("\nPersonal Finance Manager Menu:") |
| 85 | + print("1. Track Expenses") |
| 86 | + print("2. View Expenses") |
| 87 | + print("3. Generate Spending Report") |
| 88 | + print("4. Set Budget") |
| 89 | + print("5. Reset Monthly Budget and Expenses") |
| 90 | + print("6. Check Remaining Budget") |
| 91 | + print("7. Visualize Expense Distribution") |
| 92 | + print("8. Exit") |
| 93 | + |
| 94 | + choice = input("Enter your choice (1-8): ") |
| 95 | + |
| 96 | + if choice == "1": |
| 97 | + track_expenses() |
| 98 | + elif choice == "2": |
| 99 | + view_expenses() |
| 100 | + elif choice == "3": |
| 101 | + generate_spending_report() |
| 102 | + elif choice == "4": |
| 103 | + set_budget() |
| 104 | + elif choice == "5": |
| 105 | + reset_monthly_budget() |
| 106 | + elif choice == "6": |
| 107 | + check_budget() |
| 108 | + elif choice == "7": |
| 109 | + visualize_expense_distribution() |
| 110 | + elif choice == "8": |
| 111 | + print("Exiting the Personal Finance Manager.") |
| 112 | + break |
| 113 | + else: |
| 114 | + print("Invalid choice. Please try again.") |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments