import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Arrays;
import java.io.*;

public class BankAccount extends JFrame {

    // Make these variables publicly available


    String file =  ("THUS.txt");

    public static String acctNumber ;
    public static String  custid , acctDateOpened;
    public static double balance ;
    private static final String EMPTY = "EMPTY";
    boolean valid1, valid2, valid3, valid4, valid5, valid6, valid7 ;

    int i = 1; 

    Account []  account =  new Account [i] ;


    // Setup Arrays for each account


    // JPanel for user inputs
    private JPanel inputDetailJPanel;


    // JLabel and Jtext field for custID
    private JTextField acctDateOpenedJTextField;
    private JLabel acctDateOpenedJLabel;

    // JLabel and Jtext field for custID
    private JTextField custidJTextField;
    private JLabel custidJLabel;

    // JLabel and JTextField for account number
    private JLabel acctNumberJLabel;
    private JTextField acctNumberJTextField;

    // JLabel and JTextField for balance
    private JLabel balanceJLabel;
    private JTextField balanceJTextField;


    // JButton to create account
    private JButton CreateAccountJButton;

    //JButton for update
    private JButton updateJButton ; 

    //JButton for update
    private JButton interestJButton ; 


    //JButton to clear account 
    private JButton clearJButton ; 



    // JButton to delete account
    private JButton DeleteAccountJButton;



    // JLabel and JTextArea to display account details
    private JLabel displayJLabel;
    private JTextArea displayJTextArea;





    // constructor
    public BankAccount() {
        createUserInterface();
    }

