Python Practice › OOP Programs › Program 32
CheckingAccount with limit and fee
Create CheckingAccount (inherits BankAccount) with transaction_limit and transaction_fee; check a withdrawal against the limit and deduct the fee.
Output
Withdrew 2000, fee 10, balance 7990 8000 exceeds the limit of 5000
Approach
- Reject amounts over the limit; otherwise subtract amount plus fee.
Solution
class BankAccount: def __init__(self, account_number, balance=0): self.account_number, self.balance = account_number, balance class CheckingAccount(BankAccount): def __init__(self, account_number, balance, transaction_limit, transaction_fee): super().__init__(account_number, balance) self.transaction_limit = transaction_limit self.transaction_fee = transaction_fee def withdraw(self, amount): if amount > self.transaction_limit: return f"{amount} exceeds the limit of {self.transaction_limit}" self.balance -= amount + self.transaction_fee return f"Withdrew {amount}, fee {self.transaction_fee}, balance {self.balance}" acc = CheckingAccount("C-9", 10000, 5000, 10)print(acc.withdraw(2000))print(acc.withdraw(8000))Output
Withdrew 2000, fee 10, balance 7990 8000 exceeds the limit of 5000
Learn the concept
New to this topic? Read the lesson, then come back.