Java Certification, Programming, JavaBean and Object Oriented Reference Books
Java application simulating an automatic bank teller machine

//File Name: Teller.java
//Version of Java: Java 2
//Author: Saad Ayub

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.text.DecimalFormat;

public class Teller extends JFrame implements ActionListener{
   private JTextField acctNum, secCode;
   private JButton ok, cancel;
   private JPanel textI, buttonI;
   private JLabel stat;
   private Container c;
   //Setting up the login screen.
   public Teller(){
      super("Teller Login");
      acctNum = new JTextField();
      secCode = new JTextField();
      ok = new JButton("Ok");
      cancel = new JButton("Cancel");
      textI = new JPanel(new GridLayout(3, 3, 3, 3));
      stat = new JLabel("");
      buttonI = new JPanel(new GridLayout(2, 2, 5, 5));
      textI.add(new JLabel(""));
      textI.add(new JLabel(""));
      textI.add(new JLabel("Enter AccountNumber:"));
      textI.add(acctNum);
      textI.add(new JLabel("Enter Five Digit SecretCode:"));
      textI.add(secCode);
      buttonI.add(ok);
      buttonI.add(cancel);
      buttonI.add(stat);
      ok.addActionListener(this);
      cancel.addActionListener(this);
      c = getContentPane();
      c.add(textI, BorderLayout.NORTH);
      c.add(buttonI, BorderLayout.SOUTH);
      setSize(400, 200);
      show();
   }

   //The main that instantiates the Teller object.
   public static void main(String args[]){
      Teller tel = new Teller();
      tel.addWindowListener(
         new WindowAdapter(){
            public void windowClosing(WindowEvent e){
               System.exit(0);
            }
         }
      );
   }

   //Verifying the account number and the secret code.
   public int verify(){
      long acct, code, acc, cde, position;
      int stat = 0;
      RandomAccessFile output;
      try{
         output = new RandomAccessFile("acctInfo.dat", "r");
         position = (Integer.parseInt(acctNum.getText()) - 1) * 36;
         output.seek(position);

         acct = output.readInt();
         code = output.readInt();

         if(acct == Integer.parseInt(acctNum.getText()) &&
            code == Integer.parseInt(secCode.getText())){
            stat = 1;
         }
         else
            stat = 2;

         output.close();
      }
      catch(NumberFormatException ex){
         stat = 3;
      }
      catch(IOException e){
         System.out.println(e.toString());
      }
      catch(Exception ex){
         System.out.println(ex.toString());
      }
      return stat;
   }

