import java.util.Scanner; /** * This class implements a virtual bookcase where users can store and * retrieve books. * * Illustrates: * - The use of private static methods * - The use of a switch statement * - The use of a do-while loop * * @author alchambers * @version sp16 */ public class BookController{ /** * Prints a menu of the options and returns the user's choice * @param scan A Scanner that allows for reading from the keyboard * @return An integer representing the user's choice */ private static int printMenuAndGetChoice(Scanner scan){ System.out.println(); System.out.println(); System.out.println("=== Bookcase ==="); System.out.println("1. Add a book"); System.out.println("2. Search for a book"); System.out.println("3. Get a random book"); System.out.println("4. List books"); System.out.println("5. Quit"); System.out.print("Choose an option (1-5): "); int choice = scan.nextInt(); scan.nextLine(); // scans the newline System.out.println(); return choice; } /** * Allows the user to add a book to the bookcase * @param scan A Scanner that allows for reading from the keyboard * @param list The book case */ private static void optionOne(Scanner scan, BookCase list){ System.out.print("Enter the title of the book: "); String title = scan.nextLine(); list.addBook(title); } /** * Allows the user to determine whether a book is in the bookcase * @param scan A Scanner that allows for reading from the keyboard * @param list The book case */ private static void optionTwo(Scanner scan, BookCase list){ System.out.print("Enter the title of the book: "); String title = scan.nextLine(); boolean isPresent = list.containsBook(title); if(isPresent){ System.out.println("The book is in the bookcase"); } else{ System.out.println("The book isn't in the bookcase"); } System.out.println(); System.out.println(); } /** * Recommends a random book to the user * @param list The book case */ private static void optionThree(BookCase list){ String title = list.getRandomBook(); System.out.println("How about reading " + title); } /** * Prints the contents of the book case * @param list The book case */ private static void optionFour(BookCase list){ System.out.println(list); } /** * Controls the application */ public static void main(String[] args){ int choice = 0; final int QUIT = 5; BookCase list = new BookCase(); Scanner scan = new Scanner(System.in); // Loop until the user quits do{ // Get the user's selection choice = printMenuAndGetChoice(scan); // Call the appropriate methods switch(choice){ case 1: optionOne(scan, list); break; case 2: optionTwo(scan, list); break; case 3: optionThree(list); break; case 4: optionFour(list); break; default: break; } }while(choice != QUIT); System.out.println("Goodbye!"); } }