-2

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

001
  • 13,291
  • 5
  • 35
  • 66
  • 1
    One tip: if a method is exactly the same in both subclasses (currently true for all of them), they belong in the base class. Currently there is zero difference between the two subclasses, no need to have them at all. – RemcoGerlich Sep 17 '19 at 13:17
  • You *don't* switch classes; what would that model? You might have a function or method that takes an account of one type and returns a new account based on the argument, though. – chepner Sep 17 '19 at 13:19
  • You don't have a loop in `main` nor do you persist the data to disk. Where would the "switch" occur? – 001 Sep 17 '19 at 13:23

1 Answers1

0

I would recommend adding methods such as toSavingAccount() on the CheckingAccount class, that returns an instance of SavingAccount.

Alternatively you can also use @classmethods, which are kind of like additional constructors. You can read a bit about them here

This allows for calls such as

my_savings_account = SavingAccount.fromCheckingAccount(my_checking_account)

or

my_savings_account = my_checking_account.toSavingAccount()

Whatever you name the methods is up to you, but you get the idea.

J.Horr
  • 117
  • 1
  • 13