-
Notifications
You must be signed in to change notification settings - Fork 0
/
oop.py
34 lines (27 loc) · 1.13 KB
/
oop.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
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
return f"${amount} deposited. New balance: ${self.__balance}"
def withdraw(self, amount):
if amount > self.__balance:
return "Insufficient funds."
else:
self.__balance -= amount
return f"${amount} withdrawn. New balance: ${self.__balance}"
def get_balance(self):
return f"Balance for {self.owner}: ${self.__balance}"
class SavingsAccount(BankAccount):
def __init__(self, owner, balance=0, interest_rate=0.01):
super().__init__(owner, balance)
self.interest_rate = interest_rate
def apply_interest(self):
interest = self._BankAccount__balance * self.interest_rate
self.deposit(interest)
return f"Interest applied: ${interest}. New balance: ${self._BankAccount__balance}"
# Create a savings account and apply interest
savings_account = SavingsAccount("Shaliha", 1000)
print(savings_account.apply_interest())
print(savings_account.get_balance())