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. List books"); System.out.println("6. Quit"); System.out.print("Choose an option (1-6): "); 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 addBook(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 removeBook(Scanner scan, BookCase list){ System.out.print("Enter the position of the book: "); int position = scan.nextInt(); position = position - 1; // Need to account for base-0 indexing 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 search(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 recommendBook(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 listBooks(BookCase list){ System.out.println(list.toString()); } /** * Controls the application */ public static void main(String[] args){ int choice = 0; int QUIT = 6; 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: addBook(scan, list); break; case 2: removeBook(scan, list); break; case 3: search(scan, list); break; case 4: recommendBook(list); break; case 5: listBooks(list); break; default: break; } System.out.println(); System.out.println(); // after the break, we continue here }while(choice != QUIT); System.out.println("Goodbye!"); } }