   public void actionPerformed(ActionEvent e){
      int stat;
      if(e.getSource() == ok){
         if(acctNum.getText().equals("") || secCode.getText().equals("")){
            JOptionPane.showMessageDialog(this, "Please enter all information.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
         }

         stat = verify();

         //If stat is true the showMenu method is called.
         if(stat == 1){
            showMenu();
         }
         else if(stat == 2){
            JOptionPane.showMessageDialog(this, "Invalid account number or secret code.", "Login Error", JOptionPane.ERROR_MESSAGE);
            return;
         }
         else if(stat == 3){
            JOptionPane.showMessageDialog(this, "Invalid input format, please enter information in the right format.", "Format Error", JOptionPane.ERROR_MESSAGE);
            return;
         }
      }
      else{
         System.exit(0);
      }
   }

   //Method update updates an account information.
   public void update(double amt, boolean op){
      int position;
      double cur;

      try{
         RandomAccessFile output = new RandomAccessFile("acctInfo.dat", "rw");
         position = (Integer.parseInt(acctNum.getText()) - 1) * 36;
         output.seek(position);
         output.readInt();
         output.readInt();
         for(int i = 0; i < 10; i++)
            output.readChar();

         for(int i = 0; i < 10; i++)
            output.readChar();

         try{
            cur = output.readDouble();

            //Op being true means a deposit.
            if(op == true){
               output.seek(position);

               output.readInt();
               output.readInt();
               for(int i = 0; i < 10; i++)
                  output.readChar();

               for(int i = 0; i < 10; i++)
                  output.readChar();
               output.writeDouble(cur + amt);
            }

            //Op being false means a withdraw.
            else if(op == false && cur >= amt){
               output.seek(position);

               output.readInt();
               output.readInt();
               for(int i = 0; i < 10; i++)
                  output.readChar();

               for(int i = 0; i < 10; i++)
                  output.readChar();
               output.writeDouble(cur - amt);
            }
            else{
               JOptionPane.showMessageDialog(this, "Insufficient funds, withdraw operation failed.", "Insufficiant Funds", JOptionPane.ERROR_MESSAGE);
            }
         }
         catch(Exception e){System.out.println(e.toString());}
         output.close();
      }
      catch(Exception e){}
   }

   //Showing the deposit screen.
   public void showDepo(){
      setTitle("Deposit");
      final JTextField txtDepo = new JTextField();
      JButton depo = new JButton("Continue");
      depo.addActionListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent e){
               if(txtDepo.getText().equals("")){
                  JOptionPane.showMessageDialog(Teller.this, "Please enter amount to deposit.", "Error", JOptionPane.ERROR_MESSAGE);
                  return;
               }
               try{
                  update(Double.parseDouble(txtDepo.getText()), true);
                  showMenu();
               }
               catch(NumberFormatException ex){
                  JOptionPane.showMessageDialog(Teller.this, "Amount has to be numeric.", "Format Error", JOptionPane.ERROR_MESSAGE);
                  txtDepo.setText("");
               }
            }
         }
      );
      JButton can = new JButton("Cancel");
      can.addActionListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent e){
               showMenu();
            }
         }
      );
      c.removeAll();
      c.validate();
      c.repaint();

      c.setLayout(new GridLayout(5, 2, 4, 4));
      c.add(new JLabel());
      c.add(new JLabel());
      c.add(new JLabel("Enter Amount to Deposit:"));
      c.add(txtDepo);
      c.add(new JLabel());
      c.add(new JLabel());
      c.add(new JLabel());
      c.add(new JLabel());
      c.add(depo);
      c.add(can);
      c.validate();
      c.repaint();
   }

   //Showing the withdraw screen.
   public void showWith(){
      setTitle("Withdrawal");
      final JTextField txtWith = new JTextField();
      JButton with = new JButton("Continue");
      with.addActionListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent e){
               if(txtWith.getText().equals("")){
                  JOptionPane.showMessageDialog(Teller.this, "Please enter amount to withdraw.", "Error", JOptionPane.ERROR_MESSAGE);
                  return;
               }
               try{
                  update(Double.parseDouble(txtWith.getText()), false);
                  showMenu();
               }
               catch(NumberFormatException ex){
                  JOptionPane.showMessageDialog(Teller.this, "Amount has to be numeric.", "Format Error", JOptionPane.ERROR_MESSAGE);
                  txtWith.setText("");
               }
            }
         }
      );
      JButton can = new JButton("Cancel");
      can.addActionListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent e){
               showMenu();
            }
         }
      );
      c.removeAll();
      c.validate();
      c.repaint();

      c.setLayout(new GridLayout(5, 2, 4, 4));

      c.add(new JLabel());
      c.add(new JLabel());
      c.add(new JLabel("Enter Amount to Withdraw:"));
      c.add(txtWith);
      c.add(new JLabel());
      c.add(new JLabel());
      c.add(new JLabel());
      c.add(new JLabel());
      c.add(with);
      c.add(can);
      c.validate();
      c.repaint();
   }

   //Showing the account's information.
   public void showBal(){
      int position = 0;
      char alp;
      DecimalFormat twoDigits = new DecimalFormat("0.00");
      setTitle("Current Account Balance");
      JTextArea display = new JTextArea(60, 40);
      JButton ok = new JButton("Ok");
      ok.addActionListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent e){
               showMenu();
            }
         }
      );
      String mess = "";

      try{
         RandomAccessFile output = new RandomAccessFile("acctInfo.dat", "r");
         position = (Integer.parseInt(acctNum.getText()) - 1) * 36;
         output.seek(position);
         try{
            mess += "Account Number: ";
            mess += Integer.toString(output.readInt()) + "\nFirst Name: ";
            output.seek(position + 8);

            for(int i = 0; i < 10; i++){
               alp = output.readChar();
               if(alp != '\0')
                  mess += alp;
            }
 

            mess += "\nLast Name: ";

            for(int i = 0; i < 10; i++){
               alp = output.readChar();
               if(alp != '\0')
                  mess += alp;
            }

            mess += "\n\n\nBalance: $";
            mess += twoDigits.format(output.readDouble());
         }
         catch(IOException e){
            System.out.println(e.toString());
         }
         output.close();
      }
      catch(Exception e){}

      display.setText(mess);

      c.removeAll();
      c.validate();
      c.repaint();

      c.setLayout(new BorderLayout());
      c.add(display, BorderLayout.CENTER);
      c.add(ok, BorderLayout.SOUTH);
      c.validate();
      c.repaint();
   }

   //Showing the main menu.
   public void showMenu(){
      JButton depo, withdr, bal, exit;
      setTitle("Teller Menu");
      depo = new JButton("Deposit");
      depo.addActionListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent e){
               showDepo();
            }
         }
      );
      withdr = new JButton("Withdraw");
      withdr.addActionListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent e){
               showWith();
            }
         }
      );
      bal = new JButton("Balance");
      bal.addActionListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent e){
               showBal();
            }
         }
      );
      exit = new JButton("Exit");
      exit.addActionListener(this);

      c.removeAll();
      c.validate();
      c.repaint();

      c.setLayout(new GridLayout(5, 1));
      c.add(new JLabel("Select an Operation:"));
      c.add(depo);
      c.add(withdr);
      c.add(bal);
      c.add(exit);
      c.validate();
      c.repaint();
   }
}

Return to : Java Programming Hints and Tips