Computer Science 241
Hash Table Example

/* HashInterface contains the hash table methods, add, find and delete. */

package hash;

public interface HashInterface
{
     public void addToTable (HashNode newNode);
     public HashNode findNode (String keyId);
     public boolean deleteFromTable (String keyId);
} // HashInterface

/* Hash Node class defines a node with two fields, one for an id and the other for a name. */

package hash;
import java.io.*;

public class HashNode
{
     protected String id, name;

     public HashNode (String id, String name)
     {
          this.id = id;
          this.name = name;
     } // constructor
 
     public String getId () {return id;}
     protected void setId (String id) {this.id = id; }

     public void outputNode (PrintStream outfile)
     {
          outfile.println ("ID: " + id);
          outfile.println ("Name: " + name);
     } // method outputNode
} //  class HashNode

/* Hash Table class implements the HashInterface.  It has methods for adding, finding and deleting nodes. */

package hash;
import java.io.*;

public class HashTable implements HashInterface
{
     // Fill in the methods here.
} // class HashTable
 

/* HashTableManager class

 This class uses HashTable and Node in order to read data from either a file or the keyboard, fill the list, find an ID in the list, add to the list, and store the data back into a file. */
 
package hash;

import java.io.*;
import java.text.*;

public class HashTableManager
{
     protected HashTable hashTable;
     protected int numberOfNodes;
 
     public HashTableManager ()
     {
          hashTable = new HashTable ();
          numberOfNodes = 0;
     } // constructor
 
     // Method to read data from a file into the list.
     public void fillTable ()
     {
          String id, name;
          try
          {
               System.out.print ("File Name: ");
               String fileName = Reader.readString ();
               BufferedReader infile = new BufferedReader (new InputStreamReader (new FileInputStream (fileName)));
               HashNode nextNode;
               id = infile.readLine ();
               while (id != null)
               {
                    name = infile.readLine ();
                    nextNode = new HashNode (id, name);
                    hashTable.addToTable (nextNode);
                    numberOfNodes ++;
                    id = infile.readLine ();
               } // while
               infile.close ();
          } catch (IOException e) {System.out.println ("File not found");}
     } // method fillTable
 
     // Method to add a node to the list.
     public void addNode ()
     {
          System.out.print ("ID of product to add: ");
          String id = Reader.readString ();
          System.out.print ("Name of product to add: ");
          String name = Reader.readString ();
          HashNode newNode = new HashNode (id, name);
          hashTable.addToTable (newNode);
          numberOfNodes ++;
     } // method addNode
 
     // Method to find a node in the list.
     public void findNode ()
     {
          System.out.print ("ID to Find: ");
          String id = Reader.readString ();
          HashNode foundNode = hashTable.findNode (id);
          if (foundNode == null) System.out.println ("ID not found.");
          else  foundNode.outputNode (System.out);
     } // method findNode
 
     // Method to delete a node.
     public void deleteNode ()
     {
          System.out.print ("ID to Remove: ");
          String id = Reader.readString ();
          boolean deleted = hashTable.deleteFromTable (id);
          if (deleted)
          {
               System.out.println ("ID removed from list.");
               numberOfNodes --;
          }
          else  System.out.println ("ID not in list.");
     } // method deleteNode

     // Method to display the entire list on the screen.
     public void displayTable ()
     {
          hashTable.outputTable (System.out);
     } // method displayList
 
     // Method to store the list in file on disk.
     public void storeList ()
     {
          try
          {
               System.out.print ("Output File Name: ");
               String outFileName = Reader.readString ();
               PrintStream outfile = new PrintStream  (new FileOutputStream (outFileName));
               hashTable.outputTable (outfile);
               outfile.close ();
          } catch (IOException e)  {System.out.println ("File name not given.");}
     } // method storeList
} // class ListManager

/* TestHashTable class
 
 This class reads employee data from a file and stores it in a hash table.  It then displays the data on the screen, finds an ID, adds a node, deletes a node, and then stores the data in a second file.
*/

import hash.HashTable;
import hash.HashNode;
import hash.HashTableManager;

public class HashTest
{
     public static void main (String [] args)
     {
          HashTableManager hashTableManager = new HashTableManager ();
          hashTableManager.fillTable ();
          hashTableManager.displayTable ();
          hashTableManager.findNode ();
          hashTableManager.addNode ();
          hashTableManager.deleteNode ();
          hashTableManager.storeList ();
     }
} // class HashTest

 
/* The Reader class creates a package with methods for reading doubles, integers, strings, characters and formatting doubles.*/
package hash;
import java.io.*;
import java.text.*;

class Reader
{
     static BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));

     /* Reads a string from the keyboard and returns it to the calling method.*/
     public static String readString ()
     {
          String string;
          try
          { string = stdin.readLine ();
          } catch (IOException e) { string = "";}
          return string;
     } // method readString

     /* Reads a double from the keyboard and returns it to the calling method. If the entry is not a valid number, the method returns 0. */
     public static double readDouble ( )
     {
          double number;
          try
          { number = Double.parseDouble (stdin.readLine ());
          } catch (IOException e) {number = 0;}
          catch (NumberFormatException x) { number = 0;}
          return number;
     } // method readDouble
 
     // Formats a double for string output with two decimal places.
     public static String decimals (double number)
     {
          DecimalFormat decFor = new DecimalFormat ( );
          decFor.setMaximumFractionDigits (2);
          decFor.setMinimumFractionDigits (2);
          return decFor.format (number);
     } // method decimals
 
     /* Reads an integer from the keyboard and returns it to the calling method. If the entry is not a valid number, the method returns 0. */
     public static int readInteger ()
     {
          int number;
          try
          { number = Integer.parseInt (stdin.readLine ());
          } catch (IOException e) {number = 0;}
          catch (NumberFormatException x) { number = 0;}
          return number;
      } // method readInteger;
} // class Reader