    // create and position GUI components; register event handlers
    private void createUserInterface() {
        // get content pane for attaching GUI components
        Container contentPane = getContentPane();

        // enable explicit positioning of GUI components
        contentPane.setLayout(null);

        // set up inputDetailJPanel
        inputDetailJPanel = new JPanel();
        inputDetailJPanel.setBounds(31, 31, 215, 273); // 3rd number controlds length , 4th width 
        inputDetailJPanel.setBorder(new TitledBorder("Account"));
        inputDetailJPanel.setLayout(null);
        contentPane.add(inputDetailJPanel);

        // set up custIDJLabel
        custidJLabel = new JLabel();
        custidJLabel.setBounds(8, 32, 90, 23);
        custidJLabel.setText("customer ID");
        inputDetailJPanel.add(custidJLabel);

        // set up cusidJTextField
        custidJTextField = new JTextField();
        custidJTextField.setBounds(104, 32, 88, 21);
        custidJTextField.setHorizontalAlignment(JTextField.RIGHT);
        inputDetailJPanel.add(custidJTextField);



        // set up AccountnumJLabel
        acctNumberJLabel = new JLabel();
        acctNumberJLabel.setBounds(8, 56, 100, 23);
        acctNumberJLabel.setText("Account Number:");
        inputDetailJPanel.add(acctNumberJLabel);

        // set up AccountnumTextField
        acctNumberJTextField = new JTextField();
        acctNumberJTextField.setBounds(112, 56, 80, 21);
        acctNumberJTextField.setHorizontalAlignment(JTextField.RIGHT);
        inputDetailJPanel.add(acctNumberJTextField);

        // set up BalanceJLabel
        balanceJLabel = new JLabel();
        balanceJLabel.setBounds(8, 80, 60, 23);
        balanceJLabel.setText("Balance:");
        inputDetailJPanel.add(balanceJLabel);

        // set up BalanceTextField
        balanceJTextField = new JTextField();
        balanceJTextField.setBounds(112, 80, 80, 21);
        balanceJTextField.setHorizontalAlignment(JTextField.RIGHT);
        inputDetailJPanel.add(balanceJTextField);

        // set up DateJLabel
        acctDateOpenedJLabel = new JLabel();
        acctDateOpenedJLabel.setBounds(8, 100, 80, 24);
        acctDateOpenedJLabel.setText("Date:");
        inputDetailJPanel.add(acctDateOpenedJLabel);

        // set up dateTextField
        acctDateOpenedJTextField = new JTextField();
        acctDateOpenedJTextField.setBounds(114, 105, 80, 21);
        acctDateOpenedJTextField.setHorizontalAlignment(JTextField.RIGHT);
        inputDetailJPanel.add(acctDateOpenedJTextField);


        // set up CreateAccountButton
        CreateAccountJButton = new JButton();
        CreateAccountJButton.setBounds(112, 152, 80, 26);
        CreateAccountJButton.setText("Create");
        inputDetailJPanel.add(CreateAccountJButton);
        CreateAccountJButton.addActionListener(

        new ActionListener() {
            // event handler called when CreateAccountJButton
            // is clicked
            public void actionPerformed(ActionEvent event) {
                CreateAccountJButtonActionPerformed(event);
            }

        }

        ); // end call to addActionListener

         // set up interesrAccountButton
        interestJButton = new JButton();
        interestJButton.setBounds(32, 238, 160, 28);
        interestJButton.setText("intrest");
        inputDetailJPanel.add(interestJButton);
        interestJButton.addActionListener(

        new ActionListener() {
            // event handler called when interestAccountJButton
            // is clicked
            public void actionPerformed(ActionEvent event) {
                interestJButtonActionPerformed(event);
            }

        }

        ); // end call to addActionListener

        // set up DeleteAccountButton
        DeleteAccountJButton = new JButton();
        DeleteAccountJButton.setBounds(16, 152, 80, 24);
        DeleteAccountJButton.setText("Delete");
        inputDetailJPanel.add(DeleteAccountJButton);
        DeleteAccountJButton.addActionListener(

        new ActionListener() // anonymous inner class
                {
                    // event handler called when DeleteAccountJButton
                    // is clicked
                    public void actionPerformed(ActionEvent event) {
                    DeleteAccountJButtonActionPerformed(event);

                    }

                }

                ); // end call to addActionListener





         // set up updateJButton
        updateJButton = new JButton();
        updateJButton.setBounds(16, 180, 176, 24);
        updateJButton.setText("update Account ");
        inputDetailJPanel.add(updateJButton);
        updateJButton.addActionListener(

        new ActionListener() // anonymous inner class
                {
                    // event handler called when TransactionJButton
                    // is clicked
                    public void actionPerformed(ActionEvent event) {
                        updateJButtonActionPerformed(event);
                    }

                } // end anonymous inner class

                );

        // clear Button          
        clearJButton = new JButton();
        clearJButton.setBounds(18, 210, 176, 24);
        clearJButton.setText("clear");
        inputDetailJPanel.add(clearJButton);
        clearJButton.addActionListener(

        new ActionListener() // anonymous inner class
                {
                    // event handler called when clearJButton
                    // is clicked
                    public void actionPerformed(ActionEvent event) {
                        clearJButtonListenerActionPerformed(event);
                    }

                } // end anonymous inner class

                );




        // set up displayJLabel
        displayJLabel = new JLabel();
        displayJLabel.setBounds(250, 26, 190, 33);
        displayJLabel.setText("Account Details:");
        contentPane.add(displayJLabel);

        // set up displayJTextArea
        displayJTextArea = new JTextArea();
        displayJTextArea.setBounds(260, 68, 422, 205);
        displayJTextArea.setEditable(false);
        contentPane.add(displayJTextArea);

        // set properties of application's window
        setTitle("Bank Accounts"); // set title bar string
        setSize(750, 450); // set window size
        setVisible(true); // display window

    } // end method createUserInterface



