class-designinterface-design

How do I set properties in a constructor that has only a self argument. I also do not know how to define methods without implementation,Inside a class


This is the Question: It blew me away; my attempt is in my next post.

Create a class called bank account that has the methods withdraw and deposit with no implementation.

Create a class called savings account that inherits from bank account. SavingsAccount should have a constructor that only takes in a self argument. This constructor sets a property called balance to 500. (This should be the minimum balance at any given time).

In the savings account class, implement the deposit method that takes in cash deposit amounts, updates the balance accordingly and then returns the balance. For a negative deposit amounts, return invalid deposit amount. In the savings account class, implement the withdraw method that takes in the cash withdrawal amount, deducts this amount from the current balance and returns the balance. This method should never allow the balance to get below 500. (Check for this condition and output Cannot withdraw beyond the minimum account balance if it happens). Also, output Cannot withdraw beyond the current account balance if withdrawal amount is greater than current balance. For a negative withdrawal amount, return Invalid withdraw amount.

Create a class called current account that inherits from bank account. CurrentAccount should have a constructor that only takes in the self argument and sets a property called balance to 0.

In the current account class, implement a deposit method that takes in cash deposit amounts, updates the balance accordingly and then returns the balance. For a negative deposit amount, return invalid deposit amount. In the current account class, implement a withdraw method that takes in the cash withdrawal amount, deducts this amount from the current balance and returns the balance. For a negative withdrawal amount, return invalid withdraw amount. Withdrawing more than the current balance should fail with message cannot withdraw beyond the current account balance.

Here is my attempt:

class BankAccount:
        def __init__ 

(self,name,number,balance):

            self.name=name
            self.number=number
            self.balance=balance

class SavingsAccount(BankAccount):

      def __init__(self, balance=500):
      assert(self.balance>500), "minimum balance is 500"

    def deposit (self, amount):

        if amount<0:

            return "Invalid deposit amount"

        self.balance+=amount

        return self.balance
    def withdraw(self,amount):


        Assert
        (self.balance>amount),
        "cannot withdraw" 

        if amount<0:

            return "Invalid amount"
        self.balance-=amount

        return self.balance

class currentaccount(BankAccount):

    def __init__(self,balance=0):

        def deposit (self, amount):

            if amount<0:

                return ("Invalid deposit amount")

            self.balance+=amount

            return self.balance

    def withdrawal (self, amount):

        assert (self.balance > amount),"cannot withdraw beyond the current account balance"

        if amount<0:

            return "Invalid amount"
        self.balance-=amount

        return self.balance

Solution

  • The code below works!

    class BankAccount(object):
    
        def __init__(self):
            pass
    
        def withdraw():
            pass
        def deposit():
            pass
    
    class SavingsAccount(BankAccount):
    
        def __init__(self):
            self.balance = 500
        
        def deposit(self, amount):
            if (amount < 0):
                return "Invalid deposit amount"
            else:
                self.balance += amount
                return self.balance
        
        def withdraw(self, amount):
            if ((self.balance - amount) > 0) and ((self.balance - amount) < 500):
                return "Cannot withdraw beyond the minimum account balance"
            elif (self.balance - amount) < 0:
                return "Cannot withdraw beyond the current account balance"
            elif amount < 0:
                return "Invalid withdraw amount"
            else:
                self.balance -= amount
                return self.balance 
    
    class CurrentAccount(BankAccount):
    
        def __init__(self):
            self.balance = 0
        
    
        def deposit(self, amount):
            if amount < 0:
                return "Invalid deposit amount"
            else:
                self.balance += amount
                return self.balance
        
           
        def withdraw(self, amount):
            if amount < 0:
                return "Invalid withdraw amount"
            elif self.balance < amount:
                return "Cannot withdraw beyond the current account balance"
            else:
                self.balance -= amount
                return self.balance