银行账户2

2024-10-01 04:58:46 发布

您现在位置:Python中文网/ 问答频道 /正文

现在所有的函数测试都在工作(返回True,除了detact和transferTo),我有点迷失了如何修复这些测试并让它们返回True。我的目标是运行函数并使所有测试返回true。谢谢

class BankAccount:

    def __init__(self, nameIn, balanceIn, pinIn):
        self.name = nameIn
        self.balance = balanceIn
        self.pin = pinIn

    def checkPin(self, pin):
        if self.pin == pin:
            return True
        else:
            return False

    def getBalance(self, pin):
        if pin == self.pin:
            return self.balance
        else:
            return False

    def deposit(self, amount):
        if amount > 0:
            self.balance = self.balance + amount
            return True
        else:
            return False

    def withdraw(self, pin, amt):
        if pin == self.pin and amt <= self.balance:
            self.balance = self.balance - amt
            return True
        else:
            return False

    def transferTo(self, pin, amt, account2):
        if pin == self.pin and self.withdraw == True and self.deposit == True:
            self.withdraw(amt)
            account2.deposit(amt)

def testCheckPin(account1, goodPin, badPin):
    right = account1.checkPin(goodPin)
    wrong = account1.checkPin(badPin)
    check = right==True and wrong==False
    print (check, "- checkPin method okay")
    return check

def testGetBalance(account1, goodPin, badPin, startingBalance):
    balance = account1.getBalance(goodPin)
    badPinBalance = account1.getBalance(badPin)
    check = balance==startingBalance and badPinBalance == False
    print (check, "- getBalance method okay")
    return check

def testDeposit(account1, pin, goodAmt):
    oldAmt = account1.getBalance(pin)
    newAmt = account1.getBalance(pin) + goodAmt

    badAmtCheck = newAmt > oldAmt
    print ("\t", badAmtCheck, "- deposit method with bad amount")

    if newAmt != oldAmt + goodAmt:
        goodAmtCheck = False
    else:
        goodAmtCheck = True

    print ("\t", goodAmtCheck, "- deposit method with good amount")

    check = goodAmtCheck and badAmtCheck
    print (check, "- deposit method okay:", check)
    return check


def testWithdraw(account1, goodPin, badPin, goodAmt):
    oldBal = account1.getBalance(goodPin)
    #a bad pin
    withBadPin = account1.withdraw(badPin, goodAmt)
    badPinCheck = withBadPin==False
    #too much money to withdraw
    tooMuch = oldBal + 1
    withBadAmt = account1.withdraw(goodPin, tooMuch)
    badAmtCheck = withBadAmt==False
    #a negative amount to withdraw
    withNegAmt = account1.withdraw(goodPin, -1)
    negAmtCheck = withNegAmt == False
    #a good withdrawal
    withGoodPin = account1.withdraw(goodPin, goodAmt)
    goodPinCheck = withGoodPin==True

    check = goodPinCheck and badPinCheck and badAmtCheck and negAmtCheck
    check = False
    print (check, "- withdraw method okay")
    return check

def testTransferTo(account1, account2, pin1, pin2, badPin, goodAmt):
    oldBal1 = account1.getBalance(pin1)
    oldBal2 = account2.getBalance(pin2)
    #transferTo - bad pin
    withBadPin = account1.transferTo(badPin, goodAmt, account2)
    balBadPin1 = account1.getBalance(pin1)
    balBadPin2 = account2.getBalance(pin2)
    badPinCheck = withBadPin==False and balBadPin1==oldBal1 and balBadPin2==oldBal2
    print ("\t", badPinCheck, "- transferTo method with bad pin")
    #transferTo - too much money
    tooMuch = oldBal1 + 1
    withBadAmt = account1.transferTo(pin1, tooMuch, account2)
    balBadAmount1 = account1.getBalance(pin1)
    balBadAmount2 = account2.getBalance(pin2)
    badAmtCheck = withBadAmt==False and balBadAmount1==oldBal1 and balBadAmount2==oldBal2
    print ("\t", badAmtCheck, "- transferTo method with bad amount")
    #transferTo - negative money
    withNegAmt = account1.transferTo(pin1, -1, account2)
    balNegAmount1 = account1.getBalance(pin1)
    balNegAmount2 = account2.getBalance(pin2)
    negAmtCheck = withNegAmt==False and balNegAmount1==oldBal1 and balNegAmount2==oldBal2
    print ("\t", negAmtCheck, "- transferTo method with negative money")
    #transferTo - good
    withGoodPin = account1.transferTo(pin1, goodAmt, account2)
    bal1 = account1.getBalance(pin1)
    bal2 = account2.getBalance(pin2)
    goodPinCheck = withGoodPin==True and bal1==(oldBal1-goodAmt) and bal2 == (oldBal2 + goodAmt)
    print ("\t", goodPinCheck, "- transferTo method with good pin")

    check = goodPinCheck and badPinCheck and badAmtCheck and negAmtCheck
    print (check, "- transferTo method okay")
    return check

def testBankAccount():
    print ("Begin testing: Bank Account")
    goodPin = "1234"
    badPin = "1334"
    badPin2 = "0000"
    startAmount = 100
    goodAmt = 50
    testAcct = BankAccount("Jose", startAmount,goodPin)

    t1 = testCheckPin(testAcct, goodPin, badPin)
    t2 = testGetBalance(testAcct, goodPin, badPin2, startAmount)
    t3 = testDeposit(testAcct, goodPin, goodAmt)
    t4 = testWithdraw(testAcct, goodPin, badPin, goodAmt)

    pin2 = "3366"
    testAcct2 = BankAccount("Jesse", 0, pin2)

    t5 = testTransferTo(testAcct, testAcct2, goodPin, pin2, badPin, goodAmt)

    check = t1 and t2 and t3 and t4 and t5
    print (check, "- Bank Account class okay")

def main():
    testBankAccount()    

main()

Tags: andselffalsereturndefcheckpinprint
2条回答
  • testGetBalance你知道吗

    check = balance==startingBalance and badPinBalance == -1
    #                                     should be False ^^
    #    (or better yet, "not badPinBalance")
    
  • testWithdraw中,最后第三行是check = False。这可能与考试失败有关。

  • withdraw中,您从不检查取款金额是否为负,这会导致testWithdraw第三次检查失败。

编辑:重大疏忽!在

  • testDeposit实际上从不存款;-)

不那么关键的是:在相当多的地方你会做类似的事情

^{pr2}$

哪一个可以用更习惯的方式写

badAmtCheck = newAmt > oldAmt

另外,您可能需要阅读assert语句。在

您没有正确实例化您的银行帐户。。你的pin应该是第三个参数。在

testAcct = BankAccount("Jose", startAmount, pin)

相关问题 更多 >