-
Notifications
You must be signed in to change notification settings - Fork 0
/
Object_Oriented_Programming.py
74 lines (59 loc) · 2.54 KB
/
Object_Oriented_Programming.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
# 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]