2016-03-29 20 views
2

こんにちは、これは私の最初の投稿です。私は、アカウントと呼ばれる既成のデータ型クラスに基づいてクラスを作成することを想定しています。私の主な問題は、public int findAccountByAcctNumber(int acctNumber)と public account removeAccount(int index)の中にあります。難しいのは、異なるデータ型でこれらのメソッドを作成する方法です。java互換性のない型のクラスデータ型

import java.util.*; 

public class Bank 
{ 

    private ArrayList<Account> Accounts; 
    private int currentSize; 
    private String bankName; 


    public Bank(String name) 
    { 
     bankName = name; 
     Accounts = new ArrayList<Account>(0); 

    } 


    public void addAccount(Account acct){ 
     Accounts.add(acct); 

    } 
    public int findAccountByAcctNumber(int acctNumber){ 
     int tempIndex = -1; 
     for(int i = 0; i < Accounts.size(); i++){ 
      if(Accounts.get(i) == acctNumber){ 
       tempIndex = i; 
      } 
     } 
     return tempIndex; 

    } 
    public Account removeAccount(int index){ 

     Accounts.remove(index); 
     Account 
     return index; 

    } 
    public String toString(){ 
     String output = ""; 

     output += bankName + "/n"; 
     for(int i = 0; i < arrlist.size(); i++){ 
      output += Accounts.get(i); 
     } 
     return output; 


    } 


} 

答えて

1

あなたは私たちにAccountクラスを示していないが、私はそれがフィールドaccountNumberを持って推測しています。

あなたはAccountオブジェクト自体、フィールドに入力された口座番号を比較する必要はありません。

public int findAccountByAcctNumber(int acctNumber){ 
     int tempIndex = -1; 
     for(int i = 0; i < Accounts.size(); i++){ 
      //NOT if(Accounts.get(i) == acctNumber){ -> 
      if(Accounts.get(i).getAccountNumber() == acctNumber){ 
       tempIndex = i; 
      } 
     } 
     return tempIndex; 

    } 

削除は、(ArrayListには既にこの機能を実装している)非常に単純です:

public Account removeAccount(int index){ 
    return Accounts.remove(index); 
} 
関連する問題