-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
104 lines (81 loc) · 3.24 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
from datetime import datetime
# Expense and ExpenseDatabase
# Expense Class -> Represents an individual financial expense
from uuid import uuid4
from datetime import datetime
class Expense:
def __init__(self, title, amount) -> None:
# Initialize expense attributes
self.id = str(uuid4())
self.title = str(title)
self.amount = float(amount)
self.created_at = datetime.utcnow()
self.updated_at = self.created_at
def update_expense(self, title=None, amount=None):
# This method updates expense details
if title is not None:
print(f"Updating title to: {title}")
self.title = title
if amount is not None:
print(f"Updating amount to: {amount}")
self.amount = amount
if title is None and amount is None:
print("No updates were made.")
self.updated_at = datetime.utcnow()
def to_dict(self):
# This method returns a dictionary representation of the expense
return {
"id": self.id,
"title": self.title,
"amount": self.amount,
"created_at": self.created_at.isoformat(),
"updated_at": self.updated_at.isoformat()
}
# ExpenseDataBase Class -> Manages a collection of Expense objects.
class ExpenseDataBase:
def __init__(self):
# Initialize an empty list to store expenses
self.expenses = []
def add_expense(self, expense):
# This method adds an expense
self.expenses.append(expense)
print(f"Expense with ID {expense.id} added successfully")
return expense
def remove_expense(self, expense_id):
# This method removes an expense
self.expenses = [expense for expense in self.expenses if expense.id != expense_id]
print(f"Expense with ID {expense_id} has been removed")
def get_expense_by_id(self, expense_id):
# This method retrieves an expense by ID
expense = [expense for expense in self.expenses if expense.id == expense_id]
if len(expense) == 0:
return None
return expense[0]
def get_expense_by_title(self, expense_title):
# This method retrieves expenses by title
similar_expenses = [expense for expense in self.expenses if expense.title == expense_title]
return similar_expenses.copy() # This will return a copy to avoid modifying the state
def to_dict(self):
# This method returns a list of dictionaries representing expenses
return [expense.to_dict() for expense in self.expenses]
def main():
# Create an instance of ExpenseDataBase
expense_db = ExpenseDataBase()
# Create some expenses
expense1 = Expense("Beans", 100.00)
expense2 = Expense("Bread", 50.00)
# Add expenses to the database
expense_db.add_expense(expense1)
expense_db.add_expense(expense2)
# Display the current expenses
print("Current Expenses:")
for expense in expense_db.to_dict():
print(expense)
# Remove an expense by ID
expense_db.remove_expense(expense1.id)
# Display the updated expenses
print("\nUpdated Expenses:")
for expense in expense_db.to_dict():
print(expense)
if __name__ == "__main__":
main()