brainfrz

Untitled

Oct 21st, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.82 KB | None | 0 0
  1. /**************************************************************************************************
  2.  *  Program Name:     Programming Project 5.7: Bookshelf
  3.  *  Class Name:       pp5_7.Librarian
  4.  *  Author:           Terry Weiss
  5.  *  Date Written:     October 15, 2015
  6.  *  Program Description:
  7.  *     This class handles everything with the `Bookshelf`. It allows the User to store a book,
  8.  *  browse the list of books by various categories, view the details of a particular book and
  9.  *  take a book off the shelf.
  10.  **************************************************************************************************/
  11. package pp5_7;
  12.  
  13. //import pp5_7.Book;
  14. //import pp5_7.Bookshelf;
  15. //import Prompt;
  16.  
  17. /**
  18.  *  This class is the driver behind {@link pp5_7.Bookshelf}. It uses menus to allow the User to
  19.  *  store a book, browse the list of books by various categories, view the details of a particular
  20.  *  book and take a book off the shelf.
  21.  *
  22.  * @author Terry Weiss
  23.  * @see pp5_7.Book
  24.  * @see pp5_7.Bookshelf
  25.  */
  26. public class Librarian {
  27.     /*
  28.      * Definitions of all menu options for {@link #mainMenu}
  29.      */
  30.     private static enum Main_Options {
  31.         MAIN_OPTION_ADD_BOOK, MAIN_OPTION_REMOVE_BOOK, MAIN_OPTION_BROWSE, MAIN_OPTION_QUIT;
  32.  
  33.         /**
  34.          * Converts an int menu option to a Main_Options option. Since menus start at 1, all
  35.          * options are returned as 1 less to start at 0.
  36.          *
  37.          * @param option Integer option to be converted
  38.          * @return Converted value of Main_Options
  39.          */
  40.         public static Main_Options fromInteger( int option ) {
  41.             return Main_Options.values()[option - 1];
  42.         }
  43.     }
  44.  
  45.     /*
  46.      * Definitions of all menu options for {@link #browseMenu}
  47.      */
  48.     private static enum Browse_Options {
  49.         BROWSE_OPTION_BROWSE_ALL, BROWSE_OPTION_BROWSE_AUTHORS, BROWSE_OPTION_BROWSE_GENRES,
  50.         BROWSE_OPTION_BROWSE_SIZE, BROWSE_OPTION_CATEGORIZE_AUTHORS,
  51.         BROWSE_OPTION_CATEGORIZE_GENRES, BROWSE_OPTION_VIEW_BY_AUTHOR, BROWSE_OPTION_VIEW_BY_GENRE,
  52.         BROWSE_OPTION_VIEW_BOOK_DETAILS, BROWSE_OPTION_MAIN_MENU;
  53.  
  54.         /**
  55.          * Converts an int menu option to a Browse_Options option.  Since menus start at 1, all
  56.          * options are returned as 1 less to start at 0.
  57.          *
  58.          * @param option Integer option to be converted
  59.          * @return Converted value of Browse_Options
  60.          */
  61.         public static Browse_Options fromInteger( int option ) {
  62.             return Browse_Options.values()[option - 1];
  63.         }
  64.  
  65.     }
  66.  
  67.  
  68.     /*
  69.      *  {@link pp5_7.Bookshelf} instance that holds each {@link pp5_7.Book}
  70.      */
  71.     private static final Bookshelf shelf = new Bookshelf();
  72.  
  73.  
  74.  
  75.  
  76.     /*
  77.      * Creates the main menu prompt message and prompts the user for which option to proceed.
  78.      *
  79.      * @return Menu option selected
  80.      * @see pp5_7.Prompt
  81.      */
  82.     private static int mainMenu() {
  83.         String prompt = "\n"
  84.                 + "\t1. Add a book\n"
  85.                 + "\t2. Remove a book\n"
  86.                 + "\t3. Browse\n"
  87.                 + "\t4. Quit\n"
  88.                 + "Please select an option";
  89.  
  90.         return Prompt.intInRange(prompt, 1, Main_Options.MAIN_OPTION_QUIT.ordinal() + 1,
  91.                                     "Not a valid option.");
  92.     }
  93.  
  94.     /*
  95.      * Prompts the user for a {@link pp5_7.Book}'s details, creates the new Book and adds it
  96.      * to the {@link pp5_7.Bookshelf}.
  97.      *
  98.      * @see pp5_7.Bookshelf#addBook
  99.      */
  100.     private static void addBook() {
  101.         String title = Prompt.string("Enter title");
  102.         String author = Prompt.string("Enter author");
  103.         int pages = Prompt.wholeNumber("Enter number of pages");
  104.         String genre = Prompt.string("Enter genre");
  105.  
  106.         Book book = new Book(title, pages, author, genre);
  107.         shelf.addBook(book);
  108.         System.out.println("Added " + book + " to the shelf.");
  109.     }
  110.  
  111.     /*
  112.      * Removes a book from the bookshelf, or displays a message that the book doesn't exist.
  113.      */
  114.     private static void removeBook() {
  115.         String title = Prompt.string("Enter title");
  116.  
  117.         if (shelf.removeBook(title)) {
  118.             System.out.println("Removed " + title + " from the shelf.");
  119.         }
  120.         else {
  121.             System.out.println("That book doesn't exist.");
  122.         }
  123.     }
  124.  
  125.  
  126.     /*
  127.      * Creates the browse menu prompt message and prompts the user for which option to proceed.
  128.      *
  129.      * @return Menu option selected
  130.      * @see pp5_7.Prompt
  131.      */
  132.     private static int browseMenu() {
  133.         String prompt = "\n"
  134.                 + "\t1.  Browse all books\n"
  135.                 + "\t2.  Browse by authors\n"
  136.                 + "\t3.  Browse by genres\n"
  137.                 + "\t4.  Browse by size\n"
  138.                 + "\t5.  Categorize by author\n"
  139.                 + "\t6.  Categorize by genre\n"
  140.                 + "\t7.  View books by a certain author\n"
  141.                 + "\t8.  View books in a certain genre\n"
  142.                 + "\t9.  View book details\n"
  143.                 + "\t10. Return to main menu\n"
  144.                 + "Please select an option";
  145.  
  146.         return Prompt.intInRange(prompt, 1, Browse_Options.BROWSE_OPTION_MAIN_MENU.ordinal() + 1,
  147.                                     "Not a valid option.");
  148.     }
  149.  
  150.     /*
  151.      * Displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}, sorted by their title.
  152.      *
  153.      * @see pp5_7.Bookshelf#browse
  154.      */
  155.     private static void browseAll() {
  156.         System.out.println("\nAll books in " + shelf + ":");
  157.         System.out.print(shelf.browse());
  158.     }
  159.  
  160.     /*
  161.      * Displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}, sorted by their author.
  162.      *
  163.      * @see pp5_7.Bookshelf#browseBooksByAuthor
  164.      */
  165.     private static void browseAuthors() {
  166.         System.out.println("\nAll books in " + shelf + ":");
  167.         System.out.print(shelf.browseByAuthor());
  168.     }
  169.  
  170.     /*
  171.      * Displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}, sorted by their genre.
  172.      *
  173.      * @see pp5_7.Bookshelf#browseBooksByGenre
  174.      */
  175.     private static void browseGenres() {
  176.         System.out.println("\nAll books in " + shelf + ":");
  177.         System.out.print(shelf.browseByGenre());
  178.     }
  179.  
  180.     /*
  181.      * Displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}, sorted by their size.
  182.      *
  183.      * @see pp5_7.Bookshelf#browseBySize
  184.      */
  185.     private static void browseSize() {
  186.         System.out.println("\nAll books in " + shelf + ":");
  187.         System.out.print(shelf.browseBySize());
  188.     }
  189.  
  190.     /*
  191.      * Displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}, categorized and sorted
  192.      * by their author.
  193.      *
  194.      * @see pp5_7.Bookshelf#browseByCategory
  195.      */
  196.     private static void browseCategorizeAuthors() {
  197.         System.out.println("\nAll books in " + shelf + ":");
  198.         System.out.print(shelf.browseByCategory("author"));
  199.     }
  200.  
  201.     /*
  202.      * Displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}, categorized and sorted
  203.      * by their genre.
  204.      *
  205.      * @see pp5_7.Bookshelf#browseByCategory
  206.      */
  207.     private static void browseCategorizeGenres() {
  208.         System.out.println("\nAll books in " + shelf + ":");
  209.         System.out.print(shelf.browseByCategory("genre"));
  210.     }
  211.  
  212.     /*
  213.      * Prompts for an author and displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}
  214.      * written by that author, sorted by their title.
  215.      *
  216.      * @see pp5_7.Bookshelf#browseBooksOfAuthor
  217.      */
  218.     private static void browseBooksByAuthor() {
  219.         String author = Prompt.string("Pick an author");
  220.  
  221.         System.out.println("\nAll books by " + author + ":");
  222.         System.out.print(shelf.browseBooksOfAuthor(author));
  223.     }
  224.  
  225.     /*
  226.      * Prompts for a genre and displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}
  227.      * that are in that genre, sorted by their title.
  228.      *
  229.      * @see pp5_7.Bookshelf#browseBooksOfGenre
  230.      */
  231.     private static void browseBooksByGenre() {
  232.         String genre = Prompt.string("Pick a genre");
  233.  
  234.         System.out.println("\nAll " + genre + " books:");
  235.         System.out.print(shelf.browseBooksOfGenre(genre));
  236.     }
  237.  
  238.     /*
  239.      * Prompts for a title and displays the details of that {@link pp5_7.Book}
  240.      *
  241.      * @see pp5_7.Bookshelf#viewBook
  242.      */
  243.     private static void browseViewDetails() {
  244.         String title = Prompt.string("Enter a title");
  245.         System.out.println(shelf.viewBook(title));
  246.     }
  247.  
  248.  
  249.  
  250.     /**
  251.      * Runs the menus to allows the User to store a book, browse the list of books by various
  252.      * categories, view the details of a particular book, and take a book off the shelf.
  253.      *
  254.      * @param args Command line input isn't currently supported
  255.      * @throws pp5_7.Prompt.OptionOutOfBoundsException In case a menu choice isn't valid
  256.      * @see pp5_7.Book
  257.      * @see pp5_7.Bookshelf
  258.      */
  259.     public static void main( String args[] ) throws Prompt.OptionOutOfBoundsException {
  260.         int mainChoice, browseChoice;
  261.         do {
  262.             mainChoice = mainMenu();
  263.  
  264.             switch (Main_Options.fromInteger(mainChoice)) {
  265.                 case MAIN_OPTION_ADD_BOOK:
  266.                     addBook();
  267.                     break;
  268.                 case MAIN_OPTION_REMOVE_BOOK:
  269.                     removeBook();
  270.                     break;
  271.                 case MAIN_OPTION_BROWSE:
  272.                     do {
  273.                         browseChoice = browseMenu();
  274.  
  275.                         switch (Browse_Options.fromInteger(browseChoice)) {
  276.                             case BROWSE_OPTION_BROWSE_ALL:
  277.                                 browseAll();
  278.                                 break;
  279.                             case BROWSE_OPTION_BROWSE_AUTHORS:
  280.                                 browseAuthors();
  281.                                 break;
  282.                             case BROWSE_OPTION_BROWSE_GENRES:
  283.                                 browseGenres();
  284.                                 break;
  285.                             case BROWSE_OPTION_BROWSE_SIZE:
  286.                                 browseSize();
  287.                                 break;
  288.                             case BROWSE_OPTION_CATEGORIZE_AUTHORS:
  289.                                 browseCategorizeAuthors();
  290.                                 break;
  291.                             case BROWSE_OPTION_CATEGORIZE_GENRES:
  292.                                 browseCategorizeGenres();
  293.                                 break;
  294.                             case BROWSE_OPTION_VIEW_BY_AUTHOR:
  295.                                 browseBooksByAuthor();
  296.                                 break;
  297.                             case BROWSE_OPTION_VIEW_BY_GENRE:
  298.                                 browseBooksByGenre();
  299.                                 break;
  300.                             case BROWSE_OPTION_VIEW_BOOK_DETAILS:
  301.                                 browseViewDetails();
  302.                                 break;
  303.                             case BROWSE_OPTION_MAIN_MENU:
  304.                                 System.out.println("Returning to the main menu.");
  305.                                 break;
  306.                             default:
  307.                                 throw new Prompt.OptionOutOfBoundsException(
  308.                                                     "Browse menu option doesn't exist");
  309.                         }
  310.                     } while (browseChoice != Browse_Options.BROWSE_OPTION_MAIN_MENU.ordinal() + 1);
  311.                     break;
  312.                 case MAIN_OPTION_QUIT:
  313.                     System.out.println("Please come again!");
  314.                     break;
  315.                 default:
  316.                     throw new Prompt.OptionOutOfBoundsException("Main menu option doesn't exist");
  317.             }
  318.         } while (mainChoice != Main_Options.MAIN_OPTION_QUIT.ordinal() + 1);
  319.     }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment