-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_oop_bank_system.py
More file actions
215 lines (170 loc) · 6.96 KB
/
Copy path03_oop_bank_system.py
File metadata and controls
215 lines (170 loc) · 6.96 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""OOP bank system — accounts, transfers, statements, error handling."""
from dataclasses import dataclass, field
from datetime import datetime
from abc import ABC, abstractmethod
import uuid
class InsufficientFundsError(Exception):
"""Raised when a withdrawal violates an account's balance rules."""
@dataclass
class Transaction:
kind: str # 'credit' | 'debit'
amount: float
note: str = ""
when: datetime = field(default_factory=datetime.now)
def __str__(self) -> str:
sign = "+" if self.kind == "credit" else "-"
ts = self.when.strftime("%Y-%m-%d %H:%M")
return f"{ts} {sign}₹{self.amount:10.2f} {self.note}"
class Account(ABC):
def __init__(self, owner: str, initial: float = 0):
if initial < 0:
raise ValueError("Initial balance cannot be negative")
self._id = str(uuid.uuid4())[:8].upper()
self._owner = owner
self._balance = initial
self._history: list[Transaction] = []
@property
def balance(self) -> float:
return self._balance
@property
def account_id(self) -> str:
return self._id
@property
def owner(self) -> str:
return self._owner
def deposit(self, amount: float, note: str = "") -> "Account":
if amount <= 0:
raise ValueError("Deposit amount must be positive")
self._balance += amount
self._history.append(Transaction("credit", amount, note))
return self
@abstractmethod
def withdraw(self, amount: float, note: str = "") -> "Account":
"""Each account type enforces its own withdrawal rules."""
def transfer(self, to: "Account", amount: float) -> None:
if to is self:
raise ValueError("Cannot transfer to the same account")
self.withdraw(amount, note=f"Transfer to {to.account_id}")
to.deposit(amount, note=f"Transfer from {self.account_id}")
def statement(self, last: int = 5) -> None:
print(f"\nAccount {self._id} ({self.__class__.__name__}) — {self._owner}")
print(f"Balance: ₹{self._balance:,.2f}\n")
if not self._history:
print(" No transactions yet.")
return
for t in self._history[-last:]:
print(f" {t}")
def __str__(self) -> str:
return f"{self._id} | {self._owner} | {self.__class__.__name__} | ₹{self._balance:,.2f}"
class SavingsAccount(Account):
MIN_BALANCE = 1000
def withdraw(self, amount: float, note: str = "") -> "Account":
if amount <= 0:
raise ValueError("Withdrawal amount must be positive")
if self._balance - amount < self.MIN_BALANCE:
raise InsufficientFundsError(
f"Must maintain ₹{self.MIN_BALANCE:,.2f} minimum balance"
)
self._balance -= amount
self._history.append(Transaction("debit", amount, note))
return self
class CurrentAccount(Account):
"""No minimum balance, but allows overdraft up to a limit."""
OVERDRAFT_LIMIT = 5000
def withdraw(self, amount: float, note: str = "") -> "Account":
if amount <= 0:
raise ValueError("Withdrawal amount must be positive")
if self._balance - amount < -self.OVERDRAFT_LIMIT:
raise InsufficientFundsError(
f"Exceeds overdraft limit of ₹{self.OVERDRAFT_LIMIT:,.2f}"
)
self._balance -= amount
self._history.append(Transaction("debit", amount, note))
return self
class Bank:
"""Holds and manages all accounts."""
def __init__(self, name: str):
self.name = name
self._accounts: dict[str, Account] = {}
def open_account(self, owner: str, kind: str = "savings", initial: float = 0) -> Account:
kinds = {"savings": SavingsAccount, "current": CurrentAccount}
if kind not in kinds:
raise ValueError(f"Unknown account type: {kind!r} (use 'savings' or 'current')")
account = kinds[kind](owner, initial)
self._accounts[account.account_id] = account
return account
def get_account(self, account_id: str) -> Account:
try:
return self._accounts[account_id.strip().upper()]
except KeyError:
raise ValueError(f"No account with ID {account_id}")
def list_accounts(self) -> None:
if not self._accounts:
print("No accounts yet.")
return
for acc in self._accounts.values():
print(f" {acc}")
def main() -> None:
bank = Bank("PyBank")
menu = """
1. Open account
2. Deposit
3. Withdraw
4. Transfer
5. Statement
6. List accounts
7. Exit
"""
while True:
print(menu)
choice = input("Choose an option: ").strip()
try:
if choice == "1":
owner = input("Owner name: ").strip()
kind = input("Type (savings/current): ").strip().lower()
initial = float(input("Initial deposit: ₹"))
acc = bank.open_account(owner, kind, initial)
print(f"Opened account {acc.account_id} for {owner}")
elif choice == "2":
acc = bank.get_account(input("Account ID: "))
acc.deposit(float(input("Amount: ₹")), note="Manual deposit")
print(f"New balance: ₹{acc.balance:,.2f}")
elif choice == "3":
acc = bank.get_account(input("Account ID: "))
acc.withdraw(float(input("Amount: ₹")), note="Manual withdrawal")
print(f"New balance: ₹{acc.balance:,.2f}")
elif choice == "4":
src = bank.get_account(input("From account ID: "))
dst = bank.get_account(input("To account ID: "))
src.transfer(dst, float(input("Amount: ₹")))
print("Transfer complete.")
elif choice == "5":
bank.get_account(input("Account ID: ")).statement()
elif choice == "6":
bank.list_accounts()
elif choice == "7":
print("Goodbye!")
break
else:
print("Invalid option, try again.")
except ValueError as e:
print(f"Error: {e}")
except InsufficientFundsError as e:
print(f"Transaction declined: {e}")
bank = Bank("Test")
sav = bank.open_account("Veda", "savings", 5000)
cur = bank.open_account("Friend", "current", 0)
sav.deposit(1000)
sav.transfer(cur, 2000)
sav.statement()
cur.statement()
try:
sav.withdraw(10000) # should fail — below min balance
except InsufficientFundsError as e:
print("Caught:", e)
try:
cur.withdraw(6000) # should fail — beyond overdraft
except InsufficientFundsError as e:
print("Caught:", e)
if __name__ == "__main__":
main()