The Java Standalone Application
Peter Komisar


First a Word on the The Static Home Page

With respect to our earlier intention of creating
a static home page for the company, we can
of course use Eclipse to do this. I was able to
use it as a simple tag editor. It does have the
capacity to be a WYSIWYG editor.


The Java StandAlone Application

Recall we stated we would first like to develop a
standalone application to provide some function for
the employees or guests of the company.

The following example was created in Eclipse.
We can use it as a reference in creating our own
examples. Later we will go over in more detail
what Eclipse offers to the Java developer.


In this example a reasonable unique number is
created for a guest who is visiting the company
on a  day visit. This allows the guest to gain
access to the company network on a temporary
basis.

We can envision that this class will provide access
to a database at a later point.

We also code a sample UI to provide the guest
with their voucher. Perhaps it would have been
convenient to put this code into an applet that
would be accessed from a web page.

The code has three functions at this point. Undoubtedly,
with time many more methods would be needed to
make this a real world application.


Voucher Class Methods


The Voucher Class


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;


public class Voucher {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new VoucherUI();
    }
    // method
    static String createVoucher(String firstName, String lastName){
        String names =     (firstName + " " + lastName);
        Object time= Calendar.getInstance().getTime();
        String combo = time + names;
        int hash = combo.hashCode();
        int absoluteHash = Math.abs(hash);
        return ("" + absoluteHash);
        }
   void   setVoucher(){
       // will get DataSource and load
       // first and last names and voucher
       // into a database
      
   String[] getVoucher(){
       // will retrieve, first and last
       // name and the associated voucher
       String[] voucherID=new String[3];
       voucher[0]="first name";
       voucher[1]="last name";
       voucher[2]="voucher";
       return voucherID;
      }
    }
}

The UI

The UI is a simple Swing UI that captures the first and last
name of the guest, and hashes these values with a time of
day and date. This would result in a reasonably unique token
that can be used to provide temporary access to the system.

Later we will need to ensure that this token that is given to
the guest is stored with his or her first and last name in our
database.

The VoucherUI Class

public class VoucherUI extends JFrame implements ActionListener{
    
    String firstName,lastName, voucher;    
    JTextField fieldFirst,fieldLast,fieldVoucher;
    JButton button;
    JPanel panel;
    
    VoucherUI( ){
        JLabel title = new JLabel("Day Voucher");
        JLabel labelFirst = new JLabel("First Name");
        JLabel labelLast = new JLabel("Last Name");
        JLabel labelVoucher = new JLabel("Voucher");
        fieldFirst = new JTextField();
        fieldLast = new JTextField();
        fieldVoucher = new JTextField();
        button=new JButton("Get Voucher");
        button.addActionListener(this);
        panel= new JPanel();
        panel.setLayout(new GridLayout(7,1));
        getContentPane().add(title,BorderLayout.NORTH);
        // load panel
        panel.add(labelFirst);
        panel.add(fieldFirst);
        panel.add(labelLast);
        panel.add(fieldLast);
        panel.add(button);
        panel.add(labelVoucher);
        panel.add(fieldVoucher);
        getContentPane().add(panel);
        setSize(250,350);
        setVisible(true);
        }
    
    // action method
    
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource().equals(button))
         {     firstName=fieldFirst.getText();
            lastName=fieldLast.getText();
            voucher = Voucher.createVoucher(firstName, lastName);   
               System.out.println(voucher);
            fieldVoucher.setText(voucher);    
            
            // this 'snaphshot' of the voucher
            // will need to be sent to the database
            // for the day
            
            }
    } // end of action method
} // end of class


Assignment 2


Create the Java Standalone Application for
your Company. If you haven't figured one out
modify the above code so it is generic and
adaptable to different situations in the future.