    private void CreateAccountJButtonActionPerformed(ActionEvent event) {


              //invrement number in array 
               i++ ;



                balance = Double.parseDouble(balanceJTextField.getText());
                custid = custidJTextField.getText();
                custid = custid.trim();
                acctDateOpened = acctDateOpenedJTextField.getText();
                acctDateOpened = acctDateOpened.trim();
                acctNumber = acctNumberJTextField.getText();
                acctNumber = acctNumber.trim(); 


        // Check to see if each account array is populated and
        // if not add account details
         if (balance == 0  )
                {
                    JOptionPane.showMessageDialog (null, "you initial balance cannot be zero ");
                    valid1  = false ; 
                }
                else valid1 = true ; 



                    if (balance < 0 )
                {
                    valid2 = false;
                    JOptionPane.showMessageDialog (null, "Please enter a number for account balance!");
                }
                else valid2 = true;


                DateTest test = new DateTest();    
                if (acctDateOpened.length() == 0 || test.isValidDate(acctDateOpened)== false)
                {
                    JOptionPane.showMessageDialog (null, "Enter Account date in correct dd/mm/yyyy format");
                    valid3 = false;
                }
                else
                    valid3 = true;   

                if (custid.length() != 7)
                {
                    JOptionPane.showMessageDialog (null, "Please enter apositive 7 digit id number");
                    valid4 = false;
                }
                else
                    valid4 = true; 

                    //validate customer id number

                for (int i = 0; i < custid.length(); i++) 
                {
                     if (!Character.isDigit(custid.charAt(i)))
                {
                    valid5 = false;
                    JOptionPane.showMessageDialog (null, "Please enter a positive 7 digit id number");
                }
                else valid5 = true;
            }


                 if (acctNumber.length() == 0)
                {
                    JOptionPane.showMessageDialog (null, "Please enter an account number");
                    valid6 = false;
                }
                else
                    valid6 = true;

                // validate account number is a number
                for (int i = 0; i < acctNumber.length(); i++) 
                {
                     if (!Character.isDigit(acctNumber.charAt(i)))
                {
                    valid7 = false;
                    JOptionPane.showMessageDialog (null, "Please enter a number for account number");
                }
                else valid7 = true;

            }





                if (valid1 == true && valid2 == true && valid3== true && valid4 ==true && valid5 == true && valid6 == true && valid7 == true  )
                {


             try {



             FileWriter fw = new FileWriter (file, true);
             BufferedWriter bw = new BufferedWriter (fw);
             PrintWriter outFile = new PrintWriter (bw);
             account[i]   = new Account ( acctNumber ,  balance , acctDateOpened , custid ) ; 
             outFile.println ("account number  " + account[i].getacctNumber() );
             outFile.println ("balance " + account[i].getBalance ()  );
             outFile.println ("date opened " + account[i].getacctDate () );
             outFile.println ("customer ID " + account[i].getcustid () );
             outFile.println();
             outFile.close();
             JOptionPane.showMessageDialog (null, "Account has been saved at: " + file);





             }

        catch (Exception e) {

            JOptionPane.showMessageDialog(null, "Error: File not found");
        }  


             displayJTextArea.setText( "Account Number  \tcustomerID \tDate Of Opening \tbalance" +  " \n " + account[i] +  " " + account[i].getacctNumber() + "    " + account[i].getBalance ()  + "     " + account[i].getacctDate () +  "    " + account[i].getcustid () );   



    }

}


     private void clearJButtonListenerActionPerformed(ActionEvent  event)
        {



                custidJTextField.setText("  ");

                acctDateOpenedJTextField.setText(" ");

                acctNumberJTextField.setText(" ");

                balanceJTextField.setText(" ");

        }

         private void interestJButtonActionPerformed(ActionEvent  event)
        {



            double  add  =  Double.parseDouble ( JOptionPane.showInputDialog ("enter percentage interest") ) ;
            for (int i = 0; i < account.length; i++) 
                { 

               double  B = account[i].getBalance();

               double amount  = (add/100) * B; 
               balance  += amount ;

              }


            }




    private void DeleteAccountJButtonActionPerformed(ActionEvent event) {


        String D =  JOptionPane.showInputDialog (null, "Please enter the account number to delete the account");

        for ( int c = 0 ; c < account.length; c++ ) 
        {  
            if(  D.equals( account[i].getacctNumber()) )
              { 
                  JOptionPane.showMessageDialog (null , "FOUND");
                  account[i] = null ; 
                } else 
                   { JOptionPane.showMessageDialog (null, "NOT FOUND ");
                    } 

            }  




        } 







    private void updateJButtonActionPerformed(ActionEvent event) {  





    }



