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); } }