dsa

Data Structures & Algorithms - Spring 2018
Log | Files | Refs | README

Driver.java (3211B)


      1 package lab12;
      2 import java.util.Scanner;
      3 
      4 public class Driver {
      5     Scanner in = new Scanner(System.in);
      6     public static void main(String[] args) {
      7         int userSelection;
      8         Driver ui = new Driver();
      9         HashTable<String, String> table = new HashTable<>();
     10 
     11         ui.listOptions();
     12 
     13         do {
     14             userSelection = ui.menuSelection();
     15             System.out.println(userSelection);
     16 
     17             switch (userSelection) {
     18                 case 1:
     19                     ui.insert(table);
     20                     break;
     21                 case 2:
     22                     ui.delete(table);
     23                     break;
     24                 case 3:
     25                     ui.retrieve(table);
     26                     break;
     27                 case 4:
     28                     ui.displayHashIndex(table);
     29                     break;
     30                 case 5:
     31                     ui.exitProgram();
     32                     break;
     33             }
     34         } while (userSelection !=5);
     35     }
     36 
     37     public void insert(HashTable<String, String> table) {
     38         String key, value;
     39         System.out.print("Enter Key ");
     40         key = in.next().trim();
     41         System.out.println(key);
     42         System.out.print("Enter Value ");
     43         value = in.next().trim();
     44         System.out.println(value);
     45         table.tableInsert(key, value);
     46     }
     47 
     48     public void retrieve(HashTable<String, String> table) {
     49         String key;
     50         String result;
     51         System.out.print("Enter key ");
     52         key = in.next().trim();
     53         System.out.println(key);
     54 
     55         result = table.tableRetrieve(key);
     56         if (result != null) {
     57             System.out.println("Key " + key + " has value of " + result);
     58         }
     59         else {
     60             System.out.println("Key not found!");
     61         }
     62 
     63     }
     64 
     65     public void delete(HashTable<String, String> table) {
     66         String key;
     67         System.out.print("Enter Key");
     68         key = in.next().trim();
     69         System.out.println(key);
     70 
     71         if (table.tableDelete(key)) {
     72             System.out.println("Key " + key + " has been deleted");
     73         }
     74         else {
     75             System.out.println("Key not found in table");
     76         }
     77     }
     78 
     79     public void displayHashIndex(HashTable<String, String> table) {
     80         String key;
     81 
     82         System.out.print("Enter key ");
     83         key = in.next().trim();
     84         System.out.println(key);
     85 
     86         System.out.println("Hash index of key " + key + " is " + table.hashIndex(key));
     87     }
     88 
     89     public int menuSelection() {
     90         System.out.print("Make your menu selection now: ");
     91         return Integer.parseInt(in.next().trim());
     92     }
     93 
     94     public void listOptions(){
     95         System.out.println("Select from the following menu:");
     96         System.out.println("    1. Insert a symbol key with an associated value in the table");
     97         System.out.println("    2. Delete symbol from the table");
     98         System.out.println("    3. Retrieve and display the value associated with a symbol key in the table");
     99         System.out.println("    4. Display the hash index of a symbol key");
    100         System.out.println("    5. Exit Program");
    101     }
    102 
    103     public void exitProgram(){
    104         System.out.println("Exiting program...Good Bye");
    105         System.exit(0);
    106     }
    107 }