-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathabstractmethod_github.py
More file actions
53 lines (42 loc) · 1.6 KB
/
abstractmethod_github.py
File metadata and controls
53 lines (42 loc) · 1.6 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
from abc import ABC, abstractmethod
# ---------------- Abstract Base Class ---------------- #
class BankAccount(ABC):
def __init__(self, owner, balance):
self.owner = owner
self.balance = balance
@abstractmethod
def show_balance(self):
"""Display the current balance."""
pass
@abstractmethod
def withdraw(self, amount):
"""Withdraw money from the account."""
pass
# ---------------- Concrete Classes ---------------- #
class CheckingAccount(BankAccount):
def show_balance(self):
print(f"[Checking] {self.owner}'s balance: ${self.balance}")
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print(f"Withdrew ${amount} from Checking. New balance: ${self.balance}")
else:
print("❌ Not enough funds in Checking.")
class SavingsAccount(BankAccount):
def show_balance(self):
print(f"[Savings] {self.owner}'s balance: ${self.balance}")
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print(f"Withdrew ${amount} from Savings. New balance: ${self.balance}")
else:
print("❌ Not enough funds in Savings.")
def main():
john_checking = CheckingAccount("John", 1000)
john_savings = SavingsAccount("John", 5000)
john_checking.show_balance()
john_checking.withdraw(200)
john_savings.show_balance()
john_savings.withdraw(6000) # Should show not enough funds
if __name__ == "__main__":
main()