2016-11-06 26 views
0

私は、提供された銀行口座の値が口座配列に追加されるかどうかをテストするドライバを持っています。ここで要素値を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) 
+2

return newBank;関数がブール値を返すことを期待しているために窒息しています。 –

+0

@MiloStaKic私の好奇心のためだけに:あなたは自分で作成するのではなく、既存の単体テストフレームワークの1つを使用しますか? –

+0

@JeremyKahan私の間違い。私はそれを真と偽に戻すように変更しました。 –

答えて

0

修正する必要があることはたくさんあります。また、配列に要素を追加するあなたのやり方は、本当に奇妙なようです。私はより良い方法を使用することをお勧めします。それが許可されている場合は、配列リストを使用することもできます。

//default constructor 
public Customer() { 
    firstName = ""; 
    lastName = ""; 
    customerID = nextCustomerID; 
    accounts = new BankAccount[MAX_ACCOUNTS]; 
    numAccounts = 0; 
    nextCustomerID++; 
} 

public Customer (String first, String last) 
{ 
    this.firstName = first; 
    this.lastName = last; 
    this.customerID = nextCustomerID; 
    nextCustomerID++; 
    accounts = new BankAccount[MAX_ACCOUNTS]; 
} 

今、これにaddAccountを変更します。

まず、あなたは、2つのコンストラクタでaccounts配列を初期化する必要があります。あなたがArrayListを使用することを許可されている場合は

public boolean addAccount (BankAccount newAccount) 
{ 
    if (numAccounts < MAX_ACCOUNTS || Arrays.asList(accounts).contains(newAccount)){ 
     accounts[numAccount] = newAccount; 
     numAccounts++; 
     return true; 
    } else{ 
     return false; 
    } 
} 

は、その後、コードがはるかに簡単になります!コンストラクタで

private ArrayList<BankAccount> accounts; 

をして、それをinitailize:

まず配列リストとしてaccountsを宣言

//default constructor 
public Customer() { 
    firstName = ""; 
    lastName = ""; 
    customerID = nextCustomerID; 
    accounts = new ArrayList<>(); 
    numAccounts = 0; 
    nextCustomerID++; 
} 

public Customer (String first, String last) 
{ 
    this.firstName = first; 
    this.lastName = last; 
    this.customerID = nextCustomerID; 
    nextCustomerID++; 
    accounts = new ArrayList<>(); 
} 

をあなたはまた、これにgetAccounts方法を変更する必要があります。

public ArrayList<BankAccount> getAccounts() 
{ 
    return accounts; 

} 

この変更のために、あなたはchanしなければならないかもしれません新しいArrayList<BankAccount>リターンタイプに準拠するようにテストしてください。

あなたが見ることができるように、addAccount方法は非常に簡単になる:

public boolean addAccount (BankAccount newAccount) 
{ 
    if (accounts.size() < MAX_ACCOUNTS || accounts.contains(newAccount)){ 
     accounts.add(newAccount); 
     return true; 
    } else{ 
     return false; 
    } 
} 

また、すべて一緒にnumAccounts変数を削除することができます!

toStringのメソッドは、配列の手法を採用している場合は、このようになります(これは私が考えることができる最良の解決策ですが、配列を扱うのが悪いです。):

return String.format("%s %s %s", getFirst(), getLast(), getCustomerID()) + 
    Arrays.toString(Arrays.asList(accounts).stream().filter(x -> x != null).toArray()); 

あなたは配列リストのアプローチを使用している場合、それははるかに簡単です:

return String.format("%s %s %s", getFirst(), getLast(), getCustomerID()) + 
    accounts.toString(); 
+0

accounts配列のnull以外のエントリは何ですか? –

+0

null以外のエントリについては何も言及しませんでした。どういう意味ですか? @MiloStaKic – Sweeper

+0

私は元の質問に追加することを忘れたtoStringメソッドでそれらを返す必要があります。 –

0

悪い質問

あなたはあまりにも多くのコードはここに掲載方法を持っています。私たちは、あなたの問題を示すのに必要な最低限のコード例を期待しています。そうすることで、おそらくあなた自身が疑問に答えるでしょう。

ところで、BankAccountLenは、Javaの命名規則に従って、bankAccountLenにする必要があります。

エラーメッセージは、それが正確な問題を示していますように、すべての

は、お使いのエラーメッセージを読んで伝えます。このメソッドは、BankAccountオブジェクトの配列を取ると宣言しましたが、BankAccountオブジェクトを直接渡します。

クッキーの箱はクッキーではありません。クールでいっぱいの車そのものは、あごではありません。内部のものと物のコレクションを融合させないでください。メソッドへの引数を宣言するときは、明確かつ正確でなければなりません。

あなたのメソッド宣言は間違いであったと思いますが、BankAccountオブジェクトのコレクションではなく、単一のBankAccountオブジェクトを渡すつもりでした。したがって、メソッド宣言から[]を削除してください。

public boolean addAccount (BankAccount newAccount) { … 

List

ヒント:配列ではなく、成長することができます順序付きコレクションのためにArrayListようListを使用してください。

もう1つのヒント:JUnitのような単体テストフレームワークを使用することを検討してください。

+0

を使用しても失敗しています。「Java命名規則に従わない"本当に?私は、OPの命名規則はかなりよく、クラスはPascalCase、メソッドと変数はcamelCase、定数はすべて大文字になっていると思います。テストクラスの 'c1'と' b'を除いて。しかし、私は彼らがテストのために使用されるちょうどダミー変数であることを意味します... – Sweeper

+0

@ Sweeper –

関連する問題