Python Practice › OOP Programs › Program 32

CheckingAccount with limit and fee

MediumOOP Programs · 32 of 48

Create CheckingAccount (inherits BankAccount) with transaction_limit and transaction_fee; check a withdrawal against the limit and deduct the fee.

Advertisement
Output
Withdrew 2000, fee 10, balance 7990
8000 exceeds the limit of 5000

Approach

  1. Reject amounts over the limit; otherwise subtract amount plus fee.

Solution

Python
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.

More oop programs

← All 48 OOP Programs

Leave a Comment