Results 1 to 2 of 2
  1. #1
    a3404987 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default Need help with bank system assignment - vectors

    Hello all,

    I've got an assignment due in three days time and I'm stuck at the point where I need to make use of vectors (I specifically HAVE to use them) for storing multiple clients' bank account details for a banking system. As of now I am able to create an instance of any one of three types of accounts and perform actions/methods that include the withdrawing/depositing of funds etc.

    However I'm now at the point where I need to allow multiple clients to create and access their information - regardless of the type of account at any time following its creation (current/savings/business).

    A quick explanation of how my program currently runs:

    When the program is run via the "main method" (start class), an instance of a GUI is created. From here action listeners respond to button options being clicked. In the case of "create account", following a series of questions an instance of a specific account is created passing certain values such as the starting balance to the class of the account type. A textfield then displays the action that has been performed (e.g. "Jonathan has created a CURRENT account with the balance of 500" or "Jonathan has withdrawn 500 from his account, leaving 0.").



    From here I do not know how to store the variables linked with the creation of an instance of the accounts, for them to be recalled later on and used for making transactions. What I had in mind was some sort of unique pin number that would be used to sort through the vector(s) in order to return all the values that are stored in the same position of other vectors (so a unique vector for each account type, with further unique vectors for each form of data - name and balance for example).

    A "check account" button would ask the user to enter the type of account they own followed by their pin before the information is obtained.


    Here is my code thus far:

    --------
    START
    ---------
    Java Code:
    import javax.swing.*;
    
    public class start
    {
        public static void main(String[] args)
        {
            // Welcome message. This is the first thing that they will see when they run the program.
            JOptionPane.showMessageDialog(null, "Welcome to my Mini Bank Simulator.");
        
            bankGUI initiate = new bankGUI(); 
            initiate.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // enables the user to close the window (when X is pressed)
            initiate.setSize(800, 480); // setting the width, height of the window
            initiate.setVisible(true);  // enabling the window to be visible to the user
            
        }
        
        
    } // END class start


    --------
    BANK GUI
    ---------
    Java Code:
    import java.awt.FlowLayout; // arranges components from left-to-right and top-to-bottom
    import java.awt.event.*; // for event handling (i.e. action and listener events)
    import java.awt.*; // for textarea
    import java.util.*; // for vector use
    import javax.swing.*;
    
    public class bankGUI extends JFrame // enables the use of JFrame (e.g. a window)
    {
        private JButton addAccount;                
        private JButton checkAccount;
        private JButton close;
        private TextArea showResult = new TextArea(25,75);
        private String stringOutput = "";
        
        acc accAdd; // polymorphism
        
        // the constructor - everything within will be used for the creation of the window
        public bankGUI()
        {   
            super("mr x's Bank Simulator"); // creation of the title for the window
            
            setLayout(new FlowLayout()); // setting the layout - how things are placed within the window
            
            addAccount = new JButton("Add Account");
            add(addAccount); // creation and addition of buttons & tank background to the window
            
            checkAccount = new JButton("Check Account");
            add(checkAccount);
            
            close = new JButton("CLOSE");
            add(close);
            
            showResult.setEditable(false);
            add(showResult);        
    
            // creation of an object of an action listener
            thehandler handler = new thehandler(); //CHANGE THIS?
            addAccount.addActionListener(handler); 
            checkAccount.addActionListener(handler);
            close.addActionListener(handler);
    
        }
    
    
        public class thehandler implements ActionListener // the class to handle the events. This calls on a method when a specific action occurs 
        {
            
            
            public void actionPerformed(ActionEvent event)
            {
                if (event.getSource()==addAccount) 
                { 
                    String name = JOptionPane.showInputDialog("Please enter your name:");
                    String accCreate = JOptionPane.showInputDialog("What type of account would you like to create? (CURRENT / SAVINGS / BUSINESS)");
                    
                    
                    if (accCreate.equalsIgnoreCase("CURRENT"))
                    {
                       try
                       {
                           String pinNoAsk = JOptionPane.showInputDialog("Please enter a pin for your account:");
                           int pinNo = Integer.parseInt(pinNoAsk);
                           String depositAsk = JOptionPane.showInputDialog("Please enter amount to deposit into account:");
                           double depositAdd = Double.parseDouble(depositAsk); 
                           
                           
                           currentAcc(accCreate, pinNo, name, depositAdd);
                       
                           
                           stringOutput = stringOutput + " "+name + " has created a CURRENT account with a balance of £"+depositAdd+".\n";
                           showResult.setText(stringOutput);
                        }
                        
                        catch (NumberFormatException invalidValue)
                        {
                            JOptionPane.showMessageDialog(null, "You must enter a numerical value in order to deposit funds");
                        }
                       //System.out.println(one.getBalance());
                       //System.out.println(((accCurrent)one).interestBal());
                       // ((accCurrent)one).interestBal();
                    }
                    
     
                    else if (accCreate.equalsIgnoreCase("SAVINGS"))
                    {
                        try
                       {
                           String pinNoAsk = JOptionPane.showInputDialog("Please enter a pin for your account:");
                           int pinNo = Integer.parseInt(pinNoAsk);
                           String depositAsk = JOptionPane.showInputDialog("Please enter amount to deposit into account:");
                           double depositAdd = Double.parseDouble(depositAsk); 
                           
                           accountDetails.storeDetails(accCreate, name, pinNo, depositAdd);
                           
                           stringOutput = stringOutput + " "+name + " has created a SAVINGS account with a balance of £"+depositAdd+".\n";
                           showResult.setText(stringOutput);
                        }
    
                        catch (NumberFormatException invalidValue)
                        {
                            JOptionPane.showMessageDialog(null, "You must enter a numerical value in order to deposit funds");
                        }                    
                        
                    }
                    
                    
                    else if (accCreate.equalsIgnoreCase("BUSINESS"))
                    {
                        try
                        {
                           String pinNoAsk = JOptionPane.showInputDialog("Please enter a pin for your account:");
                           int pinNo = Integer.parseInt(pinNoAsk);
                           String depositAsk = JOptionPane.showInputDialog("Please enter amount to deposit into account:");
                           double depositAdd = Double.parseDouble(depositAsk); 
                    
                           
                           
                           stringOutput = stringOutput + " "+name + " has created a BUSINESS account with a balance of £"+depositAdd+".\n";
                           showResult.setText(stringOutput);
                        }
                        
                        catch (NumberFormatException invalidValue)
                        {
                            JOptionPane.showMessageDialog(null, "You must enter a numerical value in order to deposit funds");
                        }     
                        
                    }
                    
                    else
                    {
                        JOptionPane.showMessageDialog(null, "The only account types available for creation are 'CURRENT', 'SAVINGS' or 'BUSINESS'.");
                    }
                    
                    
                }  
              ////////////////////////ERRORS LIKELY TO OCCUR FROM HERE (IT SHOULD COMPILE BUT I REALISE THAT I MAY BE DOING THE WRONG THING)
    
    
                else if (event.getSource()==checkAccount) 
                { 	// HERE I WOULD ASK THE USER TO ENTER THE TYPE OF ACCOUNT TO CHECK, FOLLOWED BY THEIR PIN. IF THERE IS A MATCH THE NEXT MESSAGE WOULD BE PRINTED, WITH THE ACTIONS SELECTED USING THE VALUES (E.G. BALANCE) BEFORE RETURNING THE VALUES TO BE STORED AGAIN FOR FUTURE USE - AS IF THE USER WAS USING AN ATM MACHINE.
                    String checkAsk = JOptionPane.showInputDialog("Which action would you like to perform?\n 1] View Balance\n 2] Withdraw from Account\n 3] Deposit into Account\n 4] View current balance w/ interest\n 5] EXIT"); // add option in future to close account???                           
                    int checkAns = Integer.parseInt(checkAsk);
                    
                    if (checkAns == 1) // View Balance
                    {
                        System.out.println(accAdd.getBalance());
       //                 stringOutput = stringOutput + accAdd.getBalance()+"\n";
         //               showResult.setText(stringOutput);
                    }
                    
                    else if (checkAns == 2) // Withdraw from Account
                    {
                        String wdAsk = JOptionPane.showInputDialog("How much would you like to withdraw?");
                        double wdAns = Double.parseDouble(wdAsk);
                        
                        stringOutput = stringOutput + accAdd.withdraw(wdAns)+"\n";
                        showResult.setText(stringOutput);
                    }
                    
                    else if (checkAns == 3) // Deposit into Account
                    {
                        String wdAsk = JOptionPane.showInputDialog("How much would you like to deposit?");
                        double wdAns = Double.parseDouble(wdAsk);
                        
                        stringOutput = stringOutput + accAdd.deposit(wdAns)+"\n";
                        showResult.setText(stringOutput);
                    }
                    
                    else if (checkAns == 4) // View current w/ interest balance
                    {                                  
                        stringOutput = stringOutput + ((accCurrent)accAdd).interestBal()+"\n";
                        showResult.setText(stringOutput);
                        
                    }
                    
                    else if (checkAns == 5) // EXIT
                    {
                        JOptionPane.showMessageDialog(null, "Thank you for using our banking system.");
                    }
                    
                    else 
                    {
                        JOptionPane.showMessageDialog(null, "You must enter a value between 1 and 4.");
                    }
    
                    
                }
                // THIS IS FINE
                     
                else if (event.getSource()==close) 
                {                   
                    int closeAsk = JOptionPane.showConfirmDialog(null, "Quit program?", "CLOSE", JOptionPane.YES_NO_OPTION);
                    
                    if(closeAsk == JOptionPane.YES_OPTION)
                    {
                        JOptionPane.showMessageDialog(null, "Thank you for using this program.");
                        System.exit(0);
                    }
                    
    
                    
                }       
                
                
            }
        }
        
        
    }

    --------
    acc (ACCOUNT)
    ---------
    Java Code:
    public class acc
    {
        protected String name;
        protected double balance = 0;
           
        public acc(String n, double depAcc)
        {
            name = n;
            balance = depAcc;
        }
        
        public double getBalance() 
        {
            return balance;
        }
        
        public double withdraw(double wdAcc) 
        {
            balance -= wdAcc;
            return balance;
        }
        
        public double deposit(double depAcc) 
        {
             balance += depAcc;
             return balance;
        } 
    
    }


    --------
    accCURRENT (CURRENT ACCOUNT - subclass of acc)
    ---------
    Java Code:
    public class accCurrent extends acc
    {
        private double interestRate = 5.2;
        private double finalBal;
        
        // constructor - cus it has same name as class & is run everytime an object is created
        public accCurrent(String n, double depAcc)
        {
           super(n, depAcc);
           finalBal = depAcc;
        }  
        
        // calculating the interest rate balance
        public double interestBal ()
        {
            double intApplyC = (finalBal / 100) * interestRate;  
            double intApplyF = finalBal + intApplyC;
            
            return intApplyF; 
        }
        
    }


    --------
    accSAVINGS (SAVINGS ACCOUNT - subclass of acc)
    ---------
    Java Code:
    public class accSavings extends acc
    {
        private double interestRate = 9;
        private double finalBal;
        
        // constructor - cus it has same name as class & is run everytime an object is created
        public accSavings(String n, double depAcc)
        {
           super(n, depAcc);
           finalBal = depAcc;
        }  
        
        // calculating the interest rate balance
        public double interestBal (double finalBal, double interestRate)
        {
            double intApplyC = (finalBal / 100) * interestRate;  
            double intApplyF = finalBal + intApplyC;
            
            return intApplyF; 
        }
        
    }


    --------
    accBUSINESS (BUSINESS ACCOUNT - subclass of acc)
    ---------
    Java Code:
    public class accBusiness extends acc
    {
        private double interestRate = 12.6;
        private double finalBal;
        
        // constructor - cus it has same name as class & is run everytime an object is created
        public accBusiness(String n, double depAcc)
        {
           super(n, depAcc);
           finalBal = depAcc;
        }  
        
        // calculating the interest rate balance
        public double interestBal (double finalBal, double interestRate)
        {
            double intApplyC = (finalBal / 100) * interestRate;  
            double intApplyF = finalBal + intApplyC;
            
            return intApplyF; 
        }
        
    }
    I've attempted to store the values in a new class, where the vectors would be placed ("accountDetails" as you can see in the pic with the red cross), but ended up getting errors including one along the lines of "calling non-static method in static method in Java".

    If of any interest I can post the code but it's not currently in use:

    _-------------------
    accountDetails
    ------------
    Java Code:
    import java.util.*;
    
    public class accountDetails extends acc
    {
       Vector currentIDNo = new Vector(); // make private???
       Vector curentNames = new Vector();
       Vector currentBalance = new Vector();
       
       
       Vector savingsIDNo = new Vector();
       Vector savingsNames = new Vector();
       Vector savingsBalance = new Vector();
        
       Vector businessIDNo = new Vector();
       Vector businessNames = new Vector();
       Vector businessBalance = new Vector();
        
        
        
        public accountDetails(String accType, int pin, String nameGet, double balance)
        {
            if (accType.equalsIgnoreCase("CURRENT"))
            {
                currentIDNo.add(new Integer(pin));
                curentNames.add(new String(nameGet));
                currentBalance.add(new Double(balance));            
            }
            
            else if (accType.equalsIgnoreCase("SAVINGS"))
            {
                savingsIDNo.add(new Integer(pin));
                savingsNames.add(new String(nameGet));
                savingsBalance.add(new Double(balance));  
            }
            
            else if (accType.equalsIgnoreCase("BUSINESS"))
            {
                savingsIDNo.add(new Integer(pin));
                savingsNames.add(new String(nameGet));
                savingsBalance.add(new Double(balance));  
            }
            
            // String report = nameGet +" has created a "+accType+" account with a balance of "+balance+".\n";
        }
    
    }
    So to summarize, how do I store the values created in the instances of an account into a vector, and how do I call on said data for modification?

    Apologies for the long post but I wish to provide as much info as possible due to the deadline date approaching and the difference in time zones (it's 3am so I may not be able to respond for a few hours). Thank you!
    Last edited by a3404987; 03-29-2011 at 04:06 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You only need one Vector that can store Account objects. By the way java conventions mean that class names should begin with an uppercase letter. Also, typing a few extra characters will not kill you: Account is a more meaningful class name than acc.

Similar Threads

  1. Bank Account
    By HPcompaq256 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 10:05 PM
  2. Replies: 4
    Last Post: 11-02-2009, 04:41 PM
  3. Java Question Bank
    By mgm2010 in forum New To Java
    Replies: 2
    Last Post: 07-31-2009, 07:45 PM
  4. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 03:57 PM
  5. Code bank
    By Eranga in forum Suggestions & Feedback
    Replies: 2
    Last Post: 01-01-2008, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •