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. Remove a book"); System.out.println("3. Search for a book"); System.out.println("4. Get a random book"); System.out.println("5. Promote a book"); System.out.println("6. List books"); System.out.println("7. Quit"); System.out.print("Choose an option (1-7): "); int choice = scan.nextInt(); scan.nextLine(); // scans the newline 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(); System.out.print("Enter the author of the book: "); String author = scan.nextLine(); list.addBook(title, author); } /** * Allows a user to remove a book from the bookcase * */ private static void optionTwo(Scanner scan, BookCase list){ System.out.print("Enter the position of the book: "); int position = scan.nextInt(); scan.nextLine(); // clear the list.removeBook(position); } /** * 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 optionThree(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("Yes, the bookcase contains this book."); } else{ System.out.println("No, the bookcase doesn't contain this book."); } } /** * Recommends a random book to the user * @param list The book case */ private static void optionFour(BookCase list){ String title = list.getRandomBook(); System.out.println("How about reading " + title); } /** * Promote a book */ private static void optionFive(Scanner scan, BookCase list){ System.out.print("Enter the position of the book: "); int position = scan.nextInt(); list.promoteBook(position); System.out.println("\nHere's the new list:"); System.out.println(list.toString()); } /** * Prints the contents of the book case * @param list The book case */ private static void optionSix(BookCase list){ System.out.println(list.toString()); } /** * Controls the application */ public static void main(String[] args){ int choice = 0; final int QUIT = 7; 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: // Add a book optionOne(scan, list); break; case 2: // Remove a book optionTwo(scan, list); break; case 3: // Search for a book optionThree(scan, list); break; case 4: // Get a random book optionFour(list); break; case 5: // Promote a book optionFive(scan, list); break; case 6: // List books optionSix(list); break; default: break; } System.out.println(); System.out.println(); // after the break, we continue here }while(choice != QUIT); System.out.println("Goodbye!"); } }