BankAccount无法正常工作

时间:2018-04-27 17:45:35

标签: java junit syntax

我的代码给出了junitframeworkComparisonFailure:预期的chequing2但是为null。我正在使用单元测试来检查我的代码,我也在下面。非常感谢你。它还有其他错误,但这是我想要解决的问题。如果我的问题很明显,我很抱歉,我刚刚开始。

public class BankAccount {
private String accountNumber;
private double balance;
private double withdrawalFee;
private double annualInterestRate;
private String BankAccount; 

public BankAccount(String account_Number) {
     account_Number = accountNumber ;

}
public BankAccount(String account_Number,double iNITIAL_BALANCE)
{

    account_Number = accountNumber ;
    iNITIAL_BALANCE = balance; 
} 

public BankAccount(String account_Number, double iNITIAL_BALANCE, double wITHDRAWAL_FEE,
        double aNNUAL_INTEREST_RATE) {
    account_Number = accountNumber ;
    iNITIAL_BALANCE = balance; 
    aNNUAL_INTEREST_RATE = annualInterestRate ; 
    wITHDRAWAL_FEE = withdrawalFee; 

}

public void deposit(double amount)
{  
    double newBalance = balance + amount;
    balance = newBalance;
}
public void withdraw(double amount)
{   
    double newBalance = balance - amount;
    balance = newBalance;
}

public double getAnnualInterestRate(){
    return (annualInterestRate);
}
public String getAccountNumber()
{
    return accountNumber ;
}
public void getannualInterestRate(double rate) {
    if (rate >= 0.00) {
        annualInterestRate = rate ;}
}
public double getBalance()
{   
    return balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
    this.annualInterestRate = annualInterestRate;


}
public double getWithdrawalFee() {
    return withdrawalFee;
}
public void setWithdrawalFee(double withdrawalFee)    {
    this.withdrawalFee = withdrawalFee;
}
public boolean isOverDrawn(){
    if (0 > balance){    
        return true;
    } else {
        return false;
    }
}   

public String toString(){
return (" BankAccount" +" "accountNumber" +":"+balance);
}
}

测试:

import static org.junit.Assert.assertEquals;

public class BankAccountTest extends junit.framework.TestCase
{

final double INITIAL_BALANCE = 100;
final double WITHDRAWAL_FEE = 2.0;
final double ANNUAL_INTEREST_RATE = 5.0;

BankAccount chequing1;
BankAccount chequing2;
BankAccount chequing3;

protected void setUp()
{

    System.out.println("setup");
    chequing1 = new BankAccount("chequing1");
    chequing2 = new BankAccount("chequing2", INITIAL_BALANCE);
    chequing3 = new BankAccount("chequing3", INITIAL_BALANCE, WITHDRAWAL_FEE, ANNUAL_INTEREST_RATE);  
    assertEquals(3,3);
}

public void testAccessors()
{
    assertEquals("chequing2",  chequing2.getAccountNumber());
    assertEquals(INITIAL_BALANCE,  chequing2.getBalance(), 0);
    assertEquals(ANNUAL_INTEREST_RATE,  chequing3.getAnnualInterestRate(), 0);
    assertEquals(WITHDRAWAL_FEE,  chequing3.getWithdrawalFee(), 0);
}

public void testMutators()
{
    //test for setters
    chequing1.setWithdrawalFee(3.0);
    chequing1.setAnnualInterestRate(4.0);
    assertEquals(3.0,  chequing1.getWithdrawalFee(), 0);
    assertEquals(4.0,  chequing1.getAnnualInterestRate(), 0);
    //spac      
}

public void testDeposits()
{                   
    //test various deposits and withdrawals
    chequing1.setWithdrawalFee(3.0);

    chequing1.deposit(500);
    assertEquals(500.0,  chequing1.getBalance(), 0);

    chequing1.withdraw(200);
    assertEquals(297.0,  chequing1.getBalance(), 0);            
    assertEquals(false,  chequing1.isOverDrawn());

    chequing1.withdraw(300); 
    assertEquals(-6.0,  chequing1.getBalance(), 0);
    assertEquals(true,  chequing1.isOverDrawn());

    chequing2.deposit(0.42);
    assertEquals(100.42,  chequing2.getBalance(), 0.00);
    chequing2.deposit(0.001);
    assertEquals(100.421,  chequing2.getBalance(), 0.001);
}

public void testToString() {

    assertEquals("BankAccount chequing1: $0.00",  chequing1.toString());
    assertEquals("BankAccount chequing2: $100.00",  chequing2.toString());

    chequing2.deposit(0.42);
    assertEquals("BankAccount chequing2: $100.42",  chequing2.toString());

    chequing2.deposit(0.001);
    assertEquals("BankAccount chequing2: $100.42",  chequing2.toString());

    BankAccount chequing4 = new BankAccount("chequing4", -100);
    assertEquals("BankAccount chequing4: ($100.00)",  chequing4.toString());
    assertEquals("BankAccount chequing4: ($100.00)",  chequing4.toString());
}

}

1 个答案:

答案 0 :(得分:1)

构造函数中的赋值是向后的:

public BankAccount(String account_Number) {
  this.accountNumber= account_Number ;
}

public BankAccount(String account_Number,double iNITIAL_BALANCE)
{

  this.accountNumber = account_Number ;
  this.balance = iNITIAL_BALANCE; 
} 

public BankAccount(String account_Number, double iNITIAL_BALANCE, double 
    wITHDRAWAL_FEE,
    double aNNUAL_INTEREST_RATE) {
  this.accountNumber= account_Number ;
  this.balance= iNITIAL_BALANCE ; 
  this.annualInterestRate= aNNUAL_INTEREST_RATE ; 
  this.withdrawalFee= wITHDRAWAL_FEE ; 
}