银行账户2

时间:2014-05-07 01:07:45

标签: python

现在所有的功能测试都在工作(返回True,除了withdraw和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()

2 个答案:

答案 0 :(得分:1)

您正在错误地实例化您的BankAccount ..您的PIN应该是第三个参数。

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

答案 1 :(得分:0)

  • testGetBalance进行

    check = balance==startingBalance and badPinBalance == -1
    #                                     should be False ^^
    #    (or better yet, "not badPinBalance")
    
  • <{1}}中的
  • ,倒数第三行为testWithdraw可能与测试失败有关。

  • check = False中,您从未检查过提款金额是否为负值,导致withdraw未能通过第三次检查。

编辑:主要疏忽!

  • testWithdraw实际上从未存款; - )

不那么关键:在很多地方你会做类似

的事情
testDeposit

可以更具惯用性地写

if newAmt <= oldAmt:
    badAmtCheck = False
else:
    badAmtCheck = True

此外,您可能希望阅读badAmtCheck = newAmt > oldAmt 声明。