-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSOLID_3_L.py
52 lines (43 loc) · 2.04 KB
/
SOLID_3_L.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
# [Liskov Substitution Principle (LSP)] Download the python file from this link. In this file, there is an implementation of a
# banking system for account handling. There is a savings account and a checking account class. The checking account inherits
# the savings account as both have the same functionality and the checking account allows overdrafts (allow processing
# transactions even if there is not sufficient balance). Redesign this program to follow the Liskov Substitution Principle
# (LSP) principle which represents that “objects should be replaceable by their subtypes without altering how the program
# works”.
from abc import abstractmethod
class Account:
@abstractmethod
def withdraw(self, amount):
pass
class SavingsAccount(Account):
def __init__(self, balance) -> None:
self.balance = balance
def withdraw(self, amount):
# Savings account does not allow overdrafts
if amount <= self.balance:
self.balance -= amount
print(f"Withdrew ${amount}. Remaining balance: ${self.balance}")
else:
print("Insufficient funds!")
class CheckingAccount(Account):
def __init__(self, balance, overdraft_limit):
self.balance = balance
self.overdraft_limit = overdraft_limit
def withdraw(self, amount):
# Checking account allows overdrafts but with a limit
if amount <= self.balance + self.overdraft_limit:
self.balance -= amount
print(f"Withdrew ${amount}. Remaining balance: ${self.balance}")
else:
print("Exceeds overdraft limit or insufficient funds!")
def perform_bank_actions(account):
account.withdraw(100)
account.withdraw(200)
account.withdraw(500)
if __name__ == "__main__":
# Creating instances of SavingsAccount and CheckingAccount
savings_account = SavingsAccount(500)
checking_account = CheckingAccount(1000, overdraft_limit=200)
# Performing actions on both accounts
perform_bank_actions(savings_account)
perform_bank_actions(checking_account)