如何调用数组中保存的所有信息?

时间:2014-01-04 06:46:45

标签: java jframe

每当我进入第一个账户..我可以检查所有信息,我可以提取,我可以存款..但是当我进入第二个银行账户并检查第一个输入账户的信息..我无法得到它..它显示错误消息“没有找到记录!”..有人帮我吗?

继承人代码:

  public class BankAccount extends JFrame implements ActionListener
{
private JLabel acct_noL, nameL, addressL, bdayL, amountL;
private JTextField acct_noTF, nameTF, addressTF, bdayTF, amountTF;
private JButton addB, checkB, clearB, exitB, withdrawB, depositB;

SavingsAccount[] savings = new SavingsAccount[10];

private int index = 0;
private int acct_no = 1000;

public BankAccount()
{
    for(int ctr=0;ctr<=9;ctr++)
    {
        savings[ctr] = new SavingsAccount();
    }
    setTitle("Bank Account");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    acct_noL = new JLabel("Account Number",SwingConstants.RIGHT);
    nameL = new JLabel("Name",SwingConstants.RIGHT);
    addressL = new JLabel("Address",SwingConstants.RIGHT);
    bdayL = new JLabel("Birthday",SwingConstants.RIGHT);
    amountL = new JLabel("Amount to Save",SwingConstants.RIGHT);

    acct_noTF = new JTextField(10);
    nameTF = new JTextField(10);
    addressTF = new JTextField(10);
    bdayTF = new JTextField(10);
    amountTF = new JTextField(10);

    acct_noTF.setText("1000");

    addB = new JButton("Add Account");
    checkB = new JButton("Check Account");
    clearB = new JButton("Clear");
    exitB = new JButton("Exit");
    withdrawB = new JButton("Withdraw");
    depositB = new JButton("Deposit");

    addB.addActionListener(this);
    checkB.addActionListener(this);
    clearB.addActionListener(this);
    exitB.addActionListener(this);
    withdrawB.addActionListener(this);
    depositB.addActionListener(this);

    Container pane = getContentPane();
    pane.setLayout(new GridLayout(8,2));
    pane.add(acct_noL);
    pane.add(acct_noTF);
    pane.add(nameL);
    pane.add(nameTF);
    pane.add(addressL);
    pane.add(addressTF);
    pane.add(bdayL);
    pane.add(bdayTF);
    pane.add(amountL);
    pane.add(amountTF);
    pane.add(addB);
    pane.add(checkB);
    pane.add(withdrawB);
    pane.add(depositB);
    pane.add(clearB);
    pane.add(exitB);
}

private String showAccountInfo(int an)
{
    String info = "Record Not Found!";

    for(int s=0;s<=9;s++)
    {
        if(savings[s].getAcct_No()==an)
        {
            info = "Account Number: " + savings[s].getAcct_No() + "\nName: " + savings[s]. getName() +
                     "\nAddress: " + savings[s].getAddress() + "\nBirthday " + savings[s].getBday() + 
                     "\nBalance: " + savings[s].getAmount();
        }
    }
    return info;
}

    public void actionPerformed(ActionEvent e)
{
    Object source = e.getSource();
    int inputAcct_No;
    CheckingAccount checking = new CheckingAccount();

    if(source==addB)
    {
    if(index<=9)
    {
        savings[index].setAcct_No(acct_no);
        savings[index].setName(nameTF.getText());
        savings[index].setAddress(addressTF.getText());
        savings[index].setBday(bdayTF.getText());
        savings[index].setAmount(Integer.parseInt(amountTF.getText()));
        acct_no++;
        acct_noTF.setText(""+acct_no);
        nameTF.setText(null);
        addressTF.setText(null);        
        bdayTF.setText(null);
        amountTF.setText(null);
        JOptionPane.showMessageDialog(null,"Bank Account Saved!");
    }
    else
    {
        JOptionPane.showMessageDialog(null,"Bank Accounts are Full!");
    }
    }       
    if(source==checkB)
    {
        inputAcct_No = Integer.parseInt(JOptionPane.showInputDialog("Enter Account Number"));
        JOptionPane.showMessageDialog(null,this.showAccountInfo(inputAcct_No));
    }

    if(source==withdrawB)
    {
        inputAcct_No = Integer.parseInt(JOptionPane.showInputDialog("Enter Account Number"));
        JOptionPane.showMessageDialog(null,this.showAccountInfo(inputAcct_No));
        if(showAccountInfo(inputAcct_No)=="Record Not Found!")
        {
        }
        else
        {
        savings[index].setWithdraw(Integer.parseInt(JOptionPane.showInputDialog("Enter amount to withdraw")));
        JOptionPane.showMessageDialog(null,"Your balance is: " + (savings[index].getBalanceW()));
        savings[index].setAmount(savings[index].getBalanceW());

        int confirm_issue_date = JOptionPane.showConfirmDialog(null,"Would you like to issue cheque?");
        if(confirm_issue_date==JOptionPane.YES_OPTION)

        {
            String Receiver = JOptionPane.showInputDialog(null,"Enter name of the receiver");
            checking.setIssue_Date(Integer.parseInt(JOptionPane.showInputDialog("Enter amount to be issued")));
            JOptionPane.showMessageDialog(null,"Cheque is issued to " + Receiver + " with the amount of " +checking.getIssue_Date() + 
                                                            "\nYour balance is: " + (savings[index].getAmount()-checking.getIssue_Date()));
        savings[index].setAmount((savings[index].getAmount()-checking.getIssue_Date()));
        }
        else if(confirm_issue_date==JOptionPane.NO_OPTION)
        {
        }
        else if(confirm_issue_date==JOptionPane.CANCEL_OPTION)
        {
        }
        }
    }

    if(source==depositB)
    {
        inputAcct_No = Integer.parseInt(JOptionPane.showInputDialog("Enter Account Number"));
        JOptionPane.showMessageDialog(null,this.showAccountInfo(inputAcct_No));
        if(showAccountInfo(inputAcct_No)=="Record Not Found!")
        {
        }
        else
        {
        savings[index].setDeposit(Integer.parseInt(JOptionPane.showInputDialog("Enter amount to deposit")));
        JOptionPane.showMessageDialog(null,"Your balance is: " + (savings[index].getBalanceD()));
        savings[index].setAmount(savings[index].getBalanceD());
        }
    }

    if(source==clearB)
    {
        nameTF.setText(null);
        addressTF.setText(null);
        bdayTF.setText(null);
        amountTF.setText(null);
    }

    if(source==exitB)
    {
        System.exit(0);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在index++;声明之前添加acct_no++;。您需要添加以下功能。

private void withdrawAccountInfo(int an)
{    
    for(int s=0;s<=9;s++)
    {
        if(savings[s].getAcct_No()==an)
        {
            // do your withdrawal process here with variable "s"; not "index"
            savings[s].setWithdraw(Integer.parseInt(JOptionPane.showInputDialog("Enter amount to withdraw")));
            JOptionPane.showMessageDialog(null,"Your balance is: " + (savings[s].getBalanceW()));
            savings[s].setAmount(savings[s].getBalanceW());
            // do more process
            break;
        }
    }
}

actionPerformed(ActionEvent e)函数中 - if(source==withdrawB)块内 - 调用withdrawAccountInfo(int an)函数