我正在创建一个可以存款、取款、创建帐户和显示所有余额的银行。
我的 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;
}