-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbl.py
99 lines (86 loc) · 3.95 KB
/
pbl.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
89
90
91
92
93
94
95
96
97
98
99
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
import os
class FinanceManager:
def __init__(self):
# File to store financial data
self.file_name = "finance_data.csv"
self.columns = ["Date", "Type", "Category", "Amount"]
# Create file if not exists
if not os.path.exists(self.file_name):
pd.DataFrame(columns=self.columns).to_csv(self.file_name, index=False)
def add_transaction(self, trans_type, category, amount):
# Append a transaction to the CSV file
new_data = pd.DataFrame([{
"Date": datetime.now().strftime("%Y-%m-%d"),
"Type": trans_type,
"Category": category,
"Amount": amount
}])
new_data.to_csv(self.file_name, mode="a", header=False, index=False)
print(f"Transaction recorded: {trans_type} - {category} - ₹{amount}")
def get_monthly_report(self, month=None, year=None):
# Load data
data = pd.read_csv(self.file_name)
data["Date"] = pd.to_datetime(data["Date"])
# Filter by month and year
if month is None:
month = datetime.now().month
if year is None:
year = datetime.now().year
monthly_data = data[(data["Date"].dt.month == month) & (data["Date"].dt.year == year)]
income = monthly_data[monthly_data["Type"] == "Income"]["Amount"].sum()
expenses = monthly_data[monthly_data["Type"] == "Expense"]["Amount"].sum()
savings = income - expenses
print(f"\nMonthly Report ({month}/{year})")
print(f"Total Income: ₹{income}")
print(f"Total Expenses: ₹{expenses}")
print(f"Total Savings: ₹{savings}")
return monthly_data, income, expenses, savings
def visualize_monthly_data(self, month=None, year=None):
# Generate visualization for a given month and year
monthly_data, _, _, _ = self.get_monthly_report(month, year)
# Expense breakdown
expense_data = monthly_data[monthly_data["Type"] == "Expense"]
if not expense_data.empty:
expense_breakdown = expense_data.groupby("Category")["Amount"].sum()
expense_breakdown.plot(kind="pie", autopct="%1.1f%%", startangle=90, title="Expenses Breakdown")
plt.ylabel("")
plt.show()
else:
print("No expenses to display.")
# Main program
def main():
manager = FinanceManager()
while True:
print("\n--- Finance Management Tool ---")
print("1. Add Income")
print("2. Add Expense")
print("3. View Monthly Report")
print("4. Visualize Monthly Data")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == "1":
category = input("Enter income category (e.g., Salary, Freelance): ")
amount = float(input("Enter amount: ₹"))
manager.add_transaction("Income", category, amount)
elif choice == "2":
category = input("Enter expense category (e.g., Food, Rent): ")
amount = float(input("Enter amount: ₹"))
manager.add_transaction("Expense", category, amount)
elif choice == "3":
month = int(input("Enter month (1-12, leave empty for current month): ") or datetime.now().month)
year = int(input("Enter year (leave empty for current year): ") or datetime.now().year)
manager.get_monthly_report(month, year)
elif choice == "4":
month = int(input("Enter month (1-12, leave empty for current month): ") or datetime.now().month)
year = int(input("Enter year (leave empty for current year): ") or datetime.now().year)
manager.visualize_monthly_data(month, year)
elif choice == "5":
print("Exiting. Goodbye!")
break
else:
print("Invalid choice. Try again.")
if __name__ == "__main__":
main()