Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Creating a transfer function for a ATM simulation

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating a transfer function for a ATM simulation

    Hi guys, im fairly new to java so im not sure on what to do here. What i want to be able to do is be able to transfer say $100 from this account to another account.I have attached the code below.Any help would be greatly appreciated. =).

    [ATMDataFile]
    import java.io.*;
    import java.util.Scanner;
     
    public class ATMDataFile {
      public static String datafilename = "ATMData.txt";
      public static int lines = 0; // Number of lines in ATMdata.txt
      public static int lineNo; // Line number of the opperative account
      // Create a File object called "datafile" to see if "ATMdata.txt" exists
      static File datafile = new File(datafilename);
      // Declare a 2 dimensional array to hold account details
      // First three values are only used if no datafile exists.
      static String[][] account_inf = {{"John","Savings","12345","4444","100"},
                                          {"Jack","Manager","54321","0000","100"},
                                          {null,null,null},
                                          {null,null,null},
                                          {null,null,null}};  
     
      public ATMDataFile() throws IOException { 
        // If ATMdata.txt exists run loadData method                 
        if (datafile.exists()) {
          loadData();
          // Debug code
          //writeFile();
          // If no datfile exists run the writeNewFile method
        } else {
             writeNewFile();
        }
      }
     
      // method for loading ATMdata.txt to a 2 dimensional array
      static void loadData()throws IOException {
        Scanner scan = null;
        BufferedReader read = null;
        try {
          // Create Scanner (reads text files one space delimeted element at a time)
          // and BufferedReader (reads contents of a text file into memory) objects 
          scan = new Scanner(new BufferedReader(new FileReader(datafile.getName())));
          read = new BufferedReader(new FileReader(datafile.getName()));
          // Count the number of lines in ATMdata.txt
          String l;
          while ((l = read.readLine()) != null) {
              lines ++ ;
          }
          // Debug code
          System.out.println ("Lines =" +lines);
          // Populate the 2 dimensional array account_inf with contents of 
          // ATMdata.txt. account_inf[0][0] is 1st account number
          //              account_inf[0][1] is 1st account pasword
          //              account_inf[0][2] is 1st account balance  
          //              account_inf[1][0] is 2nd account number etc...
          for(int i=0; i<=lines-1; i++){
            for(int j=0; j<=4; j++){
              account_inf[i][j] = scan.next();
              // Debug code
              System.out.print(" [" +i +"][" +j +"]=" 
                                  +account_inf[i][j]);
            }
            // Debug code      
            System.out.println();
          }                
        } catch (IOException e) {
          System.err.println("Caught IOException: " 
                                  +  e.getMessage());
          } finally {
            if (scan != null) {
                scan.close();
            }
          }
        }
     
      static void writeNewFile()throws IOException {
        String name = account_inf[0][0];
        String account_typ = account_inf[0][1];
        String account_no = account_inf[0][2];
        String password = account_inf[0][3];
        String balance = account_inf[0][4];
        PrintWriter out = null;
        try {
          // Create an output stream to write to ATMdata.txt
          out = new PrintWriter(datafile.getName());
          // Write the first three values of the two dimensional array
          // to the ATMdata.txt file. 
         out.format ("%s %s %s %s %s", account_inf[0][0], account_inf[0][1], account_inf[0][2], account_inf[0][3], account_inf[0][4]);
        } finally { 
          if (out != null) {  
            out.close();
          }
        }
      }   
     
      static void writeFile()throws IOException {
        PrintWriter out = null;
        try {
          // Create an output stream to write to ATMdata.txt
          out = new PrintWriter(datafile.getName());
          // Write account_inf[][] to disk 
          for (int i = 0; i<=lines-1; i++) {
            out.format ("%s %s %s %s %s", account_inf[i][0], 
                                    account_inf[i][1], 
                                    account_inf[i][2],
                                    account_inf[i][3],
                                    account_inf[i][4]);
     
            out.println();
          }                         
        } finally { 
          if (out != null) {  
            out.close();
          }
        }
      }  
     
      static String getPin(String account_id){
        String pin = "";
        for (int i = 0; i <= lines ; i++) {
          if (account_id.equals(account_inf[i][2])) {
          	pin = account_inf[i][3];
          	lineNo = i;
          	// Debug code
            System.out.println("pinString="+pin);
            break; 
          }
        }    
     
        return pin;    
      }
     
       static String doDeposits(String amount){ 
        // Deposit cash -- called from ATMDeposits
        String newBalance = "";
        // Turn Strings to doubles for arithmatic
        double depositsAmount=                 
            (new Double(amount)).doubleValue();
        double currentBalance= 
            (new Double(account_inf[lineNo][4])).doubleValue(); 
        // Get the new balance as a string & update the account_inf array    
          newBalance = Double.toString
                        (currentBalance + depositsAmount);
          account_inf[lineNo][4] = newBalance;    
     
        return newBalance;    
      }
     
     
     
      static String doWithdrawl(String amount){ 
        // Withdraw cash -- called from ATMWithdrawl
        String newBalance = "";
        // Turn Strings to doubles for arithmatic
        double withdrawlAmount=                 
            (new Double(amount)).doubleValue();
        double currentBalance= 
            (new Double(account_inf[lineNo][4])).doubleValue(); 
        // Get the new balance as a string & update the account_inf array    
          newBalance = Double.toString
                        (currentBalance - withdrawlAmount);
          account_inf[lineNo][4] = newBalance;    
     
        return newBalance;    
      }
     
      static String doTransfer(String amount){ 
        // Withdraw cash -- called from ATMWithdrawl
        String newBalance = "";
        // Turn Strings to doubles for arithmatic
        double transferAmount=                 
            (new Double(amount)).doubleValue();
        double currentBalance= 
            (new Double(account_inf[lineNo][4])).doubleValue(); 
        // Get the new balance as a string & update the account_inf array    
          newBalance = Double.toString
                        (currentBalance - transferAmount);
          account_inf[lineNo][4] = newBalance;    
     
        return newBalance;    
      }
     
      static String getBalance() {
        return account_inf[lineNo][4]; 
      }

    [ATMTransfer]//
    Import GUI, IO and math libraries
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.math.*;
     
    // Class declaration establishing ATMTransfer as a subtype of 
    //  JPanel (a content pane to be displayed inside a JFrame)
    //  elements of a graphic user interface (GUI)
    //  impliments ActionListener statement indicates that it uses 
    //  the Java actionlistener class
    public class ATMTransfer extends JPanel
                          implements ActionListener {
    // Declare string variables for use in GUI
        private static String AU20 = "20";
        private static String AU50 = "50";
        private static String AU100 = "100";
        private static String AU200 = "200";
        private static String AU300 = "300";
        private static String OTHER = "other";
        private static String AMOUNT = "amount";
        private static String ACCOUNT_INF = "account_inf";
    // Declare JButton variables for use in GUI    
        private static JButton au20Button, au50Button, au100Button, 
                                au200Button,au300Button, otherButton;
    // Declare object variables for use in GUI
        private JFrame controllingFrame; //needed for dialogs
        protected JTextField amountTextField;
        protected JTextField account_infTextField;
        protected JLabel amountLabel;
        protected JLabel account_infLabel;
     
        private ATMDataFile atmData;
        private ATM atm;
     
    // Constructor method - called from ATM with ATMframe as the argument f
    // an ATMTransferl object is created as a subtype of a JPanel
        public ATMTransfer(JFrame f) {
            controllingFrame = f;
            JComponent titlePane = createTitlePane();
            add(titlePane);     // add the titlepane to the ATMTransfer JPanel
      // Call the createButtonPanel method to populate the button pane        
            JComponent buttonPane = createButtonPanel();
            add(buttonPane);
      // Call the createBotTextPane method to populate the text box pane pane
            JComponent botTextPane = createBotTextPane();
            add(botTextPane);
            add(botTextPane);
        }
     
    // "actionPerformed" method - reads the action command "cmd"
    //    set by the button and text field components and selected 
    //    by the users mouse click or text entry
    //    and selects the action to perform
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand(); //gets "cmd" if button clicked
            String input = amountTextField.getText(); //gets "input" from text entry 
            if (OTHER.equals(cmd)){             // Other amount button pressed.
              amountTextField.setEnabled(true);
              amountTextField.setEnabled(true);// enable text entry
              amountLabel.setEnabled(true); 
              account_infTextField.setEnabled(true);
              account_infLabel.setEnabled(true); 
              au20Button.setEnabled(false);     // disable (grey out) other buttons
              au50Button.setEnabled(false);
              au100Button.setEnabled(false);
              au200Button.setEnabled(false);
              au300Button.setEnabled(false);
              otherButton.setEnabled(false);
              amountTextField.requestFocusInWindow();  //set focus to text entry
            } else if (AMOUNT.equals(cmd)) {           // User enters other amount.
                //Test if the entered ammount is a multiple of $20
                // by using the "mod" opperation to divide by 20 and find the remainder
                BigInteger remainder = (new BigInteger(input)).mod 
                                        (new BigInteger("20"));
                // Test if remainder is "0"
                if (remainder.equals((new BigInteger("0")))) {    // Yes it's a multiple
                  JOptionPane.showMessageDialog(controllingFrame, // pop up a success message
                        "$"+input +" is a multiple of $20\n"
                        +"Well done!!");
                  // Invoke the "tryTransfers" method 
                  //   with the entered ammount as the argument      
                  tryTransfers(input);  
     
                } else {                                         // Not a multiple of $20
                    JOptionPane.showMessageDialog(controllingFrame,  // pop up a failure message
                        "$"+input +" is not a multiple of $20\n"
                        +"This is not a multiple of $20,Please try again.",
                        "Error Message",
                        JOptionPane.ERROR_MESSAGE);           
                }
            } else {        // User pressed a value button
                int buttonValue = (new Integer(cmd)).intValue();
                switch (buttonValue) {
                    case 20:  tryTransfers(cmd); break;
                    case 50:  tryTransfers(cmd); break;
                    case 100:  tryTransfers(cmd); break;
                    case 200:  tryTransfers(cmd); break;
                    case 300:  tryTransfers(cmd); break;
                  }   
              }              
        }
     
    // "tryTransferl" method calls ATMData.doTransferl and process results
        protected void tryTransfers(String in) {
        // run the doTransferl method on the ATMData object
        // this method returns the amount remaining in the account 
        // which is stored in the newBalance variable
        String newBalance = atmData.doTransfer(in);
     
          if (newBalance.isEmpty()) {           //Not enough cash
              // Display an error message 
              JOptionPane.showMessageDialog(controllingFrame,
                        "You don't have $"+in +" to Transfer.\n"
                        +"Sorry mate please try again.",
                        "Error Message",
                        JOptionPane.ERROR_MESSAGE);
          } else {                              // Transferl OK
              // Go back to the balance display screen by invoking the 
              // balanceGUI method on the atm object
              atm.balanceGUI();      
          }
        }    
     
    // Methods to populate the title pane, button pane 
    //  and bottom text entry pane with text, buttons and 
    //  text entry components. These methods are called from the 
    //  class constructor method which also adds them to the atm frame.
    //  These are written as seperate methods to seperate the "messy and confusing"
    //  GUI componentry from the actual processes conducted by this class.
     
        protected JComponent createTitlePane() {
            JPanel t = new JPanel(new GridLayout(0,1));
     
            JLabel topLabel = new JLabel("Select amount to Transfer: ");
            t.add(topLabel); 
     
            return (t);
        }
     
     
        protected JComponent createButtonPanel() {
            JPanel p = new JPanel(new GridLayout(0,2));
     
            au20Button = new JButton("$20");
            au20Button.setActionCommand(AU20);
            au20Button.addActionListener(this);
            p.add(au20Button);
     
            au200Button = new JButton("$200");
            au200Button.setActionCommand(AU200);        
            au200Button.addActionListener(this);
            p.add(au200Button);
     
            au50Button = new JButton("$50");
            au50Button.setActionCommand(AU50);       
            au50Button.addActionListener(this);
            p.add(au50Button);
     
            au300Button = new JButton("$300");
            au300Button.setActionCommand(AU300);   
            au300Button.addActionListener(this);        
            p.add(au300Button); 
     
            au100Button = new JButton("$100");
            au100Button.setActionCommand(AU100);        
            au100Button.addActionListener(this);
            p.add(au100Button);
     
            otherButton = new JButton("Choose another amount");
            otherButton.setActionCommand(OTHER);
            otherButton.addActionListener(this);
            p.add(otherButton);
     
            return p;
        }
     
        protected JComponent createBotTextPane() {
            JPanel b = new JPanel(new FlowLayout(FlowLayout.TRAILING));   
     
            amountTextField = new JTextField(5);
            amountTextField.setActionCommand(AMOUNT);
            amountTextField.addActionListener(this);
            amountTextField.setEnabled(false);
     
            amountLabel = new JLabel("Type Transfer amount (multiples of $20 only)");
            amountLabel.setLabelFor(amountTextField);
            amountLabel.setEnabled(false);
     
            account_infTextField = new JTextField(8);
            account_infTextField.setActionCommand(ACCOUNT_INF);
            account_infTextField.addActionListener(this);
            account_infTextField.setEnabled(true);
     
            account_infLabel = new JLabel("Enter the account number");
            account_infLabel.setLabelFor(account_infTextField);
            account_infLabel.setEnabled(true);
     
            b.add(amountLabel);
            b.add(amountTextField);
            b.add(account_infLabel);
            b.add(account_infTextField);
     
     
            return (b);
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating a transfer function for a ATM simulation

    Please edit your code and wrap it in code tags. See: BB Code List - Java Programming Forums
    You're missing the leading [code]

    You should define a class for the account data and the use method(s) to handle transfers between accounts. I don't see an Account class in your code.

    static String[][] account_inf = {{"John","Savings","12345","4444","100"},
    {"Jack","Manager","54321","0000","100"},
    {null,null,null},
    {null,null,null},
    {null,null,null}};
    This appears to be your account data. Change it from a 2 dim array to a class with some fields.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a transfer function for a ATM simulation

    Sorry, that should hopefully be better. =) I have included a account class but it contains errors and im not sure how i can incorporate the account class along with the ATMDataFile.



    import java.io.*;
    import java.util.Scanner;
     
    public class ATMDataFile {
    public static String datafilename = "ATMData.txt";
    public static int lines = 0; // Number of lines in ATMdata.txt
    public static int lineNo; // Line number of the opperative account
    // Create a File object called "datafile" to see if "ATMdata.txt" exists
    static File datafile = new File(datafilename);
    // Declare a 2 dimensional array to hold account details
    // First three values are only used if no datafile exists.
    static String[][] account_inf = {{"John","Savings","12345","4444","100"},
    {"Jack","Manager","54321","0000","100"},
    {null,null,null},
    {null,null,null},
    {null,null,null}}; 
     
    public ATMDataFile() throws IOException { 
    // If ATMdata.txt exists run loadData method 
    if (datafile.exists()) {
    loadData();
    // Debug code
    //writeFile();
    // If no datfile exists run the writeNewFile method
    } else {
    writeNewFile();
    }
    }
     
    // method for loading ATMdata.txt to a 2 dimensional array
    static void loadData()throws IOException {
    Scanner scan = null;
    BufferedReader read = null;
    try {
    // Create Scanner (reads text files one space delimeted element at a time)
    // and BufferedReader (reads contents of a text file into memory) objects 
    scan = new Scanner(new BufferedReader(new FileReader(datafile.getName())));
    read = new BufferedReader(new FileReader(datafile.getName()));
    // Count the number of lines in ATMdata.txt
    String l;
    while ((l = read.readLine()) != null) {
    lines ++ ;
    }
    // Debug code
    System.out.println ("Lines =" +lines);
    // Populate the 2 dimensional array account_inf with contents of 
    // ATMdata.txt. account_inf[0][0] is 1st account number
    // account_inf[0][1] is 1st account pasword
    // account_inf[0][2] is 1st account balance 
    // account_inf[1][0] is 2nd account number etc...
    for(int i=0; i<=lines-1; i++){
    for(int j=0; j<=4; j++){
    account_inf[i][j] = scan.next();
    // Debug code
    System.out.print(" [" +i +"][" +j +"]=" 
    +account_inf[i][j]);
    }
    // Debug code 
    System.out.println();
    } 
    } catch (IOException e) {
    System.err.println("Caught IOException: " 
    + e.getMessage());
    } finally {
    if (scan != null) {
    scan.close();
    }
    }
    }
     
    static void writeNewFile()throws IOException {
    String name = account_inf[0][0];
    String account_typ = account_inf[0][1];
    String account_no = account_inf[0][2];
    String password = account_inf[0][3];
    String balance = account_inf[0][4];
    PrintWriter out = null;
    try {
    // Create an output stream to write to ATMdata.txt
    out = new PrintWriter(datafile.getName());
    // Write the first three values of the two dimensional array
    // to the ATMdata.txt file. 
    out.format ("%s %s %s %s %s", account_inf[0][0], account_inf[0][1], account_inf[0][2], account_inf[0][3], account_inf[0][4]);
    } finally { 
    if (out != null) { 
    out.close();
    }
    }
    } 
     
    static void writeFile()throws IOException {
    PrintWriter out = null;
    try {
    // Create an output stream to write to ATMdata.txt
    out = new PrintWriter(datafile.getName());
    // Write account_inf[][] to disk 
    for (int i = 0; i<=lines-1; i++) {
    out.format ("%s %s %s %s %s", account_inf[i][0], 
    account_inf[i][1], 
    account_inf[i][2],
    account_inf[i][3],
    account_inf[i][4]);
     
    out.println();
    } 
    } finally { 
    if (out != null) { 
    out.close();
    }
    }
    } 
     
    static String getPin(String account_id){
    String pin = "";
    for (int i = 0; i <= lines ; i++) {
    if (account_id.equals(account_inf[i][2])) {
    pin = account_inf[i][3];
    lineNo = i;
    // Debug code
    System.out.println("pinString="+pin);
    break; 
    }
    } 
     
    return pin; 
    }
     
    static String doDeposits(String amount){ 
    // Deposit cash -- called from ATMDeposits
    String newBalance = "";
    // Turn Strings to doubles for arithmatic
    double depositsAmount= 
    (new Double(amount)).doubleValue();
    double currentBalance= 
    (new Double(account_inf[lineNo][4])).doubleValue(); 
    // Get the new balance as a string & update the account_inf array 
    newBalance = Double.toString
    (currentBalance + depositsAmount);
    account_inf[lineNo][4] = newBalance; 
     
    return newBalance; 
    }
     
     
     
    static String doWithdrawl(String amount){ 
    // Withdraw cash -- called from ATMWithdrawl
    String newBalance = "";
    // Turn Strings to doubles for arithmatic
    double withdrawlAmount= 
    (new Double(amount)).doubleValue();
    double currentBalance= 
    (new Double(account_inf[lineNo][4])).doubleValue(); 
    // Get the new balance as a string & update the account_inf array 
    newBalance = Double.toString
    (currentBalance - withdrawlAmount);
    account_inf[lineNo][4] = newBalance; 
     
    return newBalance; 
    }
     
    static String doTransfer(String amount){ 
    // Withdraw cash -- called from ATMWithdrawl
    String newBalance = "";
    // Turn Strings to doubles for arithmatic
    double transferAmount= 
    (new Double(amount)).doubleValue();
    double currentBalance= 
    (new Double(account_inf[lineNo][4])).doubleValue(); 
    // Get the new balance as a string & update the account_inf array 
    newBalance = Double.toString
    (currentBalance - transferAmount);
    account_inf[lineNo][4] = newBalance; 
     
    return newBalance; 
    }
     
    static String getBalance() {
    return account_inf[lineNo][4];

    Import GUI, IO and math libraries
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.math.*;
     
    // Class declaration establishing ATMTransfer as a subtype of 
    // JPanel (a content pane to be displayed inside a JFrame)
    // elements of a graphic user interface (GUI)
    // impliments ActionListener statement indicates that it uses 
    // the Java actionlistener class
    public class ATMTransfer extends JPanel
    implements ActionListener {
    // Declare string variables for use in GUI
    private static String AU20 = "20";
    private static String AU50 = "50";
    private static String AU100 = "100";
    private static String AU200 = "200";
    private static String AU300 = "300";
    private static String OTHER = "other";
    private static String AMOUNT = "amount";
    private static String ACCOUNT_INF = "account_inf";
    // Declare JButton variables for use in GUI 
    private static JButton au20Button, au50Button, au100Button, 
    au200Button,au300Button, otherButton;
    // Declare object variables for use in GUI
    private JFrame controllingFrame; //needed for dialogs
    protected JTextField amountTextField;
    protected JTextField account_infTextField;
    protected JLabel amountLabel;
    protected JLabel account_infLabel;
     
    private ATMDataFile atmData;
    private ATM atm;
     
    // Constructor method - called from ATM with ATMframe as the argument f
    // an ATMTransferl object is created as a subtype of a JPanel
    public ATMTransfer(JFrame f) {
    controllingFrame = f;
    JComponent titlePane = createTitlePane();
    add(titlePane); // add the titlepane to the ATMTransfer JPanel
    // Call the createButtonPanel method to populate the button pane 
    JComponent buttonPane = createButtonPanel();
    add(buttonPane);
    // Call the createBotTextPane method to populate the text box pane pane
    JComponent botTextPane = createBotTextPane();
    add(botTextPane);
    add(botTextPane);
    }
     
    // "actionPerformed" method - reads the action command "cmd"
    // set by the button and text field components and selected 
    // by the users mouse click or text entry
    // and selects the action to perform
    public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand(); //gets "cmd" if button clicked
    String input = amountTextField.getText(); //gets "input" from text entry 
    if (OTHER.equals(cmd)){ // Other amount button pressed.
    amountTextField.setEnabled(true);
    amountTextField.setEnabled(true);// enable text entry
    amountLabel.setEnabled(true); 
    account_infTextField.setEnabled(true);
    account_infLabel.setEnabled(true); 
    au20Button.setEnabled(false); // disable (grey out) other buttons
    au50Button.setEnabled(false);
    au100Button.setEnabled(false);
    au200Button.setEnabled(false);
    au300Button.setEnabled(false);
    otherButton.setEnabled(false);
    amountTextField.requestFocusInWindow(); //set focus to text entry
    } else if (AMOUNT.equals(cmd)) { // User enters other amount.
    //Test if the entered ammount is a multiple of $20
    // by using the "mod" opperation to divide by 20 and find the remainder
    BigInteger remainder = (new BigInteger(input)).mod 
    (new BigInteger("20"));
    // Test if remainder is "0"
    if (remainder.equals((new BigInteger("0")))) { // Yes it's a multiple
    JOptionPane.showMessageDialog(controllingFrame, // pop up a success message
    "$"+input +" is a multiple of $20\n"
    +"Well done!!");
    // Invoke the "tryTransfers" method 
    // with the entered ammount as the argument 
    tryTransfers(input); 
     
    } else { // Not a multiple of $20
    JOptionPane.showMessageDialog(controllingFrame, // pop up a failure message
    "$"+input +" is not a multiple of $20\n"
    +"This is not a multiple of $20,Please try again.",
    "Error Message",
    JOptionPane.ERROR_MESSAGE); 
    }
    } else { // User pressed a value button
    int buttonValue = (new Integer(cmd)).intValue();
    switch (buttonValue) {
    case 20: tryTransfers(cmd); break;
    case 50: tryTransfers(cmd); break;
    case 100: tryTransfers(cmd); break;
    case 200: tryTransfers(cmd); break;
    case 300: tryTransfers(cmd); break;
    } 
    } 
    }
     
    // "tryTransferl" method calls ATMData.doTransferl and process results
    protected void tryTransfers(String in) {
    // run the doTransferl method on the ATMData object
    // this method returns the amount remaining in the account 
    // which is stored in the newBalance variable
    String newBalance = atmData.doTransfer(in);
     
    if (newBalance.isEmpty()) { //Not enough cash
    // Display an error message 
    JOptionPane.showMessageDialog(controllingFrame,
    "You don't have $"+in +" to Transfer.\n"
    +"Sorry mate please try again.",
    "Error Message",
    JOptionPane.ERROR_MESSAGE);
    } else { // Transferl OK
    // Go back to the balance display screen by invoking the 
    // balanceGUI method on the atm object
    atm.balanceGUI(); 
    }
    } 
     
    // Methods to populate the title pane, button pane 
    // and bottom text entry pane with text, buttons and 
    // text entry components. These methods are called from the 
    // class constructor method which also adds them to the atm frame.
    // These are written as seperate methods to seperate the "messy and confusing"
    // GUI componentry from the actual processes conducted by this class.
     
    protected JComponent createTitlePane() {
    JPanel t = new JPanel(new GridLayout(0,1));
     
    JLabel topLabel = new JLabel("Select amount to Transfer: ");
    t.add(topLabel); 
     
    return (t);
    }
     
     
    protected JComponent createButtonPanel() {
    JPanel p = new JPanel(new GridLayout(0,2));
     
    au20Button = new JButton("$20");
    au20Button.setActionCommand(AU20);
    au20Button.addActionListener(this);
    p.add(au20Button);
     
    au200Button = new JButton("$200");
    au200Button.setActionCommand(AU200); 
    au200Button.addActionListener(this);
    p.add(au200Button);
     
    au50Button = new JButton("$50");
    au50Button.setActionCommand(AU50); 
    au50Button.addActionListener(this);
    p.add(au50Button);
     
    au300Button = new JButton("$300");
    au300Button.setActionCommand(AU300); 
    au300Button.addActionListener(this); 
    p.add(au300Button); 
     
    au100Button = new JButton("$100");
    au100Button.setActionCommand(AU100); 
    au100Button.addActionListener(this);
    p.add(au100Button);
     
    otherButton = new JButton("Choose another amount");
    otherButton.setActionCommand(OTHER);
    otherButton.addActionListener(this);
    p.add(otherButton);
     
    return p;
    }
     
    protected JComponent createBotTextPane() {
    JPanel b = new JPanel(new FlowLayout(FlowLayout.TRAILING)); 
     
    amountTextField = new JTextField(5);
    amountTextField.setActionCommand(AMOUNT);
    amountTextField.addActionListener(this);
    amountTextField.setEnabled(false);
     
    amountLabel = new JLabel("Type Transfer amount (multiples of $20 only)");
    amountLabel.setLabelFor(amountTextField);
    amountLabel.setEnabled(false);
     
    account_infTextField = new JTextField(8);
    account_infTextField.setActionCommand(ACCOUNT_INF) ;
    account_infTextField.addActionListener(this);
    account_infTextField.setEnabled(true);
     
    account_infLabel = new JLabel("Enter the account number");
    account_infLabel.setLabelFor(account_infTextField) ;
    account_infLabel.setEnabled(true);
     
    b.add(amountLabel);
    b.add(amountTextField);
    b.add(account_infLabel);
    b.add(account_infTextField);
     
     
    return (b);
    }
    }

    public class Account {
        private String accountName;
        private int password;
        private double balanceAmount;
        private static int accountNumberCounter = 100;
        private int accountNumber;
     
     
        public Account(String name, int pword, double balance) {
            accountName = name;
            password = pword;
            balanceAmount = balance;
            accountNumberCounter++;
            accountNumber = accountNumberCounter;
     
            // This shows we have brought the information over from the BankApp class
            System.out.println(accountName + " " + password + " " + balanceAmount);
            System.out.println("Option " + BankApp.option + " was selected.");
     
        }
     
        public int acctNum() {
            return accountNumber;
        }
     
        public double getBalance() {
            return balanceAmount;
     
        }
     
        public void withdraw(int amount) {
            if (i  > balanceAmount) {
                System.out
                        .println("You do not have enough money in your account, try again");
            } else {
                balanceAmount = balanceAmount - ! amount;
            }
     
        }
     
        public void deposit(int amount) {
            balanceAmount = balanceAmount + "int amount"; 
            if( i > 0) System.out
                        .println("Can't deposit negative amounts");
        }
     
        public void transfer(Account from, Account to, int amount);
     
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating a transfer function for a ATM simulation

    it contains errors
    Please copy and paste the full text of the error messages here.

    The Account class should hold the data you have in this array. Use an ArrayList to hold all of the Account class objects that are created.
    // Declare a 2 dimensional array to hold account details
    // First three values are only used if no datafile exists.
    static String[][] account_inf = {{"John","Savings","12345","4444","100"},
    {"Jack","Manager","54321","0000","100"},
    {null,null,null},
    {null,null,null},
    {null,null,null}};

    how i can incorporate the account class along with the ATMDataFile.
    As you read in the data from the file, build a new Account class object and add it to the arraylist that holds the Accounts.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a transfer function for a ATM simulation

    Cannot Find Symbol-Variable i in the Account class.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating a transfer function for a ATM simulation

    Please copy full text of error message and paste it here. Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    Where is the variable i defined?

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a transfer function for a ATM simulation

    I'm not sure what is supposed to be where i have placed "i"
     public void withdraw(int amount) {
            if (i  > balanceAmount) {[/COLOR]
                System.out
                        .println("You do not have enough money in your account, try again");
            } else {
                balanceAmount = balanceAmount - ! amount;
            }
     
        }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating a transfer function for a ATM simulation

    Look at the logic of the if statement? What would you compare to the balance?
    What is the name of the method? What does that mean you intend to do to the balance?

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a transfer function for a ATM simulation

    What im trying to do there is if the withdraw amount is greater than the balance amount then deny it.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating a transfer function for a ATM simulation

    That makes sense.

    What is the variable i for?

  11. #11
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a transfer function for a ATM simulation

    Ok i have fixed that problem, however i dont really know how to implement an arraylist for the accounts.Sorry i'm fairly new to Java =(
    import java.io.BufferedReader; 
    import java.io.IOException; 
    import java.io.InputStreamReader; 
    import java.util.ArrayList;
    import java.util.Scanner; 
     
    public class Account {
        private String accountName;
        private int password;
        private double balanceAmount;
        private static int accountNumberCounter = 100;
        private int accountNumber;
        public static Scanner userInput = new Scanner(System.in);
     
        public Account(String name, int pword, double balance) {
            accountName = name;
            password = pword;
            balanceAmount = balance;
            accountNumberCounter++;
            accountNumber = accountNumberCounter;
     
            // This shows we have brought the information over from the BankApp class
            System.out.println(accountName + " " + password + " " + balanceAmount);
            System.out.println("Option " + BankApp.option + " was selected.");
     
     
            Scanner userInput = new Scanner(System.in);    
            Account bob = new Account("Bob Jones", 1111,100.00);
            Account mary = new Account("Mary Mack", 2222,200.00);
            Account sally = new Account("Sally Star", 1111,300.00);
     
     
        }
     
        public int acctNum() {
            return accountNumber;
        }
     
        public double getBalance() {
            return balanceAmount;
     
        }
     
        public void withdraw(int amount) {
            if (amount  > balanceAmount) {
                System.out
                        .println("You do not have enough money in your account, try again");
            } else {
                balanceAmount = balanceAmount - amount;
            }
     
        }
     
        public void deposit(int amount) {
            balanceAmount = balanceAmount + amount; 
            if(amount > 0) System.out
                        .println("Can't deposit negative amounts");
        }
     
        public void transfer(Account from, Account to, int amount) {
     
     
    }
    }
    Last edited by D3C; August 13th, 2011 at 10:26 AM.

Similar Threads

  1. MIDI question ... need to slow down receiver.send() transfer rate
    By LegGodt in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: July 10th, 2011, 08:56 AM
  2. [SOLVED] Dice Rolling Simulation
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2011, 06:51 PM
  3. Program with transfer value from one class to another
    By Adam22 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 6th, 2011, 12:26 AM
  4. Keypress simulation problems
    By ummix in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2010, 07:31 AM
  5. Replies: 1
    Last Post: September 10th, 2009, 10:54 AM