brainfrz

Untitled

Oct 18th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.09 KB | None | 0 0
  1. /**************************************************************************************************
  2.  *  Program Name:     Programming Project 5.7: Bookshelf
  3.  *  Class Name:       pp5_7.Bookshelf
  4.  *  Author:           Terry Weiss
  5.  *  Date Written:     October 15, 2015
  6.  *  Program Description:
  7.  *     This class contains instance data for a bookshelf that is full of `Book`s. It allows the
  8.  *  User to store a book, browse the list of books, 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 java.util.Scanner;
  15. import java.util.ArrayList;
  16. import java.util.Collections;
  17.  
  18.  
  19. /**
  20.  *
  21.  * @author Terry Weiss
  22.  * @see    pp5_7.Book
  23.  * @see    pp5_7.Librarian
  24. */
  25. public class Bookshelf {
  26.     private final String DEFAULT_NAME = "Bookshelf";
  27.  
  28.     private ArrayList<Book> bookList = new ArrayList();
  29.     private ArrayList<String> genreList = new ArrayList();
  30.     private ArrayList<String> authorList = new ArrayList();
  31.     private final String name;
  32.  
  33.  
  34.     Bookshelf() {
  35.         name = DEFAULT_NAME;
  36.     }
  37.  
  38.     Bookshelf( String name ) {
  39.         this.name = name;
  40.     }
  41.  
  42.  
  43.     public int getNumberOfBooks() {
  44.         return bookList.size();
  45.     }
  46.  
  47.     public String getName() {
  48.         return name;
  49.     }
  50.  
  51.  
  52.     Book addBook( Book book ) {
  53.         bookList.add(book);
  54.  
  55.         String genre = book.getGenre();
  56.         if (!genreList.contains(genre)) {
  57.             genreList.add(genre);
  58.         }
  59.  
  60.         String author = book.getAuthor();
  61.         if (!authorList.contains(author)) {
  62.             authorList.add(author);
  63.         }
  64.  
  65.         return book;
  66.     }
  67.  
  68.     boolean removeBook( Book book ) {
  69.         // If it's the only book of that genre or author, remove the genre/author from its list
  70.         String genre = book.getGenre();
  71.         if (numBooksWithGenre(genre) == 1) {
  72.             genreList.remove(genre);
  73.         }
  74.  
  75.         String author = book.getAuthor();
  76.         if (numBooksWithAuthor(author) == 1) {
  77.             authorList.remove(author);
  78.         }
  79.  
  80.         return bookList.remove(book);
  81.     }
  82.  
  83.     Book removeBook( int position ) {
  84.         String genre = bookList.get(position).getGenre();
  85.         if (numBooksWithGenre(genre) == 1) {
  86.             genreList.remove(genre);
  87.         }
  88.  
  89.         String author = bookList.get(position).getAuthor();
  90.         if (numBooksWithGenre(genre) == 1) {
  91.             authorList.remove(author);
  92.         }
  93.  
  94.         return bookList.remove(position);
  95.     }
  96.  
  97.     boolean removeBook( String title ) {
  98.         if (numBooksWithTitle(title) == 1) {
  99.             removeBook(findBook(title));
  100.             return true;
  101.         }
  102.         else {
  103.             return false;
  104.         }
  105.     }
  106.  
  107.  
  108.     String viewBook( int position ) {
  109.         String ret;
  110.  
  111.         if (position < 0 || position >= bookList.size()) {
  112.             ret = "That book isn't on the shelf.";
  113.         }
  114.         else {
  115.             ret = bookList.get(position).displayDetails();
  116.         }
  117.  
  118.         return ret;
  119.     }
  120.  
  121.     String viewBook( String title ) {
  122.         String details = "";
  123.  
  124.         for (Book currentBook : bookList) {
  125.             if (currentBook.getTitle().equalsIgnoreCase(title)) {
  126.                 if (details.length() > 0) {  // Separate each entry by a blank line in cases
  127.                     details += "\n\n";       // of duplicate titles
  128.                 }
  129.  
  130.                 details += currentBook.displayDetails();
  131.             }
  132.         }
  133.  
  134.         if (details.length() == 0) {
  135.             details = title + " isn't on the shelf.";
  136.         }
  137.  
  138.         return details;
  139.     }
  140.  
  141.  
  142.     public void sortBooksByTitle() {
  143.         Collections.sort(bookList, Book.sortByTitle);
  144.     }
  145.  
  146.     /**
  147.      * Sort the Books by Author first and then by Title
  148.      */
  149.     public void sortBooksByAuthor() {
  150.         sortBooksByTitle();
  151.         Collections.sort(bookList, Book.sortByAuthor);
  152.     }
  153.  
  154.     /**
  155.      * Sort the Books by Genre first and then by Title
  156.      */
  157.     public void sortBooksByGenre() {
  158.         sortBooksByTitle();
  159.         Collections.sort(bookList, Book.sortByGenre);
  160.     }
  161.  
  162.     /**
  163.      * Sort the Books by size first and then by Title
  164.      */
  165.     public void sortBooksBySize() {
  166.         sortBooksByTitle();
  167.         Collections.sort(bookList);
  168.     }
  169.  
  170.  
  171.     /**
  172.      * Returns the index of the first occurrence of the Book in this list, or -1 if this list
  173.      * does not contain the Book.
  174.      *
  175.      * @param book Book object to search for
  176.      * @return the index of the first Book, or -1 if it's not there
  177.      */
  178.     public int findBook( Book book ) {
  179.         return bookList.indexOf(book);
  180.     }
  181.  
  182.     /**
  183.      * Returns the index of the first occurrence of the Book of the same title in this list,
  184.      * or -1 if this list does not contain the Book.
  185.      *
  186.      * @param title Title of the book to search for
  187.      * @return the index of the first Book, or -1 if it's not there
  188.      */
  189.     public int findBook( String title ) {
  190.         for (int currentBook = 0; currentBook < bookList.size(); currentBook++) {
  191.             String currentTitle = bookList.get(currentBook).getTitle();
  192.             if (currentTitle.equalsIgnoreCase(title)) {
  193.                 return currentBook;
  194.             }
  195.         }
  196.  
  197.         return -1;
  198.     }
  199.  
  200.  
  201.     public int numBooksWithGenre( String genre ) {
  202.         int books = 0;
  203.  
  204.         for (Book book : bookList ) {
  205.             if (book.getGenre().equalsIgnoreCase(genre)) {
  206.                 books++;
  207.             }
  208.         }
  209.  
  210.         return books;
  211.     }
  212.  
  213.     public int numBooksWithAuthor( String author ) {
  214.         int books = 0;
  215.  
  216.         for (Book book : bookList ) {
  217.             if (book.getAuthor().equalsIgnoreCase(author)) {
  218.                 books++;
  219.             }
  220.         }
  221.  
  222.         return books;
  223.     }
  224.  
  225.     public int numBooksWithTitle( String title ) {
  226.         int books = 0;
  227.  
  228.         for (Book book : bookList ) {
  229.             if (book.getTitle().equalsIgnoreCase(title)) {
  230.                 books++;
  231.             }
  232.         }
  233.  
  234.         return books;
  235.     }
  236.  
  237.  
  238.     public String browse() {
  239.         String list = "";
  240.  
  241.         for (Book currentBook : bookList ) {
  242.             list += currentBook + "\n";
  243.         }
  244.  
  245.         return list;
  246.     }
  247.  
  248.     public String browseByTitle() {
  249.         sortBooksByTitle();
  250.         return browse();
  251.     }
  252.  
  253.     public String browseByAuthor() {
  254.         sortBooksByAuthor();
  255.         return browse();
  256.     }
  257.  
  258.     public String browseByGenre() {
  259.         sortBooksByGenre();
  260.         return browse();
  261.     }
  262.  
  263.     public String browseBySize() {
  264.         sortBooksBySize();
  265.         return browse();
  266.     }
  267.  
  268.     public String browseBooksOfGenre( String genre ) {
  269.         String list = "";
  270.  
  271.         // Make the genre Capitalized to match format of genres and be case-insensitive
  272.         genre = genre.substring(0, 1).toUpperCase() + genre.substring(1).toLowerCase();
  273.  
  274.         if (genreList.indexOf(genre) == -1) {
  275.             list = "There are no books of that genre.\n";
  276.         }
  277.         else {
  278.             sortBooksByGenre();
  279.             int i = 0;
  280.  
  281.             // Find first book with the genre
  282.             while (!bookList.get(i).getGenre().equalsIgnoreCase(genre)) {
  283.                 i++;
  284.             }
  285.  
  286.             // Display all books until the genre changes or until it's the last book of the shelf
  287.             while (i < bookList.size() && bookList.get(i).getGenre().equalsIgnoreCase(genre)) {
  288.                 list += bookList.get(i) + "\n";
  289.                 i++;
  290.             }
  291.         }
  292.  
  293.         return list;
  294.     }
  295.  
  296.     public String browseBooksOfAuthor( String author ) {
  297.         String list = "";
  298.  
  299.         // Make the genre Capitalized to match format of genres and be case-insensitive
  300.         author = author.substring(0, 1).toUpperCase() + author.substring(1).toLowerCase();
  301.  
  302.         if (authorList.indexOf(author) == -1) {
  303.             list = "There are no books by that author.\n";
  304.         }
  305.         else {
  306.             sortBooksByAuthor();
  307.             int i = 0;
  308.  
  309.             // Find first book with the genre
  310.             while (!bookList.get(i).getAuthor().equalsIgnoreCase(author)) {
  311.                 i++;
  312.             }
  313.  
  314.             // Display all books until the genre changes or until it's the last book of the shelf
  315.             while (i < bookList.size() && bookList.get(i).getAuthor().equalsIgnoreCase(author)) {
  316.                 list += bookList.get(i) + "\n";
  317.                 i++;
  318.             }
  319.         }
  320.  
  321.         return list;
  322.     }
  323.  
  324.     public String browseByCategory( String category ) {
  325.         String list = "";
  326.  
  327.         if (category.equalsIgnoreCase("author")) {
  328.             for (String author : authorList) {
  329.                 list += author + ":\n"
  330.                       + browseBooksOfAuthor(author) + "\n";
  331.             }
  332.         }
  333.         else if (category.equalsIgnoreCase("genre")) {
  334.             for (String genre : genreList) {
  335.                 list += genre + ":\n"
  336.                       + browseBooksOfGenre(genre) + "\n";
  337.             }
  338.         }
  339.         else {
  340.             list = "No such category.\n";
  341.         }
  342.  
  343.         return list;
  344.     }
  345.  
  346.  
  347.  
  348.     @Override
  349.     public String toString() {
  350.         return getName();
  351.     }
  352.  
  353.  
  354.  
  355.  
  356.     /**
  357.      * @param args the command line arguments
  358.      */
  359.     public static void main(String[] args) {
  360.         Scanner user_input = new Scanner(System.in);
  361.  
  362.         Bookshelf myShelf = new Bookshelf();
  363.         myShelf.addBook(new Book("book1", 321, "martin", "fiction"));
  364.         myShelf.addBook(new Book("book2", 123, "Martin", "non-fiction"));
  365.         myShelf.addBook(new Book("book3", 852, "Peter", "sci-fi"));
  366.         myShelf.addBook(new Book("book4", 753, "Martin", "fiction"));
  367.         myShelf.addBook(new Book("book5", 159, "Ralph", "non-fiction"));
  368.         myShelf.addBook(new Book("book6", 349, "MartIn", "fiction"));
  369.  
  370.         myShelf.removeBook("book2");
  371.  
  372.         System.out.print(myShelf.browseByCategory("author"));
  373.  
  374.         System.out.println(myShelf.viewBook("book1"));
  375.     }
  376. }
Advertisement
Add Comment
Please, Sign In to add comment