title: Java - Running Total in Simple GUI Banking Program
body: <p>I am having trouble trying to figure out how to code the deposits and withdrawals for my banking program and keep a running total. The program should start you with $0 in the bank and cannot go below $0. You are able to deposit and withdraw from this account and you can view who deposited and withdrew from the menu.</p>
  
  <p>I need help trying to code the part that keeps the total of the account so that the user can't withdraw more than whats in the account. So if there is $100 in the account, they can't withdraw $200.</p>
  
  <p>Note: The 'Create Account' action is just used to give yourself a unique ID when depositing/withdrawing and doesn't effect the login at all.</p>
  
  <p><strong>Login.java:</strong></p>
  
  <pre><code>import java.awt.FlowLayout;
  import java.awt.GridLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.WindowAdapter;
  import java.awt.event.WindowEvent;
  import java.util.Random;
  
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.JPasswordField;
  import javax.swing.JTextField;
  
  public class Login {
  
      private JFrame mainFrame;
      private JLabel headerLabel;
      private JLabel statusLabel;
      private JPanel controlPanel;
  
      int cid;
  
      public Login() {
          prepareGUI();
      }
  
      public static void main(String[] args) {
          Login login = new Login();
          login.showTextField();
      }
  
      private void prepareGUI() {
          mainFrame = new JFrame("Login");
          mainFrame.setSize(400, 400);
          mainFrame.setLayout(new GridLayout(3, 1));
          mainFrame.addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent wE) {
                  System.exit(0);
              }
          });
          headerLabel = new JLabel("", JLabel.CENTER);
          statusLabel = new JLabel("", JLabel.CENTER);
  
          statusLabel.setSize(350, 100);
  
          controlPanel = new JPanel();
          controlPanel.setLayout(new FlowLayout());
  
          mainFrame.add(headerLabel);
          mainFrame.add(controlPanel);
          mainFrame.add(statusLabel);
          mainFrame.setVisible(true);
      }
  
      private void showTextField() {
          headerLabel.setText("Account Access");
  
          JLabel namelabel = new JLabel("User ID: ", JLabel.RIGHT);
          JLabel passwordLabel = new JLabel("Password: ", JLabel.CENTER);
          final JTextField userText = new JTextField(6);
          final JPasswordField passwordText = new JPasswordField(6);
  
          JButton loginButton = new JButton("Login");
          JButton createAccountButton = new JButton("Create Account?");
  
          loginButton.addActionListener(new ActionListener() {
              @SuppressWarnings("deprecation")
              public void actionPerformed(ActionEvent e) {
                  /*
                   * check for correct password/usernameinclude a desired
                   * 'hardcoded' username /password to verify againstuser input
                   * values for both username &amp; password fieldsgive popup message
                   * if either username or password is incorrect
                   */
                  if (userText.getText().equals("mister")
                          &amp;&amp; passwordText.getText().equals("jim")) {
  
                      // close of Login window
                      mainFrame.dispose();
                      // open up MainWindow
                      new MainWindow(userText.getText(), cid);
  
                  }
  
                  else {
                      String message = "Incorrect username and/or password!\nTry again!";
                      JOptionPane.showMessageDialog(null, message);
                  }
              }
  
          });
  
          createAccountButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
  
                  String name = JOptionPane.showInputDialog(null,
                          "Enter your name");
                  // generate client id
                  Random r = new Random();
                  cid = r.nextInt(100000);
              }
  
          });
  
          controlPanel.add(namelabel);
          controlPanel.add(userText);
          controlPanel.add(passwordLabel);
          controlPanel.add(passwordText);
          controlPanel.add(loginButton);
          controlPanel.add(createAccountButton);
          mainFrame.setVisible(true);
      }
  }
  </code></pre>
  
  <p><strong>MainWindow.java:</strong></p>
  
  <pre><code>import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileReader;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.util.Vector;
  
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JMenu;
  import javax.swing.JMenuBar;
  import javax.swing.JMenuItem;
  import javax.swing.JOptionPane;
  import javax.swing.JScrollPane;
  import javax.swing.JTable;
  
  public class MainWindow extends JFrame {
  
      private JFrame mainFrame;
      private JLabel statusLabel;
  
      private JMenu file = new JMenu("File");
      private JMenu Account = new JMenu("Account");
  
      int cid;
  
      public MainWindow(String name, final int cid) {
  
          this.cid = cid;
  
          file.setMnemonic('F');
          JMenuItem ItemNew = new JMenuItem("New");
          ItemNew.setMnemonic('N');
          file.add(ItemNew);
          JMenuItem ItemExit = new JMenuItem("Exit");
          ItemExit.setMnemonic('x');
          file.add(ItemExit);
  
          ItemExit.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  System.exit(0);
              }
          });
  
          Account.setMnemonic('A');
          JMenuItem ItemDeposits = new JMenuItem("Deposits");
          ItemDeposits.setMnemonic('D');
          Account.add(ItemDeposits);
  
          JMenuItem ItemWithdraws = new JMenuItem("Withdrawals");
          ItemWithdraws.setMnemonic('W');
          Account.add(ItemWithdraws);
  
          JMenuItem ItemView = new JMenuItem("View Account");
          ItemView.setMnemonic('W');
          Account.add(ItemView);
  
          ItemDeposits.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {// add a deposit
                  Double sBal = Double.parseDouble(JOptionPane.showInputDialog(
                          null, "Enter deposit amount"));
                  // create Account object
                  Account accountObj = new Account();
                  accountObj.setBal(sBal);
  
                  // show result
                  System.out.println(accountObj.getCID() + accountObj.getBal());
                  File f = new File("account.dat");
                  try {
                      FileWriter fw = new FileWriter(f, true);
                      fw.write(cid + " " + accountObj.getBal() + "\n");
  
                      fw.close();
                  } catch (IOException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
  
              }
          });
  
          ItemWithdraws.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {// add a deposit
                  Double sBal = Double.parseDouble(JOptionPane.showInputDialog(
                          null, "Enter withdrawal amount"));
                  // create Account object
                  Account accountObj = new Account();
                  accountObj.setBal(sBal);
  
                  // show result
                  System.out.println(accountObj.getBal());
                  File f = new File("account.dat");
                  try {
                      FileWriter fw = new FileWriter(f, true);
                      fw.write(cid + " " + accountObj.getBal() + "\n");
  
                      fw.close();
                  } catch (IOException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
  
              }
          });
  
          ItemView.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  statusLabel.setVisible(false); // make status label invisible
  
                  // set up JTable logic
                  Vector&lt;Vector&lt;String&gt;&gt; myVector = new Vector&lt;Vector&lt;String&gt;&gt;(); // multidim
                                                                                  // vector
                                                                                  // (vector
                                                                                  // of
                                                                                  // vectors)
  
                  try {
  
                      BufferedReader file = new BufferedReader(new FileReader(
                              "account.dat"));
                      String input;
                      while ((input = file.readLine()) != null) {
                          String[] temp = input.split(" "); // grab row (record)
                                                              // data parsed by a
                                                              // space
                          Vector&lt;String&gt; v = new Vector&lt;String&gt;(); // single dim
                                                                      // vector to
                                                                      // get
                                                                      // fields in
                                                                      // each
                                                                      // record
                          for (int i = 0; i &lt; temp.length; i++) {
                              v.add(temp[i]); // add each field to vector
                          }
                          myVector.add(v); // add all field data as a new vector
                                              // row (represents a record of data
                                              // each dynamically!!!)
                      }
                      file.close();
                  } catch (IOException e1) {
                      e1.printStackTrace(System.err);
                  }
  
                  Vector&lt;String&gt; columnData = new Vector&lt;String&gt;();
                  columnData.addElement("ID");
                  columnData.addElement("D/W");
  
                  try {
  
                      JTable jt;
  
                      jt = new JTable(myVector, columnData);
                      jt.setBounds(30, 40, 200, 300);
                      JScrollPane sp = new JScrollPane(jt);
  
                      mainFrame.add(sp);
  
                  } catch (Exception ex) {
                      System.out.println("There was a problem: " + ex);
                      ex.printStackTrace();
                  }
              }
          });
  
          ItemNew.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
  
              }
          });
          statusLabel = new JLabel();
  
          statusLabel.setText("Currently Logged In: " + name + " #" + cid);
          statusLabel.setHorizontalAlignment(JLabel.RIGHT);
          statusLabel.setVerticalAlignment(JLabel.TOP);
  
          prepareGUI();
  
      }
  
      private void prepareGUI() {
          mainFrame = new JFrame("Main");
  
          // adjust label position to sit in the upper right corner of window
          mainFrame.add(statusLabel);
  
          // add menu bar component to frame
          JMenuBar bar = new JMenuBar();
          bar.add(file); // set menu orders
          bar.add(Account);
          mainFrame.setJMenuBar(bar);
  
          mainFrame.setSize(400, 400);
          mainFrame.setVisible(true);
  
      }
  
  }
  </code></pre>
  
  <p><strong>Account.java:</strong></p>
  
  <pre><code>public class Account {
  
      // data members
      double bal;
      static double intRate;
      int cid;
      String name;
  
      public Account() {
      }
  
      // getters
      double getBal() {
          return bal;
      }
  
      int getCID() {
          return cid;
      }
  
      // setters
      void setBal(double bal) {
          // update balance
          this.bal += bal;
  
      }
  
      void setCID(int cid) {
          this.cid = cid;
      }
  
      static void adjustIntRate(double r) {
          intRate = r;
      }
  
  }
  </code></pre>