brainfrz

Untitled

Oct 15th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.69 KB | None | 0 0
  1. /**************************************************************************************************
  2.  *  Program Name:     Programming Project 5.7: Bookshelf
  3.  *  Class Name:       pp5_7.Book
  4.  *  Author:           Terry Weiss
  5.  *  Date Written:     October 15, 2015
  6.  *  Program Description:
  7.  *     This class contains instance data for a book that will be used in `Bookshelf`. It contains
  8.  *  instance data for the title, author, genre, and number of pages.
  9.  **************************************************************************************************/
  10. package pp5_7;
  11.  
  12. import java.util.Comparator;
  13. import java.util.ArrayList;
  14. import java.util.Collections;
  15.  
  16. /**
  17.  *
  18.  * @author Terry
  19.  */
  20. public class Book implements Comparable<Book> {
  21.     private static final String DEFAULT_AUTHOR = "Unknown";
  22.     private static final String DEFAULT_GENRE = "General";
  23.  
  24.     private static int untitledBooks = 0; // Total count of all untitled books to uniquely name them
  25.     private String title;
  26.     private String author;
  27.     private String genre;
  28.     private int pages;
  29.  
  30.  
  31.  
  32.     Book( String title, int pages, String author, String genre ) {
  33.         setTitle(title);
  34.         setPages(pages);
  35.         setAuthor(author);
  36.         setGenre(genre);
  37.     }
  38.  
  39.     Book( String title, int pages, String author ) {
  40.         setTitle(title);
  41.         setPages(pages);
  42.         setAuthor(author);
  43.         setGenre(DEFAULT_GENRE);
  44.     }
  45.  
  46.     Book( String title, int pages ) {
  47.         setTitle(title);
  48.         setPages(pages);
  49.         setAuthor(DEFAULT_AUTHOR);
  50.         setGenre(DEFAULT_GENRE);
  51.     }
  52.  
  53.  
  54.     private String setTitle( String title ) {
  55.         if (title.length() == 0) {
  56.             untitledBooks++;
  57.             title = "Untitled #" + untitledBooks;
  58.         }
  59.         else {
  60.             title = titleCase(title);
  61.         }
  62.  
  63.         return (this.title = title);
  64.     }
  65.  
  66.     private int setPages( int pages ) {
  67.         if (pages < 1) {
  68.             pages = 1;
  69.         }
  70.  
  71.         return (this.pages = pages);
  72.     }
  73.  
  74.     private String setAuthor( String author ) {
  75.         String fullName = "";
  76.         String[] names = author.split(" ");
  77.  
  78.         for (int currentName = 0; currentName < names.length - 1; currentName++) {
  79.             fullName += capitalize(names[currentName]) + " ";
  80.         }
  81.  
  82.         fullName += capitalize(names[names.length - 1]);
  83.  
  84.         return (this.author = fullName);
  85.     }
  86.  
  87.     private String setGenre( String genre ) {
  88.         if (genre.length() == 0) {
  89.             genre = DEFAULT_GENRE;
  90.         }
  91.         else {
  92.             genre = capitalize(genre);
  93.         }
  94.  
  95.         return (this.genre = genre);
  96.     }
  97.  
  98.  
  99.     public String getTitle() {
  100.         return title;
  101.     }
  102.  
  103.     public int getPages() {
  104.         return pages;
  105.     }
  106.  
  107.     public String getAuthor() {
  108.         return author;
  109.     }
  110.  
  111.     public String getGenre() {
  112.         return genre;
  113.     }
  114.  
  115.  
  116.     public String displayDetails() {
  117.         String details = "Title: " + title + "\n"
  118.                        + "Author: " + author + "\n"
  119.                        + "Genre: " + genre + "\n"
  120.                        + "Pages: " + pages + "\n";
  121.  
  122.         return details;
  123.     }
  124.  
  125.  
  126.  
  127.     private String titleCase( String str ) {
  128.         String[] words = str.split(" ");
  129.         String ret;
  130.         int currentWord;
  131.  
  132.         // Always capitalize first and last words, with other words capitalized as needed
  133.         ret = capitalize(words[0]) + " ";
  134.  
  135.         for (currentWord = 1; currentWord < (words.length - 1); currentWord++) {
  136.             String word = words[currentWord];
  137.  
  138.             if (isTitleWord(word)) {
  139.                 ret += capitalize(word) + " ";
  140.             }
  141.             else {
  142.                 ret += word.toLowerCase() + " ";
  143.             }
  144.         }
  145.  
  146.         if (words.length > 1) {
  147.             ret += capitalize(words[currentWord]);
  148.         }
  149.  
  150.         return ret;
  151.     }
  152.  
  153.     private String capitalize( String word ) {
  154.         return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
  155.     }
  156.  
  157.     private boolean isTitleWord( String word ) {
  158.         boolean isCapitalized;
  159.         word = word.toLowerCase();
  160.  
  161.         // All articles coordinate conjuctions and prepositions are lower case,
  162.         // per Chicago Manual of Style
  163.         switch (word) {
  164.             // Articles:
  165.             case "a":       case "an":      case "the":
  166.             // Coordinate conjunctions:
  167.             case "and":     case "but":     case "for":     case "nor":     case "or":
  168.             case "so":      case "yet":
  169.             // Most common single-word English prepositions:
  170.             case "at":      case "about":   case "above":   case "across":  case "ago":
  171.             case "before":  case "beside":  case "by":      case "in":      case "into":
  172.             case "on":      case "onto":    case "over":    case "past":    case "since":
  173.             case "til":     case "through": case "to":      case "towards": case "until":
  174.                 isCapitalized = false;
  175.                 break;
  176.  
  177.             default:
  178.                 isCapitalized = true;
  179.         }
  180.  
  181.         return isCapitalized;
  182.     }
  183.  
  184.  
  185.     @Override
  186.     public String toString() {
  187.         return getTitle();
  188.     }
  189.  
  190.     /**
  191.      *  Allows Book objects to be sorted by their size with `this.pages`
  192.      *
  193.      *  @param book2 The second Book object being compared against
  194.      *  @return The number of pages more than the book being compared against
  195.      */
  196.     @Override
  197.     public int compareTo( Book book2 ) {
  198.         return this.getPages() - book2.getPages();
  199.     }
  200.  
  201.     /**
  202.      *  Compares books by their titles in ascending order.
  203.      */
  204.     public static Comparator<Book> sortByTitle = (Book book1, Book book2) -> {
  205.         String bookTitle1 = book1.getTitle();
  206.         String bookTitle2 = book2.getTitle();
  207.  
  208.         return bookTitle1.compareTo(bookTitle2);
  209.     };
  210.  
  211.     /**
  212.      *  Compares books by their authors in ascending order.
  213.      */
  214.     public static Comparator<Book> sortByAuthor = (Book book1, Book book2) -> {
  215.         String bookAuthor1 = book1.getAuthor();
  216.         String bookAuthor2 = book2.getAuthor();
  217.  
  218.         return bookAuthor1.compareTo(bookAuthor2);
  219.     };
  220.  
  221.  
  222.  
  223.     /*
  224.      *  Main method is used for debugging and testing purposes on `Book` class
  225.      */
  226.     public static void main( String args[] ) {
  227.         Scanner user_input = new Scanner(System.in);
  228.         ArrayList<Book> bookList = new ArrayList(6);
  229.         Book book1, book2, book3, book4, book5, book6;
  230.         String title, author, genre;
  231.         int pages;
  232.  
  233.         bookList.add(book1 = new Book("peteR pAn", 42, "marTIn", "fIcTIoN"));
  234.         bookList.add(book2 = new Book("", 234));
  235.         bookList.add(book3 = new Book("the wiTCH IS deAD", 50, "bILLy tHOmaS"));
  236.         bookList.add(book4 = new Book("", 23));
  237.         bookList.add(book5 = new Book("gross ON OuT", 80));
  238.         bookList.add(book6 = new Book("", 89));
  239.  
  240.         System.out.println("Before sorting:");
  241.         for (Book currentBook : bookList) {
  242.             System.out.println(currentBook);
  243.         }
  244.  
  245.         Collections.sort(bookList, Book.sortByTitle);
  246.  
  247.         System.out.println("\nAfter sorting by title:");
  248.         for (Book currentBook : bookList) {
  249.             System.out.printf("%-19s %s\n", currentBook, currentBook.getTitle());
  250.         }
  251.  
  252.         Collections.sort(bookList, Book.sortByAuthor);
  253.  
  254.         System.out.println("\nAfter sorting by author:");
  255.         for (Book currentBook : bookList) {
  256.             System.out.printf("%-19s %s\n", currentBook, currentBook.getAuthor());
  257.         }
  258.  
  259.         Collections.sort(bookList);
  260.  
  261.         System.out.println("\nAfter sorting by size:");
  262.         for (Book currentBook : bookList) {
  263.             System.out.printf("%-19s %d\n", currentBook, currentBook.getPages());
  264.         }
  265.     }
  266. }
  267.  
  268. /**OUTPUT OF MAIN*************************************************************************************
  269. Before sorting:
  270. Peter Pan
  271. Untitled #1
  272. The Witch Is Dead
  273. Untitled #2
  274. Gross on Out
  275. Untitled #3
  276.  
  277. After sorting by title:
  278. Gross on Out        Gross on Out
  279. Peter Pan           Peter Pan
  280. The Witch Is Dead   The Witch Is Dead
  281. Untitled #1         Untitled #1
  282. Untitled #2         Untitled #2
  283. Untitled #3         Untitled #3
  284.  
  285. After sorting by author:
  286. The Witch Is Dead   Billy Thomas
  287. Peter Pan           Martin
  288. Gross on Out        Unknown
  289. Untitled #1         Unknown
  290. Untitled #2         Unknown
  291. Untitled #3         Unknown
  292.  
  293. After sorting by size:
  294. Untitled #2         23
  295. Peter Pan           42
  296. The Witch Is Dead   50
  297. Gross on Out        80
  298. Untitled #3         89
  299. Untitled #1         234
  300. ***********************************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment