Create a Savings Account class- Using Inheritance

Create a Savings Account class that behaves just like a Bank Account, but also has an interest rate and a method that increases the balance by the appropriate amount of interest (Hint: use Inheritance).

 

class BankAccount:

    def __init__(self, balance=0):

        self.balance = balance

 

    def deposit(self, amount):

        self.balance += amount

 

    def withdraw(self, amount):

        if self.balance >= amount:

            self.balance -= amount

        else:

            print("Insufficient balance!")

 

    def get_balance(self):

        return self.balance

 

class SavingsAccount(BankAccount):

    def __init__(self, balance=0, interest_rate=0.01):

        super().__init__(balance)

        self.interest_rate = interest_rate

 

    def add_interest(self):

        interest_amount = self.balance * self.interest_rate

        self.balance += interest_amount

 

# Example usage:

savings_account = SavingsAccount(balance=1000, interest_rate=0.05)

print("Initial balance:", savings_account.get_balance())

 

savings_account.add_interest()

print("Balance after adding interest:", savings_account.get_balance())

 

savings_account.deposit(500)

print("Balance after deposit:", savings_account.get_balance())

 

savings_account.withdraw(200)

print("Balance after withdrawal:", savings_account.get_balance())

 

 

 

OUTPUT

 

Initial balance: 1000

Balance after adding interest: 1050.0

Balance after deposit: 1550.0

Balance after withdrawal: 1350.0