/**************************************************************************************************
* Program Name: Programming Project 5.7: Bookshelf
* Class Name: pp5_7.Librarian
* Author: Terry Weiss
* Date Written: October 15, 2015
* Program Description:
* This class handles everything with the `Bookshelf`. It allows the User to store a book,
* browse the list of books by various categories, view the details of a particular book and
* take a book off the shelf.
**************************************************************************************************/
package pp5_7;
//import pp5_7.Book;
//import pp5_7.Bookshelf;
//import Prompt;
/**
* This class is the driver behind {@link pp5_7.Bookshelf}. It uses menus to allow the User to
* store a book, browse the list of books by various categories, view the details of a particular
* book and take a book off the shelf.
*
* @author Terry Weiss
* @see pp5_7.Book
* @see pp5_7.Bookshelf
*/
public class Librarian {
/*
* Definitions of all menu options for {@link #mainMenu}
*/
private static enum Main_Options {
MAIN_OPTION_ADD_BOOK, MAIN_OPTION_REMOVE_BOOK, MAIN_OPTION_BROWSE, MAIN_OPTION_QUIT;
/**
* Converts an int menu option to a Main_Options option. Since menus start at 1, all
* options are returned as 1 less to start at 0.
*
* @param option Integer option to be converted
* @return Converted value of Main_Options
*/
public static Main_Options fromInteger( int option ) {
return Main_Options.values()[option - 1];
}
}
/*
* Definitions of all menu options for {@link #browseMenu}
*/
private static enum Browse_Options {
BROWSE_OPTION_BROWSE_ALL, BROWSE_OPTION_BROWSE_AUTHORS, BROWSE_OPTION_BROWSE_GENRES,
BROWSE_OPTION_BROWSE_SIZE, BROWSE_OPTION_CATEGORIZE_AUTHORS,
BROWSE_OPTION_CATEGORIZE_GENRES, BROWSE_OPTION_VIEW_BY_AUTHOR, BROWSE_OPTION_VIEW_BY_GENRE,
BROWSE_OPTION_VIEW_BOOK_DETAILS, BROWSE_OPTION_MAIN_MENU;
/**
* Converts an int menu option to a Browse_Options option. Since menus start at 1, all
* options are returned as 1 less to start at 0.
*
* @param option Integer option to be converted
* @return Converted value of Browse_Options
*/
public static Browse_Options fromInteger( int option ) {
return Browse_Options.values()[option - 1];
}
}
/*
* {@link pp5_7.Bookshelf} instance that holds each {@link pp5_7.Book}
*/
private static final Bookshelf shelf = new Bookshelf();
/*
* Creates the main menu prompt message and prompts the user for which option to proceed.
*
* @return Menu option selected
* @see pp5_7.Prompt
*/
private static int mainMenu() {
String prompt = "\n"
+ "\t1. Add a book\n"
+ "\t2. Remove a book\n"
+ "\t3. Browse\n"
+ "\t4. Quit\n"
+ "Please select an option";
return Prompt.intInRange(prompt, 1, Main_Options.MAIN_OPTION_QUIT.ordinal() + 1,
"Not a valid option.");
}
/*
* Prompts the user for a {@link pp5_7.Book}'s details, creates the new Book and adds it
* to the {@link pp5_7.Bookshelf}.
*
* @see pp5_7.Bookshelf#addBook
*/
private static void addBook() {
String title = Prompt.string("Enter title");
String author = Prompt.string("Enter author");
int pages = Prompt.wholeNumber("Enter number of pages");
String genre = Prompt.string("Enter genre");
Book book = new Book(title, pages, author, genre);
shelf.addBook(book);
System.out.println("Added " + book + " to the shelf.");
}
/*
* Removes a book from the bookshelf, or displays a message that the book doesn't exist.
*/
private static void removeBook() {
String title = Prompt.string("Enter title");
if (shelf.removeBook(title)) {
System.out.println("Removed " + title + " from the shelf.");
}
else {
System.out.println("That book doesn't exist.");
}
}
/*
* Creates the browse menu prompt message and prompts the user for which option to proceed.
*
* @return Menu option selected
* @see pp5_7.Prompt
*/
private static int browseMenu() {
String prompt = "\n"
+ "\t1. Browse all books\n"
+ "\t2. Browse by authors\n"
+ "\t3. Browse by genres\n"
+ "\t4. Browse by size\n"
+ "\t5. Categorize by author\n"
+ "\t6. Categorize by genre\n"
+ "\t7. View books by a certain author\n"
+ "\t8. View books in a certain genre\n"
+ "\t9. View book details\n"
+ "\t10. Return to main menu\n"
+ "Please select an option";
return Prompt.intInRange(prompt, 1, Browse_Options.BROWSE_OPTION_MAIN_MENU.ordinal() + 1,
"Not a valid option.");
}
/*
* Displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}, sorted by their title.
*
* @see pp5_7.Bookshelf#browse
*/
private static void browseAll() {
System.out.println("\nAll books in " + shelf + ":");
System.out.print(shelf.browse());
}
/*
* Displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}, sorted by their author.
*
* @see pp5_7.Bookshelf#browseBooksByAuthor
*/
private static void browseAuthors() {
System.out.println("\nAll books in " + shelf + ":");
System.out.print(shelf.browseByAuthor());
}
/*
* Displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}, sorted by their genre.
*
* @see pp5_7.Bookshelf#browseBooksByGenre
*/
private static void browseGenres() {
System.out.println("\nAll books in " + shelf + ":");
System.out.print(shelf.browseByGenre());
}
/*
* Displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}, sorted by their size.
*
* @see pp5_7.Bookshelf#browseBySize
*/
private static void browseSize() {
System.out.println("\nAll books in " + shelf + ":");
System.out.print(shelf.browseBySize());
}
/*
* Displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}, categorized and sorted
* by their author.
*
* @see pp5_7.Bookshelf#browseByCategory
*/
private static void browseCategorizeAuthors() {
System.out.println("\nAll books in " + shelf + ":");
System.out.print(shelf.browseByCategory("author"));
}
/*
* Displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}, categorized and sorted
* by their genre.
*
* @see pp5_7.Bookshelf#browseByCategory
*/
private static void browseCategorizeGenres() {
System.out.println("\nAll books in " + shelf + ":");
System.out.print(shelf.browseByCategory("genre"));
}
/*
* Prompts for an author and displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}
* written by that author, sorted by their title.
*
* @see pp5_7.Bookshelf#browseBooksOfAuthor
*/
private static void browseBooksByAuthor() {
String author = Prompt.string("Pick an author");
System.out.println("\nAll books by " + author + ":");
System.out.print(shelf.browseBooksOfAuthor(author));
}
/*
* Prompts for a genre and displays all {@link pp5_7.Book}s on the {@link pp5_7.Bookshelf}
* that are in that genre, sorted by their title.
*
* @see pp5_7.Bookshelf#browseBooksOfGenre
*/
private static void browseBooksByGenre() {
String genre = Prompt.string("Pick a genre");
System.out.println("\nAll " + genre + " books:");
System.out.print(shelf.browseBooksOfGenre(genre));
}
/*
* Prompts for a title and displays the details of that {@link pp5_7.Book}
*
* @see pp5_7.Bookshelf#viewBook
*/
private static void browseViewDetails() {
String title = Prompt.string("Enter a title");
System.out.println(shelf.viewBook(title));
}
/**
* Runs the menus to allows the User to store a book, browse the list of books by various
* categories, view the details of a particular book, and take a book off the shelf.
*
* @param args Command line input isn't currently supported
* @throws pp5_7.Prompt.OptionOutOfBoundsException In case a menu choice isn't valid
* @see pp5_7.Book
* @see pp5_7.Bookshelf
*/
public static void main( String args[] ) throws Prompt.OptionOutOfBoundsException {
int mainChoice, browseChoice;
do {
mainChoice = mainMenu();
switch (Main_Options.fromInteger(mainChoice)) {
case MAIN_OPTION_ADD_BOOK:
addBook();
break;
case MAIN_OPTION_REMOVE_BOOK:
removeBook();
break;
case MAIN_OPTION_BROWSE:
do {
browseChoice = browseMenu();
switch (Browse_Options.fromInteger(browseChoice)) {
case BROWSE_OPTION_BROWSE_ALL:
browseAll();
break;
case BROWSE_OPTION_BROWSE_AUTHORS:
browseAuthors();
break;
case BROWSE_OPTION_BROWSE_GENRES:
browseGenres();
break;
case BROWSE_OPTION_BROWSE_SIZE:
browseSize();
break;
case BROWSE_OPTION_CATEGORIZE_AUTHORS:
browseCategorizeAuthors();
break;
case BROWSE_OPTION_CATEGORIZE_GENRES:
browseCategorizeGenres();
break;
case BROWSE_OPTION_VIEW_BY_AUTHOR:
browseBooksByAuthor();
break;
case BROWSE_OPTION_VIEW_BY_GENRE:
browseBooksByGenre();
break;
case BROWSE_OPTION_VIEW_BOOK_DETAILS:
browseViewDetails();
break;
case BROWSE_OPTION_MAIN_MENU:
System.out.println("Returning to the main menu.");
break;
default:
throw new Prompt.OptionOutOfBoundsException(
"Browse menu option doesn't exist");
}
} while (browseChoice != Browse_Options.BROWSE_OPTION_MAIN_MENU.ordinal() + 1);
break;
case MAIN_OPTION_QUIT:
System.out.println("Please come again!");
break;
default:
throw new Prompt.OptionOutOfBoundsException("Main menu option doesn't exist");
}
} while (mainChoice != Main_Options.MAIN_OPTION_QUIT.ordinal() + 1);
}
}
Comments