HashtableTest Applet



// testing the Hashtable class of the java.util package

import java.applet.*;
import java.awt.*;
import java.util.*;

public class HashtableTest extends Applet {

	Hashtable table;

	Label name1, name2;
	TextField fName, lName;
	TextArea display;
	Button putBtn, getBtn, removeBtn, emptyBtn, containsKeyBtn, containsObjBtn, clearBtn, listElemsBtn, listKeysBtn;
	
   public void init()
     	{
		table = new Hashtable();

		name1 = new Label( " First Name" );
		name2 = new Label( " Last Name (key)" );
		fName = new TextField( 10 );
		lName = new TextField( 10 );
		display = new TextArea ( 4, 45);
		putBtn = new Button( "Put" );
		getBtn = new Button( "Get" );
		removeBtn = new Button( "Remove" );
		emptyBtn = new Button( "Empty?");
		containsKeyBtn = new Button( "Contains key");
		containsObjBtn = new Button( "Contains object");
		clearBtn = new Button( "Clear");
		listElemsBtn = new Button( "List objects");
		listKeysBtn = new Button( "List keys");

		add( name1);
		add( fName);
		add( name2);
		add( lName);
		add( display);
		add( putBtn );
		add( getBtn );
		add( removeBtn );
		add( emptyBtn );
		add( containsKeyBtn );
		add( containsObjBtn );
		add( clearBtn );
		add( listElemsBtn );
		add( listKeysBtn );
	}

   public boolean action( Event e, Object o)
	{
		if ( e.target == putBtn ) {
			Employee emp = new Employee( fName.getText(), lName.getText());
			Object val = table.put (lName.getText(), emp);
			if ( val == null )
				showStatus( "Put: " + emp.toString() ) ;
			else
				showStatus( "Put: " + emp.toString() + "; Replaced: " + val.toString() );
		}			
		else if ( e.target == getBtn ) {
			Object val = table.get (lName.getText());
			if ( val != null )
				showStatus( "Get: " + val.toString() ) ;
			else
				showStatus( "Get: " + lName.getText() + " not in table" );		}
		else if ( e.target == removeBtn ) {
			Object val = table.remove (lName.getText());
			if ( val != null )
				showStatus( "Remove: " + val.toString() ) ;
			else
				showStatus( "Remove " + lName.getText() + " not in table" );
		}
		else if ( e.target == emptyBtn ) {
			showStatus ( table.isEmpty() ?  "Table is empty " : "Table is not empty");
		}
		else if ( e.target == containsKeyBtn ) {
			showStatus ("Contains Key: " + table.containsKey (lName.getText() ) );
		}
		else if ( e.target == containsObjBtn ) {
			showStatus ("Contains object: " + table.contains (new Employee( fName.getText() , lName.getText()) ));
		}
		else if ( e.target == clearBtn ) {
			table.clear();
			showStatus ("Clear: Table is now empty" );
		}
		else if ( e.target == listElemsBtn ) {
			StringBuffer buf = new StringBuffer();
			for ( Enumeration enum = table.elements(); enum.hasMoreElements(); )
				buf.append( enum.nextElement() ).append( '\n');
			display.setText( buf.toString() );
		}
		else if ( e.target == listKeysBtn ) {
			StringBuffer buf = new StringBuffer();
			for ( Enumeration enum = table.keys(); enum.hasMoreElements(); )
				buf.append( enum.nextElement() ).append( '\n');
			display.setText( buf.toString() );
		}
		return true;
	}
 }

class Employee {
	private String first, last;

	public Employee( String fName, String lName)
	{
		first = fName;
		last = lName;
	}

	public String toString() { return first + " " + last; }
}