-3

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
Rohith
  • 226
  • 5
  • 9

3 Answers3

2

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
    
Community
  • 1
  • 1
0

Read the assignment carefully: you set the balance to 500 every time; no need for another constructor argument. There is no name, no account number.

The "no implementation" methods are simple:

def method():
    pass

Does that get you moving? Don't try to make this harder than it really is: start simple, write a few lines at a time, and debug those before you move on.

Prune
  • 76,765
  • 14
  • 60
  • 81
0

To set a constructor with only a self argument, do this

class Example(object):
    def __init__(self):
        pass

For this particular question, read the instructions step by step and implement the methods as the instructions say. The catch here is to have the logic correct. In the savings account class, make sure that first you check for overdrafts, then check that a withdrawal does not result in a balance less than 500. That order is important. I had a similar problem and I solved it after making that change.

Here's the solution:

class BankAccount(object):
    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 amount < 0:
            return "Invalid withdraw amount"
        elif amount > self.balance:
            return "Cannot withdraw beyond the current account balance"
        elif (self.balance - amount) < 500:
            return "Cannot withdraw beyond the minimum account balance"
        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
Bonny
  • 1
  • 2