    public static void main(String[] args) {
        // Populate arrays with the word EMPTY
        // so we can check to see if the values are empty later



        BankAccount application = new BankAccount();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

Recommended Answers

All 9 Replies

You provide no information about testDate and Acount

You provide no information about testDate and Acount

/////////////////////////account class\\\\\\\\\\\\\\\\\\\\\\\\\\
public class Account {
  // Instance variable 
  private double balance;

  String acctNumber ; 

   String dateOpened;
   String custID ; 
   int amountInArray;

  // Constructor 1 (for initial deposit accounts)
  public Account(String acctNum , double bal , String date , String custid) {
           acctNumber = acctNum;
           balance = bal ; 
           date = dateOpened ; 
           custID = custid ; 
  }

  // Constructor 2 (for initially empty accounts)
  public Account() {
    balance = 0.0;
  }

  // Instance methods:

  //deposit to account
  public void deposit(double amount) {
    balance += amount;
  }

  //withdraw from account
  public void withdraw(double amount) {
    balance -= amount;
  }

 //get the account number
 public String getacctNumber() {
    return acctNumber;
  }


   //get the account number
 public String getcustid() {
    return custID;
  }

  //get account balance
  public double getBalance() {
    return balance;
  }

    public String getacctDate() {
    return dateOpened;
  }

  //close account
  public void close() {
    balance = 0.0;
  }
}

////////////////////////////TestDate class\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

import java.text.SimpleDateFormat;
import java.text.ParseException;

public class DateTest {

  public boolean isValidDate(String inDate) {

    if (inDate == null)
      return false;

    //set the format to use as a constructor argument
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

    if (inDate.trim().length() != dateFormat.toPattern().length())
      return false;

    dateFormat.setLenient(false);

    try {
      //parse the inDate parameter
      dateFormat.parse(inDate.trim());
    }
    catch (ParseException pe) {
      return false;
    }
    return true;


  }


}

The exception you got is not about the file but in fact it's an ArrayIndexOutOfBoundsException; but the message you show is your's.
Try to depect the incremetation of th i value.
Hope it helps.

You increse the value of i without incresing the size of the account array.
That is the error.

You increse the value of i without incresing the size of the account array.
That is the error.

I thought by increasing "i" i was increasing the size of the array , if its not too much trouble could u correct the error and copy and past it and show me please ?

Add those instructions after increasing i and let me now!

Account [] newaccount = new Account [i] ;
System.arraycopy(account, 0, newaccount, 0, account.length);
account=newaccount;

System.arraycopy(account, 0, newaccount, 0, account.length);

thats gives an error

: <identifier> expected

and when if i put it after the i++ in the create listner it says variable account not found

The whole function should look like:

private void CreateAccountJButtonActionPerformed(ActionEvent event) {


//invrement number in array
i++ ;
Account [] newaccount = new Account [i] ;
System.arraycopy(account, 0, newaccount, 0, account.length);
account=newaccount;

balance = Double.parseDouble(balanceJTextField.getText());
custid = custidJTextField.getText();
custid = custid.trim();
acctDateOpened = acctDateOpenedJTextField.getText();
acctDateOpened = acctDateOpened.trim();
acctNumber = acctNumberJTextField.getText();
acctNumber = acctNumber.trim();


// Check to see if each account array is populated and
// if not add account details
if (balance == 0 )
{
JOptionPane.showMessageDialog (null, "you initial balance cannot be zero ");
valid1 = false ;
}
else valid1 = true ;



if (balance < 0 )
{
valid2 = false;
JOptionPane.showMessageDialog (null, "Please enter a number for account balance!");
}
else valid2 = true;


DateTest test = new DateTest();
if (acctDateOpened.length() == 0 || test.isValidDate(acctDateOpened)== false)
{
JOptionPane.showMessageDialog (null, "Enter Account date in correct dd/mm/yyyy format");
valid3 = false;
}
else
valid3 = true;

if (custid.length() != 7)
{
JOptionPane.showMessageDialog (null, "Please enter apositive 7 digit id number");
valid4 = false;
}
else
valid4 = true;

//validate customer id number

for (int i = 0; i < custid.length(); i++)
{
if (!Character.isDigit(custid.charAt(i)))
{
valid5 = false;
JOptionPane.showMessageDialog (null, "Please enter a positive 7 digit id number");
}
else valid5 = true;
}


if (acctNumber.length() == 0)
{
JOptionPane.showMessageDialog (null, "Please enter an account number");
valid6 = false;
}
else
valid6 = true;

// validate account number is a number
for (int i = 0; i < acctNumber.length(); i++)
{
if (!Character.isDigit(acctNumber.charAt(i)))
{
valid7 = false;
JOptionPane.showMessageDialog (null, "Please enter a number for account number");
}
else valid7 = true;

}





if (valid1 == true && valid2 == true && valid3== true && valid4 ==true && valid5 == true && valid6 == true && valid7 == true )
{


try {



FileWriter fw = new FileWriter (file, true);
BufferedWriter bw = new BufferedWriter (fw);
PrintWriter outFile = new PrintWriter (bw);
account[i-1] = new Account ( acctNumber , balance , acctDateOpened , custid ) ;
outFile.println ("account number " + account[i-1].getacctNumber() );
outFile.println ("balance " + account[i-1].getBalance () );
outFile.println ("date opened " + account[i-1].getacctDate () );
outFile.println ("customer ID " + account[i-1].getcustid () );
outFile.println();
outFile.close();
JOptionPane.showMessageDialog (null, "Account has been saved at: " + file);





}

catch (Exception e) {

JOptionPane.showMessageDialog(null, "Error: File not found");
}

//Account acc =account[i-1];
displayJTextArea.setText( "Account Number \tcustomerID \tDate Of Opening \tbalance" + " \n " + account[i-1] + " " + account[i-1].getacctNumber() + " " + account[i-1].getBalance () + " " + account[i-1].getacctDate () + " " + account[i-1].getcustid () );



}

}

this is all the change i done.

The whole function should look like:

private void CreateAccountJButtonActionPerformed(ActionEvent event) {


//invrement number in array
i++ ;
Account [] newaccount = new Account [i] ;
System.arraycopy(account, 0, newaccount, 0, account.length);
account=newaccount;

balance = Double.parseDouble(balanceJTextField.getText());
custid = custidJTextField.getText();
custid = custid.trim();
acctDateOpened = acctDateOpenedJTextField.getText();
acctDateOpened = acctDateOpened.trim();
acctNumber = acctNumberJTextField.getText();
acctNumber = acctNumber.trim();


// Check to see if each account array is populated and
// if not add account details
if (balance == 0 )
{
JOptionPane.showMessageDialog (null, "you initial balance cannot be zero ");
valid1 = false ;
}
else valid1 = true ;



if (balance < 0 )
{
valid2 = false;
JOptionPane.showMessageDialog (null, "Please enter a number for account balance!");
}
else valid2 = true;


DateTest test = new DateTest();
if (acctDateOpened.length() == 0 || test.isValidDate(acctDateOpened)== false)
{
JOptionPane.showMessageDialog (null, "Enter Account date in correct dd/mm/yyyy format");
valid3 = false;
}
else
valid3 = true;

if (custid.length() != 7)
{
JOptionPane.showMessageDialog (null, "Please enter apositive 7 digit id number");
valid4 = false;
}
else
valid4 = true;

//validate customer id number

for (int i = 0; i < custid.length(); i++)
{
if (!Character.isDigit(custid.charAt(i)))
{
valid5 = false;
JOptionPane.showMessageDialog (null, "Please enter a positive 7 digit id number");
}
else valid5 = true;
}


if (acctNumber.length() == 0)
{
JOptionPane.showMessageDialog (null, "Please enter an account number");
valid6 = false;
}
else
valid6 = true;

// validate account number is a number
for (int i = 0; i < acctNumber.length(); i++)
{
if (!Character.isDigit(acctNumber.charAt(i)))
{
valid7 = false;
JOptionPane.showMessageDialog (null, "Please enter a number for account number");
}
else valid7 = true;

}





if (valid1 == true && valid2 == true && valid3== true && valid4 ==true && valid5 == true && valid6 == true && valid7 == true )
{


try {



FileWriter fw = new FileWriter (file, true);
BufferedWriter bw = new BufferedWriter (fw);
PrintWriter outFile = new PrintWriter (bw);
account[i-1] = new Account ( acctNumber , balance , acctDateOpened , custid ) ;
outFile.println ("account number " + account[i-1].getacctNumber() );
outFile.println ("balance " + account[i-1].getBalance () );
outFile.println ("date opened " + account[i-1].getacctDate () );
outFile.println ("customer ID " + account[i-1].getcustid () );
outFile.println();
outFile.close();
JOptionPane.showMessageDialog (null, "Account has been saved at: " + file);





}

catch (Exception e) {

JOptionPane.showMessageDialog(null, "Error: File not found");
}

//Account acc =account[i-1];
displayJTextArea.setText( "Account Number \tcustomerID \tDate Of Opening \tbalance" + " \n " + account[i-1] + " " + account[i-1].getacctNumber() + " " + account[i-1].getBalance () + " " + account[i-1].getacctDate () + " " + account[i-1].getcustid () );



}

}

this is all the change i done.

it works man u helped me so much!!! i wish i could make it up to u thanks!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.