-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.py
More file actions
53 lines (45 loc) · 1.55 KB
/
Copy pathtracker.py
File metadata and controls
53 lines (45 loc) · 1.55 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
import json
from datetime import datetime
class ExpenseTracker:
def __init__(self):
self.expenses = []
def add_expense(self, amount, category):
expense = {
"amount" : amount,
"category" : category,
"date" : datetime.now().strftime("%Y-%m-%d")
}
self.expenses.append(expense)
def total_expense(self):
total = 0
for expense in self.expenses:
total += expense["amount"]
return total
def filter_by_category(self, category):
result = []
for expense in self.expenses:
if expense["category"].lower() == category.lower():
result.append(expense)
return result
def save_to_file(self):
with open("expenses.json", "w") as file:
json.dump(self.expenses, file, indent=4, sort_keys=True)
def load_from_file(self):
try:
with open("expenses.json", "r") as file:
self.expenses = json.load(file)
except FileNotFoundError:
self.expenses = []
def show_all(self):
if not self.expenses:
print("No expenses found.")
return
for exp in self.expenses:
print(f"{exp['date']} | {exp['category']} | ₹{exp['amount']:.2f}")
def category_summary(self):
summary = {}
for exp in self.expenses:
cat = exp["category"]
summary[cat] = summary.get(cat, 0) + exp["amount"]
for cat, amt in summary.items():
print(f"{cat}: ₹{amt:.2f}")