預金、引き出し、アカウントの作成、およびすべての残高の表示ができる銀行を作成しています。
私の 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;
}