2013-07-19 46 views
0

我正在創建一個可以存款,取款,創建賬戶和顯示所有餘額的銀行。找到一個ArrayList對象 - 搜索不工作?

我createButton方法工作正常 -

public void createNewAccountButtonPanel(){ 
    //create button 
    createButton = new JButton("Create New Account"); 
    //Add Listener modeled from InputFrame.Java from GroupProject 
    class AddCreateNewListener implements ActionListener{ 
     @Override 
     public void actionPerformed(ActionEvent CreateNew){ 
      //account number has to be 4 digits. Balance has to be 100 or more 
      if(accountField.getText().trim().length() != 4 || balanceField.getText().trim().length() < 3){ 
       //not correct input, tell the user to enter the correct input 
       System.out.println("Failed to create a Bank Account!"); 
       textArea.append("Please enter a Account number and a Balance!" + "\n"); 
      } 
      else 
      {//read the input 
       System.out.println("Creating a Bank Account!"); 
       Integer accountNumber = Integer.parseInt(accountField.getText()); 
       Double amount = Double.parseDouble(balanceField.getText()); 
       getBank().createNew(accountNumber, amount); 
       textArea.append("You created " + getBank().accounts.get(getBank().accounts.size()-1) + " \n"); 
      } 
      } 
    } 
    createNew = new AddCreateNewListener(); 
    createButton.addActionListener(createNew); 
} 

這裏是我的搜索 - 它總是返回null,就算我知道我已經添加帳戶...

public BankAccount search(Integer accountNumber){ 
    BankAccount found = null; 
    for(BankAccount a : accounts){ 
     if(a.getAccountNumber() == accountNumber) { 
      System.out.println("Found the account!"); 
      found = a;  
     } 
     else{ 
      System.out.println("The Account Number you entered was not found."); 
      found = null; 
     } 
    }  
    return found; 
} 

我在想什麼?這也導致我的存款和取款按鈕現在可以正常工作。我的顯示所有帳戶都能正常工作。

編輯︰看來我的搜索工作,我已經添加了break。現在的問題是如何平衡在GUI的文本區域顯示 - 它總是顯示0.0

public void displayBalancePanel(){ 
    //create the button 
    displayBalanceButton = new JButton("Display The Balance"); 
    //Add listener modeled from InputFrame.java from GroupProject 
    class AddDisplayBalanceListener implements ActionListener{ 
     @Override 
     public void actionPerformed(ActionEvent DisplayBalance){ 
      //read the input 
      Integer accountNumber = Integer.parseInt(accountField.getText()); 
      System.out.println("accountNumber to Display Balance for: " + accountNumber); 
      getBank().displayBalance(accountNumber, amount); 
      textArea.append("The Balance for Account: " + accountNumber + " is " + getAmount() + "\n"); 
     } 
    } 
    displayBalance = new AddDisplayBalanceListener(); 
    displayBalanceButton.addActionListener(displayBalance); 
} 

新的搜索 -

public BankAccount search(Integer accountNumber){ 
    BankAccount found = null; 
    for(BankAccount a : accounts){ 
     if(a.getAccountNumber().equals(accountNumber)) { 
      System.out.println("Found the account!"); 
      found = a;  
      System.out.println("a: " + a); 
      break; 
     } 
     else{ 
      System.out.println("The Account Number you entered was not found."); 
      found = null; 
     } 
    }  
    return found; 
} 
+0

'a.getAccountNumber()'的確切返回類型是什麼? 'Integer'? 'int'? – rgettman

+0

getAccountNumber()返回int – user2600585

回答

0

當您發現該帳戶,您for循環不斷去超過期望的Account。然後elsefound設置回null

當您找到該帳戶時,只需在if區塊中將其從右側返回即可。你甚至不需要else塊。

事實上,根本不需要變量found。只需直接在if塊中返回a,如果找不到該方法中的最後一個語句,則可將其更改爲return null;

+0

謝謝,一旦找到該帳戶,我已將其修改爲「中斷」。一旦我們得到其他部分的工作,我將清理它 – user2600585

1

因爲它是一個整數(不是int):

a.getAccountNumber().intValue() == accountNumber.intValue(); 

可能是你想要的東西。此外,他們所說的關於擺脫循環的說法。

+0

OP已經闡明'getAccountNumber()'返回'int',所以調用'intValue'是不必要的; 'accountNumber'取消裝箱並且比較按預期工作。 – rgettman