-1

在arraylist中使用accountno返回帐户名?我在搜索 accountno 时无法返回帐户名称。我只想当用户在帐户字段中输入帐户编号时,与该帐户编号关联的帐户名称将显示在帐户名称字段中。

private void txt_wdaccountnumberActionPerformed(java.awt.event.ActionEvent evt) {
for(BankAccount account: list){
    if(account.getAccountNo().equals(txt_wdaccountnumber.getText())){
    txt_wdaccountname.setText(account.getAccountName());
        System.out.println(txt_wdaccountname);
        return;

添加账户:

private void btnSaveAActionPerformed(java.awt.event.ActionEvent evt) {                                         
    BankAccount account = new BankAccount();

    ButtonGroup bg = new ButtonGroup();
    bg.add(rad_savings);
    bg.add(rad_checking);
    account.setAccountName(txt_accountname.getText());
    account.setAccountNo(txt_accountnumber.getText());
    account.setBalance(Double.parseDouble(txt_initialbalance.getText()));
    list.add(account);
    String fileName = "bank.txt";
    FileWriter file = null;
    try {
        file = new FileWriter(fileName, true);
        PrintWriter pw = new PrintWriter(file);
        for (BankAccount str : list) {
            pw.println(str);
           }

        pw.flush();
        pw.println("\n");
4

1 回答 1

1

如果 if 条件为真,那么您的 BankAccount 由 account 变量持有——然后您就可以使用它:

for(BankAccount account: list){
  if(account.getAccountNo().equals(txt_wdaccountnumber.getText())){

    // the account variable will now hold the BankAccount of interest

    // let's test to see if we are getting to this spot

    String accountName = account.getName();
    System.out.println("inside if block. accountName: " + accountName);

    txt_wdaccountname.setText(accountName);
    return;
  }
}

请注意,我不确定您如何称呼显示帐户名称的 JTextField 或获取该名称的 BankAccount 方法,所以我在上面的代码中猜到了,但您应该明白这一点并从那里开始。

于 2013-06-03T02:03:43.820 回答