我正在创建一个可以存入,提取,创建帐户并显示所有余额的银行。

我的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().intValue() == accountNumber.intValue();


可能就是您想要的。另外,他们所说的打破循环。

10-05 22:48
查看更多