// Name : Matthew Reeves // This is a menu-driven test program for UnsortedBookList class //Menu driven test program for the UnsortedBookList class import java.util.Scanner; import java.util.InputMismatchException; public class UnsortedBookListTest { //main method for test class public static void main (String args[]) { boolean continueLoop=true; Scanner input=new Scanner(System.in); UnsortedBookList bookList=new UnsortedBookList(); int size=0; //accepts, validates, and processes user input for list size do { try { System.out.print("\nEnter the list size (>0): "); size=input.nextInt(); bookList=new UnsortedBookList(size); } catch (InputMismatchException inputMismatchException) { System.err.printf("\nException: %s\n\n",inputMismatchException); System.out.println("You must enter a valid integer value (>0) for the list size, please try again."); input.nextLine(); continue; } catch (Exception exception) { System.err.printf ("\nException: %s\n", exception); System.out.println ("List size MUST be greater than 0"); continue; } continueLoop=false; } while (continueLoop); System.out.println("\nThank you for using the UnsortedBookList Test Program!"); int choice; continueLoop=true; //calls appropriate methods based on user's input choice do { choice=menuOpt(); switch (choice) { case 1: bookList.addItem(bookEntryPoint()); System.out.println("\n\tThe book has been added to the list!"); break; case 2: if (bookList.removeItem(bookEntryPoint())==true) { System.out.println("\n\tThis book has been removed from the list!"); } else { System.out.println("\n\tUnable to remove, book not found in the list!"); } break; case 3: if (bookList.findItem(bookEntryPoint())==true) { System.out.println("\n\tThis book is in the list!"); } else { System.out.println("\n\tThis book IS NOT in the list!"); } break; case 4: if (bookList.isEmpty()==true) { System.out.println("The list IS empty!"); } else { System.out.println("The list IS NOT empty!"); } break; case 5: System.out.printf("The current number of elements in the list = %d\n",bookList.lengthIs()); break; case 6: if (bookList.lengthIs()<=0) { System.out.println("The current list is:\n\n***List is Empty***"); } else { System.out.printf("The current list is:\n\n%s",bookList.toString()); } break; case 7: bookList.clear(); System.out.println("The list is now empty."); break; case 0: continueLoop=false; break; } }while (continueLoop); } //generates the prompt for a user to enter a new book private static Book bookEntryPoint() { Scanner input=new Scanner(System.in); //initializes local variables for method String titl; String auth; String publ; int pyear; String isbn; Book bookEntry=new Book(); boolean continueLoop=true; //validates input type for user input do { try { //prompts for and stores attribute info from user System.out.print("\tEnter the book's title: "); titl=input.nextLine(); System.out.print("\tEnter the book's author: "); auth=input.nextLine(); System.out.print("\tEnter the book's publisher: "); publ=input.nextLine(); System.out.print("\tEnter the book's publication year: "); pyear=input.nextInt(); input.nextLine(); System.out.print("\tEnter the book's ISBN number: "); isbn=input.nextLine(); //creates Book object using user input bookEntry=new Book(titl,auth,publ,pyear,isbn); } catch (InputMismatchException inputMismatchException) { System.err.printf("\nException: %s\n\n",inputMismatchException); System.out.println("You must enter a valid integer value for the publication year, please try again.\n"); input.nextLine(); continue; } continueLoop=false; } while (continueLoop); return bookEntry; } //prompts user for a menu choice and gives user available options private static int menuOpt() { boolean continueLoop=true; int choice=0; Scanner input=new Scanner(System.in); do { try { System.out.println("\nChoose from the following options: "); System.out.println("\t1\tAdd an item to the list "); System.out.println("\t2\tRemove an item from the list "); System.out.println("\t3\tFind an item in the list "); System.out.println("\t4\tDetermine if the list is empty "); System.out.println("\t5\tGet the length of the list "); System.out.println("\t6\tPrint the list "); System.out.println("\t7\tClear the list "); System.out.println("\t0\tQuit "); System.out.print("\tOption: "); choice=input.nextInt(); System.out.println(); if (choice<0||choice>7) { System.out.println("\nYou must enter an integer 1-7, or 0 to exit. Please try again."); continue; } else { continueLoop=false; } } catch (InputMismatchException inputMismatchException) { System.err.printf("\nException: %s\n",inputMismatchException); System.out.println("\nYou must enter an integer 1-7, or 0 to exit. Please try again."); input.nextLine(); } }while (continueLoop); return choice; } }