从arraylist获取数据

时间:2013-05-26 15:33:43

标签: java swing arraylist

我有一个新的帐户框架,其中包括帐号,帐户名,初始权限,存储在arraylist中并保存在文本文件中。我还有一个提取和存款框架,其中只包含帐号和帐户名。我的问题是,如何从保存在文本文件中的arraylist的新帐户中获取/显示帐号,帐号名称。 例如:帐户名称:James,帐号:201,Initialbalance:5000。在Withdraw框架中,当我在帐号文本字段中输入帐号时,它将搜索保存在文本文件中的arraylist,并显示帐户名称在帐户名称字段中。此外,当用户试图提取更大的初始余额时,它会发出“资金不足!”的消息。

以下是我的新帐户代码,我不知道如何从撤销框架中获取数据。

public class JFrameNewAccount extends javax.swing.JFrame {
ArrayList<String> al = new ArrayList<String>();    

  public JFrameNewAccount() {
    initComponents();

   groupButton();


 }


private void btnCancelAActionPerformed(java.awt.event.ActionEvent evt) {                                           
        this.setVisible(false);
    }



private void btnSaveAActionPerformed(java.awt.event.ActionEvent evt) {                                         
            ButtonGroup bg = new ButtonGroup();
            bg.add(rad_savings);
            bg.add(rad_checking);
        al.add(txt_accountnumber.getText());
    al.add((txt_accountname.getText()));
    if(rad_savings.isSelected()){
            al.add(rad_savings.getText());
    }
        else{
            al.add(rad_checking.getText());
    }
        al.add(txt_initialbalance.getText());
    if(rad_savings.isSelected()){
            al.add(txt_interestrate.getText());
    }
        else{
            al.add(txt_overdraft.getText());
    }
    String fileName = "bank.txt";
        FileWriter file = null;
        try {
            file = new FileWriter(fileName,true);
            PrintWriter pw = new PrintWriter(file);
            for(String str: al) {
                pw.println(str);
            }

       pw.flush();
       pw.println("\n");
        System.out.println("Write successful...");
    } catch(FileNotFoundException ex) {
        System.out.println("File not found....");
    } 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);
       }
    }
 JOptionPane.showMessageDialog(this, "Your accont has been created!");
        txt_accountname.setText("");   
        txt_accountnumber.setText("");// TODO add your handling code here:
        txt_initialbalance.setText("");
        txt_overdraft.setText("");
        txt_interestrate.setText("");
        bg.clearSelection();
        txt_accountnumber.requestFocus();




}                

0 个答案:

没有答案