// **************************************************************** // IntegerListTest.java // // Provide a menu-driven tester for the IntegerList class. // // **************************************************************** import java.io.*; public class IntegerListTest { static IntegerList list = new IntegerList(10); static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); //------------------------------------------------------- // Create a list, then repeatedly print the menu and do 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()); } } //------------------------------------------------------- // Do what the menu item calls for //------------------------------------------------------- public static void dispatch(int choice) throws IOException { int loc; switch(choice) { case 0: System.out.println("Bye!"); break; case 1: System.out.println("How big should the list be?"); int size = Integer.parseInt(keyboard.readLine()); list = new IntegerList(size); list.randomize(); break; case 2: list.selectionSort(); break; case 3: System.out.print("Enter the value to look for: "); loc = list.search(Integer.parseInt(keyboard.readLine())); if (loc != -1) System.out.println("Found at location " + loc); else System.out.println("Not in list"); break; case 4: list.print(); break; default: System.out.println("Sorry, invalid choice"); } } //------------------------------------------------------- // Print 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: Create a new list (** do this first!! **)"); System.out.println("2: Sort the list using selection sort"); System.out.println("3: Find an element in the list using sequential search"); System.out.println("4: Print the list"); System.out.print("\nEnter your choice: "); } }