-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
133 lines (117 loc) · 4.97 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import datetime
import os
class FinancialWallet:
def __init__(self, data_file="wallet_data.json"):
self.data_file = data_file
self.records = []
self.load_records()
def load_records(self):
if not os.path.exists(self.data_file):
return
with open(self.data_file, 'r') as file:
self.records = json.load(file)
def save_records(self):
for idx, record in enumerate(self.records):
record["Index"] = idx # Add index field
with open(self.data_file, 'w') as file:
json.dump(self.records, file, indent=4)
def add_record(self, date, category, amount, description):
index = len(self.records) # Index is the next available index
self.records.append({
"Index": index, # Add index field
"Date": date,
"Category": category,
"Amount": amount,
"Description": description
})
self.save_records()
def edit_record(self, index, date, category, amount, description):
# Check if the index exists
record_exists = any(record["Index"] == index for record in self.records)
if not record_exists:
print("Invalid record index.")
return
# Prompt for other fields only if the index exists
new_record = {
"Index": index,
"Date": date,
"Category": category,
"Amount": amount,
"Description": description
}
# Update the record
for record in self.records:
if record["Index"] == index:
record.update(new_record)
break
self.save_records()
def search_record(self, category=None, date=None, amount=None):
results = []
for record in self.records:
if date:
record_date = datetime.datetime.strptime(record["Date"], "%Y-%m-%d").date()
if (category is None or record["Category"].lower() == category.lower()) and \
(record_date == date):
results.append(record)
else:
if (category is None or record["Category"].lower() == category.lower()) and \
(amount is None or float(record["Amount"]) == float(amount)):
results.append(record)
return results
def show_balance(self):
income = sum(float(record["Amount"]) for record in self.records if record["Category"].lower() == "income")
expenses = sum(float(record["Amount"]) for record in self.records if record["Category"].lower() == "expense")
balance = income - expenses
print(f"Current Balance: {balance}")
print(f"Income: {income}")
print(f"Expenses: {expenses}")
# Example usage
wallet = FinancialWallet()
while True:
print("\nPersonal Financial Wallet")
print("1. Show Balance")
print("2. Add Record")
print("3. Edit Record")
print("4. Search Record")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == "1":
wallet.show_balance()
elif choice == "2":
date = input("Enter Date (YYYY-MM-DD): ")
category = input("Enter Category (Income/Expense): ")
amount = input("Enter Amount: ")
description = input("Enter Description: ")
wallet.add_record(date, category, amount, description)
elif choice == "3":
index = int(input("Enter Record Index to Edit: "))
# Check if the index exists
if not any(record["Index"] == index for record in wallet.records):
print("Invalid record index.")
continue # Continue to the next iteration of the loop
# Prompt for other fields only if the index exists
date = input("Enter Date (YYYY-MM-DD): ")
category = input("Enter Category (Income/Expense): ")
amount = input("Enter Amount: ")
description = input("Enter Description: ")
wallet.edit_record(index, date, category, amount, description)
elif choice == "4":
category = input("Enter Category to Search (Leave blank for any): ")
date_str = input("Enter Date to Search (YYYY-MM-DD) (Leave blank for any): ")
date = datetime.datetime.strptime(date_str, "%Y-%m-%d").date() if date_str else None
amount = input("Enter Amount to Search (Leave blank for any): ")
results = wallet.search_record(category, date, amount)
if results:
print("Search Results:")
for idx, result in enumerate(results):
print(f"Record {idx+1}:")
for key, value in result.items():
print(f"{key}: {value}")
print()
else:
print("No records found matching the criteria.")
elif choice == "5":
break
else:
print("Invalid choice. Please try again.")