0

ArrayLists の textfield を介して入力データを保存し、ファイルに保存するこのボタンがあります。

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());
    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");


    } catch(FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this, "Can't create your account!");
    } catch (IOException ex) {
        Logger.getLogger(JFrameNewAccount.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            file.close();
        } catch (IOException ex) {
            Logger.getLogger(JFrameNewAccount.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Withdraw_AccountName と Withdraw_AccountNumber のテキスト フィールドもありますが、配列リスト内の口座番号を検索するにはどうすればよいですか? Withdraw_AccountNumber が arraylists の口座番号と一致すると、Withdraw_AccountName に口座名も表示されます。助けてください。

4

1 に答える 1

1

BankAccounts を反復処理して、必要な口座番号を持つものを見つける必要があります。

for(BankAccount account: list) {
    if (account.getAccountNo().equals(accountNumberToSearchFor)){
        // found ya! set the correct text field
        break;
    }
}

または、銀行口座への口座番号のマップを作成する Map に BankAccounts を格納することもできます。または、カスタム コンパレータを作成して、アカウント番号でソートされた Java Collection を使用することもできます。

于 2013-05-30T04:09:18.077 に答える