// ************************************************************* // IntListTest.java // // Driver to test IntList methods. // ************************************************************* import java.io.*; public class IntListTest { private static IntList list = new IntList(); private static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); //---------------------------------------------------------------- // Creates a list, then repeatedly prints the menu and does what // the user asks until they quit. //---------------------------------------------------------------- public static void main(String[] args) throws IOException { printMenu(); int choice = Integer.parseInt(keyboard.readLine()); while (choice != 0) { dispatch(choice); printMenu(); choice = Integer.parseInt(keyboard.readLine()); } } //---------------------------------------- // Does what the menu item calls for. //---------------------------------------- public static void dispatch(int choice) throws IOException { int newVal; switch(choice) { case 0: System.out.println("Bye!"); break; case 1: //add to front System.out.print("\nEnter integer to add to front: "); newVal = Integer.parseInt(keyboard.readLine()); list.addToFront(newVal); list.print(); break; case 2: //add to end System.out.print("\nEnter integer to add to end: "); newVal = Integer.parseInt(keyboard.readLine()); list.addToEnd(newVal); list.print(); break; case 3: //remove first element list.removeFirst(); System.out.println("\nThe first integer in the list has been removed."); list.print(); break; case 4: //print list.print(); break; default: System.out.println("\nSorry, invalid choice"); } } //---------------------------------------- // Prints the user's choices //---------------------------------------- public static void printMenu() { System.out.println("\n Menu "); System.out.println(" ===="); System.out.println("0: Quit"); System.out.println("1: Add an integer to the front of the list"); System.out.println("2: Add an integer to the end of the list"); System.out.println("3: Remove an integer from the front of the list"); System.out.println("4: Print the list"); System.out.print("\nEnter your choice: "); } }