// Name : Matthew Reeves // This is a program to test the methods in the linkedList class import java.util.Scanner; import ListPkg.LinkedList; import MenuPkg.Menu; import ExceptionPkg.EmptyListException; import java.util.InputMismatchException; public class LinkedListTest { //main method, includes the codes to send menu options to //Menu so menu can be displayed and inputs read/validated/accepted public static void main( String args[] ) { Scanner input=new Scanner(System.in); LinkedList linkedList=new LinkedList("testList"); String menuOpts[]={"Quit","Add an item to list - from FRONT","Add an item to list - from BACK","Remove an item from list - from FRONT","Remove an item from list - from BACK","Find and remove an item in the list","Find an Item in the list","Check the length of the list","Check if list is empty","Print list","Clear list"}; Menu displayMenu=new Menu(menuOpts); System.out.println("\nThank you for using the LinkedListTest Program!\n"); int choice; boolean continueLoop=true; //calls appropriate methods based on user's input choice do { System.out.println("\tPlease choose from the following options\n"); choice=displayMenu.runMenu(); int value; Integer wrappedValue; try { switch (choice) { case 1: System.out.print("Please enter the value to add: "); value=input.nextInt(); wrappedValue=value; linkedList.insertAtFront(wrappedValue); System.out.println("\nAdded to the list!"); linkedList.Print(); break; case 2: System.out.print("Please enter the value to add: "); value=input.nextInt(); wrappedValue=value; linkedList.insertAtBack(wrappedValue); System.out.println("\nAdded to the list!"); linkedList.Print(); break; case 3: if (linkedList.isEmpty()) { System.out.println("List is empty - value NOT removed\n"); } else { linkedList.removeFromFront(); System.out.println("Value removed from the list!"); linkedList.Print(); } break; case 4: if (linkedList.isEmpty()) { System.out.println("List is empty - value NOT removed\n"); } else { linkedList.removeFromBack(); System.out.println("Value removed from the list!"); linkedList.Print(); } break; case 5: System.out.print("Please enter the value to find and remove: "); value=input.nextInt(); wrappedValue=value; if (linkedList.isEmpty()) { System.out.println("\nList is empty - value NOT removed\n"); break; } if (linkedList.findAndRemove(wrappedValue)) { System.out.println("\nValue found in list - value has been removed"); } else { System.out.println("\nValue NOT found in list - value NOT removed"); } linkedList.Print(); break; case 6: System.out.print("Please enter the value to search for: "); value=input.nextInt(); wrappedValue=value; if (linkedList.findItem(wrappedValue)) { System.out.println("\nValue found in the list!"); } else { System.out.println("\nValue NOT found in the list!"); } linkedList.Print(); break; case 7: System.out.printf("Number of elements currently in the list: %d\n\n",linkedList.lengthIs()); break; case 8: if (linkedList.isEmpty()) { System.out.println("The list is empty\n"); break; } else { System.out.println("The list is NOT empty\n"); break; } case 9: linkedList.Print(); break; case 10: linkedList.Clear(); System.out.println("The list has been cleared!\n"); break; case 0: continueLoop=false; break; } } catch (InputMismatchException inputMismatchException) { System.err.printf("\nException: %s\n",inputMismatchException); System.out.println("\nYou must enter a valid integer value to add to the list, please try again.\n"); input.nextLine(); continue; } }while(continueLoop); } }