/**
*This class tests a hash table.
*
*@author Jonathan Brophy
*@version 103Project03
*/
import java.util.Scanner;
import java.io.*;
public class HTDriver
{
public static void main(String[] args)
{
/* Take student records taken from an input file */
Scanner scanner = new Scanner(System.in);
System.out.print("Enter input filename: ");
String filename = scanner.next();
try
{
File file = new File(filename);
Scanner fileScanner = new Scanner(file);
int collectionSize = fileScanner.nextInt();
fileScanner.nextLine(); //Set scanner to the next line
HashTable studentTable = new HashTable(collectionSize);
for (int i = 0; i < collectionSize; i++)
{
String input = fileScanner.nextLine();
Scanner stringScanner = new Scanner(input);
//Long Value
if (stringScanner.hasNextLong())
{
Long id = stringScanner.nextLong();
//Positive Number
if (id > 0)
{
if (stringScanner.hasNext())
{
String lastName = stringScanner.next();
//Not more than 2 values on a line
if (!stringScanner.hasNext())
{
Student student = new Student(id, lastName);
studentTable.insert(student);
}
}
}
}
}
studentTable.printTable();
studentTable.outputData();
/* Menu Operations */
displayMenu();
while (true)
{
System.out.print("Enter a letter: ");
String choice = scanner.next();
scanner.nextLine(); //Clear Scanner Buffer
if (choice.equals("q")) //Quit
{
System.out.println("\nThank you for using this program!!");
break;
}
else if (choice.equals("a")) //Add
{
System.out.println("Enter student info, id and last name " +
"on one line separated by spaces:");
Scanner stringScanner = new Scanner(scanner.nextLine());
if (stringScanner.hasNextLong())
{
Long id = stringScanner.nextLong();
if (id > 0)
{
if (stringScanner.hasNext())
{
String lastName = stringScanner.next();
if (!stringScanner.hasNext())
{
Student student = new Student(id, lastName);
if (studentTable.find(student) == null)
{
studentTable.insert(student);
System.out.println("Student Added!");
}
else
{
System.out.println("Student already added");
}
}
else
{
System.out.println("Invalid Input");
}
}
else
{
System.out.println("Invalid Input");
}
}
else
{
System.out.println("Invalid Input");
}
}
else
{
System.out.println("Invalid Input");
}
}
else if (choice.equals("d")) //Delete
{
System.out.print("Enter key of student to be deleted: ");
if (scanner.hasNextLong())
{
Long value = scanner.nextLong();
Student student = new Student(value, "Dummy");
if (studentTable.find(student) != null)
{
System.out.println(studentTable.find(student).toString() +
" has been deleted");
studentTable.delete(student);
}
else
{
System.out.println("Student could not be found");
}
}
else
{
System.out.println("Invalid Input");
scanner.nextLine(); //Clear scanner buffer
}
}
else if (choice.equals("f")) //Find
{
System.out.print("Enter the key of student: ");
if (scanner.hasNextLong())
{
Long id = scanner.nextLong();
Student student = new Student(id, "Dummy");
if (studentTable.find(student) != null)
{
System.out.println("The student has been found! " +
studentTable.find(student));
}
else
{
System.out.println("The student has not been found");
}
}
else
{
System.out.println("Invalid Input");
scanner.nextLine(); //Clear scanner buffer
}
}
else if (choice.equals("n")) //Number of elements in collection
{
System.out.println("Number of elements: " +
studentTable.elementCount());
}
else if (choice.equals("e")) //Check Empty
{
if (studentTable.isEmpty())
{
System.out.println("The table is empty");
}
else
{
System.out.println("The table is not empty");
}
}
else if (choice.equals("k")) //Empty the table
{
studentTable.makeEmpty();
System.out.println("Emptying the table...Complete");
}
else if (choice.equals("p")) //Print entire table
{
studentTable.printTable();
}
else if (choice.equals("o")) //Print data output
{
studentTable.outputData();
}
}
}
catch (FileNotFoundException exc)
{
exc.printStackTrace();
}
}
/**
*This method prints out a menu to interact with the user.
*/
private static void displayMenu()
{
System.out.println("\nEnter a provided letter below:\n");
System.out.println(" a - add the element");
System.out.println(" d - delete the element");
System.out.println(" f - find and retrieve the element");
System.out.println(" n - get the number of elements in the collection");
System.out.println(" e - check if the collection is empty");
System.out.println(" k - make the hash table empty");
System.out.println(" p - print the content of the hash table");
System.out.println(" o - outputs the elements of the collection");
System.out.println(" q - Quit the program\n");
}
}