-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpenseTracker
More file actions
86 lines (74 loc) · 3.29 KB
/
ExpenseTracker
File metadata and controls
86 lines (74 loc) · 3.29 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
class Expense:
def __init__(self, date, category, amount, description=""):
self.date = date
self.category = category
self.amount = amount
self.description = description
def __str__(self):
return f"Date: {self.date}, Category: {self.category}, Amount: ₹{self.amount}, Description: {self.description}"
class ExpenseTracker:
def __init__(self):
self.expenses = []
def add_expense(self, date, category, amount, description=""):
expense = Expense(date, category, amount, description)
self.expenses.append(expense)
print("✅ Expense added successfully!")
def view_expenses(self):
if not self.expenses:
print("No expenses recorded.")
return
print("\n--- Expense List ---")
for idx, expense in enumerate(self.expenses, start=1):
print(f"{idx}. {expense}")
def get_total_expense(self):
total = sum(expense.amount for expense in self.expenses)
print(f"\n💰 Total Expense: ₹{total}")
def view_expenses_by_category(self, category):
filtered_expenses = [expense for expense in self.expenses if expense.category.lower() == category.lower()]
if not filtered_expenses:
print(f"No expenses found in the '{category}' category.")
return
print(f"\n--- Expenses in '{category}' Category ---")
for expense in filtered_expenses:
print(expense)
def remove_expense(self, index):
if 0 <= index < len(self.expenses):
removed_expense = self.expenses.pop(index)
print(f"🗑️ Removed: {removed_expense}")
else:
print("Invalid index. Please try again.")
def menu(self):
while True:
print("\n--- Expense Tracker Menu ---")
print("1. Add Expense")
print("2. View All Expenses")
print("3. View Expenses by Category")
print("4. Get Total Expense")
print("5. Remove an Expense")
print("6. Exit")
choice = input("Choose an option (1-6): ")
if choice == "1":
date = input("Enter the date (YYYY-MM-DD): ")
category = input("Enter the category (e.g., Food, Travel, Shopping): ")
amount = float(input("Enter the amount: ₹"))
description = input("Enter a description (optional): ")
self.add_expense(date, category, amount, description)
elif choice == "2":
self.view_expenses()
elif choice == "3":
category = input("Enter the category to filter: ")
self.view_expenses_by_category(category)
elif choice == "4":
self.get_total_expense()
elif choice == "5":
self.view_expenses()
index = int(input("Enter the index of the expense to remove: ")) - 1
self.remove_expense(index)
elif choice == "6":
print("Exiting Expense Tracker. Goodbye! 👋")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
tracker = ExpenseTracker()
tracker.menu()