私は、提供された銀行口座の値が口座配列に追加されるかどうかをテストするドライバを持っています。ここで要素値をJavaの配列に追加する方法は?
はドライバーである:ここでは
public class CustomerTest {
private static int customerCounter = 0;
public static boolean test1(){
System.out.println("Test1: create a customer");
Customer c = new Customer("John", "Green");
customerCounter++;
return c.getName().equals("John Green") && customerCounter == c.getCustomerID();
}
public static boolean test2() {
System.out.println("Test2: create two customers");
Customer c1 = new Customer("John", "Green");
Customer c2 = new Customer("Mike", "Red");
customerCounter += 2;
return c1.getName().equals("John Green") && (customerCounter - 1) == c1.getCustomerID()
&& c2.getName().equals("Mike Red") && (customerCounter) == c2.getCustomerID();
}
public static boolean test4() {
System.out.println("Test4: copy a customer");
Customer c1 = new Customer("John", "Green");
Customer c2 = new Customer("Mike", "Red");
c1.copy(c2);
customerCounter += 2;
return c1.getName().equals("Mike Red") && (customerCounter) == c1.getCustomerID()
&& c2.getName().equals("Mike Red") && (customerCounter) == c2.getCustomerID()
&& c1 != c2;
}
public static boolean test5() {
System.out.println("Test5: add an account to a customer.");
BankAccount b = new BankAccount();
Customer c1 = new Customer("John", "Green");
customerCounter ++;
if (!c1.addAccount(b))
return false;
return c1.toString().equals("John Green, " + c1.getCustomerID() + "\n" + b.toString() + "\n");
}
public static void main(String[] args) {
String result = "";
//System.out.print("Test 1: ");
result = test1() ? "pass." : "failed.";
System.out.println(result);
//System.out.print("Test 2: ");
result = test2() ? "pass." : "failed.";
System.out.println(result);
//System.out.print("Test 4: ");
result = test4() ? "pass." : "failed.";
System.out.println(result);
//System.out.print("Test 5: ");
result = test5() ? "pass." : "failed.";
System.out.println(result);
}
}
は、私がこれまでに書かれたものです。 addAccountsメソッドは、私がアカウント配列に値を追加するために使用しているメソッドです。同じ口座番号の勘定科目に勘定科目がすでに存在する場合、または勘定科目がいっぱいである場合、このメソッドはfalseを返します。
public class Customer {
public static final int MAX_ACCOUNTS = 5;
private String firstName;
private String lastName;
private int customerID;
private BankAccount[] accounts;
private int numAccounts;
private static int nextCustomerID = 1;
//default constructor
public Customer() {
firstName = "";
lastName = "";
customerID = nextCustomerID;
accounts = null;
numAccounts = 0;
nextCustomerID++;
}
public Customer (String first, String last)
{
this.firstName = first;
this.lastName = last;
this.customerID = nextCustomerID;
nextCustomerID++;
}
public void copy (Customer copyFrom) {
this.firstName = copyFrom.firstName;
this.lastName = copyFrom.lastName;
this.customerID = copyFrom.customerID;
this.accounts = copyFrom.accounts;
this.numAccounts = copyFrom.numAccounts;
}
public boolean addAccount (BankAccount newAccount[])
{
if (numAccounts < 5){
int BankAccountLen = newAccount.length;
BankAccount[] newBank = new BankAccount[BankAccountLen];
System.arraycopy(newAccount, 0, newBank, 0, BankAccountLen);
return true;
}else{
return false;
}
}
public BankAccount[] getAccounts()
{
return accounts;
}
public String getFirst(){
return firstName;
}
public String getLast(){
return lastName;
}
public String getName()
{
return String.format("%s %s", getFirst(), getLast());
}
public int getCustomerID()
{
return customerID;
}
public String toString()
{
return String.format("%s %s %s", getFirst(), getLast(), getCustomerID());
return String.format("%d",numAccounts);
}
}
TEST5配列が成功したアカウントに追加した場合、それはパスを示すべき時に失敗した表示されます。しかし例外も同様に投げています:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method addAccount(BankAccount[]) in the type Customer is not applicable for the arguments (BankAccount)
at CustomerTest.test5(CustomerTest.java:44)
at CustomerTest.main(CustomerTest.java:192)
return newBank;関数がブール値を返すことを期待しているために窒息しています。 –
@MiloStaKic私の好奇心のためだけに:あなたは自分で作成するのではなく、既存の単体テストフレームワークの1つを使用しますか? –
@JeremyKahan私の間違い。私はそれを真と偽に戻すように変更しました。 –