/************************************************************************************************** * Program Name: Programming Project 5.7: Bookshelf * Class Name: pp5_7.Bookshelf * Author: Terry Weiss * Date Written: October 15, 2015 * Program Description: * This class contains instance data for a bookshelf that is full of `Book`s. It allows the * User to store a book, browse the list of books, view the details of a particular book and * take a book off the shelf. **************************************************************************************************/ package pp5_7; //import pp5_7.Book; import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; /** * * @author Terry Weiss * @see pp5_7.Book * @see pp5_7.Librarian */ public class Bookshelf { private final String DEFAULT_NAME = "Bookshelf"; private ArrayList bookList = new ArrayList(); private ArrayList genreList = new ArrayList(); private ArrayList authorList = new ArrayList(); private final String name; Bookshelf() { name = DEFAULT_NAME; } Bookshelf( String name ) { this.name = name; } public int getNumberOfBooks() { return bookList.size(); } public String getName() { return name; } Book addBook( Book book ) { bookList.add(book); String genre = book.getGenre(); if (!genreList.contains(genre)) { genreList.add(genre); } String author = book.getAuthor(); if (!authorList.contains(author)) { authorList.add(author); } return book; } boolean removeBook( Book book ) { // If it's the only book of that genre or author, remove the genre/author from its list String genre = book.getGenre(); if (numBooksWithGenre(genre) == 1) { genreList.remove(genre); } String author = book.getAuthor(); if (numBooksWithAuthor(author) == 1) { authorList.remove(author); } return bookList.remove(book); } Book removeBook( int position ) { String genre = bookList.get(position).getGenre(); if (numBooksWithGenre(genre) == 1) { genreList.remove(genre); } String author = bookList.get(position).getAuthor(); if (numBooksWithGenre(genre) == 1) { authorList.remove(author); } return bookList.remove(position); } boolean removeBook( String title ) { if (numBooksWithTitle(title) == 1) { removeBook(findBook(title)); return true; } else { return false; } } String viewBook( int position ) { String ret; if (position < 0 || position >= bookList.size()) { ret = "That book isn't on the shelf."; } else { ret = bookList.get(position).displayDetails(); } return ret; } String viewBook( String title ) { String details = ""; for (Book currentBook : bookList) { if (currentBook.getTitle().equalsIgnoreCase(title)) { if (details.length() > 0) { // Separate each entry by a blank line in cases details += "\n\n"; // of duplicate titles } details += currentBook.displayDetails(); } } if (details.length() == 0) { details = title + " isn't on the shelf."; } return details; } public void sortBooksByTitle() { Collections.sort(bookList, Book.sortByTitle); } /** * Sort the Books by Author first and then by Title */ public void sortBooksByAuthor() { sortBooksByTitle(); Collections.sort(bookList, Book.sortByAuthor); } /** * Sort the Books by Genre first and then by Title */ public void sortBooksByGenre() { sortBooksByTitle(); Collections.sort(bookList, Book.sortByGenre); } /** * Sort the Books by size first and then by Title */ public void sortBooksBySize() { sortBooksByTitle(); Collections.sort(bookList); } /** * Returns the index of the first occurrence of the Book in this list, or -1 if this list * does not contain the Book. * * @param book Book object to search for * @return the index of the first Book, or -1 if it's not there */ public int findBook( Book book ) { return bookList.indexOf(book); } /** * Returns the index of the first occurrence of the Book of the same title in this list, * or -1 if this list does not contain the Book. * * @param title Title of the book to search for * @return the index of the first Book, or -1 if it's not there */ public int findBook( String title ) { for (int currentBook = 0; currentBook < bookList.size(); currentBook++) { String currentTitle = bookList.get(currentBook).getTitle(); if (currentTitle.equalsIgnoreCase(title)) { return currentBook; } } return -1; } public int numBooksWithGenre( String genre ) { int books = 0; for (Book book : bookList ) { if (book.getGenre().equalsIgnoreCase(genre)) { books++; } } return books; } public int numBooksWithAuthor( String author ) { int books = 0; for (Book book : bookList ) { if (book.getAuthor().equalsIgnoreCase(author)) { books++; } } return books; } public int numBooksWithTitle( String title ) { int books = 0; for (Book book : bookList ) { if (book.getTitle().equalsIgnoreCase(title)) { books++; } } return books; } public String browse() { String list = ""; for (Book currentBook : bookList ) { list += currentBook + "\n"; } return list; } public String browseByTitle() { sortBooksByTitle(); return browse(); } public String browseByAuthor() { sortBooksByAuthor(); return browse(); } public String browseByGenre() { sortBooksByGenre(); return browse(); } public String browseBySize() { sortBooksBySize(); return browse(); } public String browseBooksOfGenre( String genre ) { String list = ""; // Make the genre Capitalized to match format of genres and be case-insensitive genre = genre.substring(0, 1).toUpperCase() + genre.substring(1).toLowerCase(); if (genreList.indexOf(genre) == -1) { list = "There are no books of that genre.\n"; } else { sortBooksByGenre(); int i = 0; // Find first book with the genre while (!bookList.get(i).getGenre().equalsIgnoreCase(genre)) { i++; } // Display all books until the genre changes or until it's the last book of the shelf while (i < bookList.size() && bookList.get(i).getGenre().equalsIgnoreCase(genre)) { list += bookList.get(i) + "\n"; i++; } } return list; } public String browseBooksOfAuthor( String author ) { String list = ""; // Make the genre Capitalized to match format of genres and be case-insensitive author = author.substring(0, 1).toUpperCase() + author.substring(1).toLowerCase(); if (authorList.indexOf(author) == -1) { list = "There are no books by that author.\n"; } else { sortBooksByAuthor(); int i = 0; // Find first book with the genre while (!bookList.get(i).getAuthor().equalsIgnoreCase(author)) { i++; } // Display all books until the genre changes or until it's the last book of the shelf while (i < bookList.size() && bookList.get(i).getAuthor().equalsIgnoreCase(author)) { list += bookList.get(i) + "\n"; i++; } } return list; } public String browseByCategory( String category ) { String list = ""; if (category.equalsIgnoreCase("author")) { for (String author : authorList) { list += author + ":\n" + browseBooksOfAuthor(author) + "\n"; } } else if (category.equalsIgnoreCase("genre")) { for (String genre : genreList) { list += genre + ":\n" + browseBooksOfGenre(genre) + "\n"; } } else { list = "No such category.\n"; } return list; } @Override public String toString() { return getName(); } /** * @param args the command line arguments */ public static void main(String[] args) { Scanner user_input = new Scanner(System.in); Bookshelf myShelf = new Bookshelf(); myShelf.addBook(new Book("book1", 321, "martin", "fiction")); myShelf.addBook(new Book("book2", 123, "Martin", "non-fiction")); myShelf.addBook(new Book("book3", 852, "Peter", "sci-fi")); myShelf.addBook(new Book("book4", 753, "Martin", "fiction")); myShelf.addBook(new Book("book5", 159, "Ralph", "non-fiction")); myShelf.addBook(new Book("book6", 349, "MartIn", "fiction")); myShelf.removeBook("book2"); System.out.print(myShelf.browseByCategory("author")); System.out.println(myShelf.viewBook("book1")); } }