I'm writing code about "bank" for my homework I need to create abstract class Account
and two sub-classes CheckingAccount
and SavingAccount
. I made it to add money, show balance etc. But I also need to switch between CheckingAccount
and SavingAccount
.
from abc import ABC, abstractmethod
class Account(ABC):
@abstractmethod
def add_money(self):
pass
@abstractmethod
def balance(self):
pass
@abstractmethod
def take_money(self):
pass
class CheckingAccount(Account):
def __init__(self, money):
self.__money = money
def add_money(self, y):
self.__money += y
def balance(self):
return self.__money
def take_money(self, y):
self.__money -= y
class SavingsAccount(Account):
def __init__(self, money):
self.__money = money
def add_money(self, y):
self.__money += y
def balance(self):
return self.__money
def take_money(self, y):
self.__money -= y
if __name__ == '__main__':
z = 0
j = 0
print("Which account you want to choose?")
j = int(input("1- Checking\n2- Savings"))
if j == 1:
cc = CheckingAccount(0)
while z != 4:
print("1- Balance\n2- Add money\n3- Take money\n4- Exit\n5- Switch Account")
z = int(input("Choose operation: "))
if z == 1:
print(cc.balance())
if z == 2:
y = int(input("Money: "))
cc.add_money(y)
if z == 3:
y = int(input("Take money: "))
cc.take_money(y)
if j == 2:
cc = SavingsAccount(0)
while z != 4:
print("1- Balance\n2- Add money\n3- Take money\n4- Exit\n5- Switch Account")
z = int(input("Choose operation: "))
if z == 1:
print(cc.balance())
if z == 2:
y = int(input("Money: "))
cc.add_money(y)
if z == 3:
y = int(input("Take money: "))
cc.take_money(y)
I expect to be able switch between account without memory loss, I mean if I add 50 to Checking
account and switch to SavingsAccount
and back I still can see 50 in balance.
Please give me some ideas