-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
4

3 回答 3

2

下面的代码有效!

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
    
于 2016-09-09T23:01:08.557 回答
0

仔细阅读作业:你每次设置余额为500;不需要另一个构造函数参数。没有名字,没有账号。

“无实现”方法很简单:

def method():
    pass

这会让你感动吗?不要试图让它变得比实际更难:从简单开始,一次写几行,然后在继续之前调试它们。

于 2016-09-07T23:12:37.370 回答
0

要设置仅带有 self 参数的构造函数,请执行以下操作

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

对于这个特定问题,请逐步阅读说明并按照说明执行方法。这里的问题是逻辑正确。在储蓄账户类中,确保首先检查透支,然后检查提款不会导致余额低于 500。这个顺序很重要。我有一个类似的问题,我在做出改变后解决了它。

这是解决方案:

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
于 2016-09-17T09:32:29.043 回答