我正在創建一個可以存款,取款,創建賬戶和顯示所有餘額的銀行。找到一個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;
}
'a.getAccountNumber()'的確切返回類型是什麼? 'Integer'? 'int'? – rgettman
getAccountNumber()返回int – user2600585