phantom900

Book.java

Mar 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.19 KB | None | 0 0
  1. /**
  2.  * This class represents a Book object.
  3.  *
  4.  * Author: Daniel Litvak
  5.  * Date: 1.4.2018
  6.  */
  7. public class Book
  8. {
  9.     private String _title;
  10.     private String _author;
  11.     private int _yearPublished;
  12.     private int _noOfPages;
  13.     private boolean _borrowed;
  14.     private String _studentName;
  15.     private Date _borrowDate;
  16.     private Date _returnDate;
  17.  
  18.     private final int MAX_BORROW_DAYS = 30;
  19.     private final int DOUBLE_PENALTY_DAYS = 60;
  20.     private final int PENALTY_PER_DAY = 5;
  21.  
  22.     // Constructors
  23.  
  24.     /**
  25.      * Constructs a new Book object. If year is not Valid it will be set to default 2000.
  26.      * If number of pages in not valid it will be set to default 1. _borrowed field is set to false.
  27.      * All other field is set to null.
  28.      *
  29.      * @param title         book title
  30.      * @param author        book author
  31.      * @param yearPublished the year the book was published
  32.      * @param noOfPages     book number of pages
  33.      */
  34.     public Book(String title, String author, int yearPublished, int noOfPages)
  35.     {
  36.         if (yearPublished < 1800 || _yearPublished > 2018)
  37.             yearPublished = 2000;
  38.         if (noOfPages < 1)
  39.             noOfPages = 1;
  40.  
  41.  
  42.         _title = title;
  43.         _author = author;
  44.         _yearPublished = yearPublished;
  45.         _noOfPages = noOfPages;
  46.         _borrowed = false;
  47.         _borrowDate = null;
  48.         _returnDate = null;
  49.         _studentName = null;
  50.  
  51.     }
  52.  
  53.     /**
  54.      * Copy constructor for Book object.
  55.      *
  56.      * @param other book to be copied
  57.      */
  58.     public Book(Book other)
  59.     {
  60.         if (other != null)
  61.         {
  62.             _title = other._title;
  63.             _author = other._author;
  64.             _yearPublished = other._yearPublished;
  65.             _noOfPages = other._noOfPages;
  66.             _borrowed = other._borrowed;
  67.             _studentName = other._studentName;
  68.  
  69.             // Checking if there is a borrow date
  70.             if (other._borrowDate != null)
  71.             {
  72.                 // Create a copy to avoid aliasing
  73.                 _borrowDate = new Date(other._borrowDate);
  74.             }
  75.             else
  76.             {
  77.                 _borrowDate = null;
  78.             }
  79.             // Checking if there is a return date
  80.             if (other._returnDate != null)
  81.             {
  82.                 // Create a copy to avoid aliasing
  83.                 _returnDate = new Date(other._returnDate);
  84.             }
  85.             else
  86.             {
  87.                 _returnDate = null;
  88.             }
  89.         }
  90.     }
  91.  
  92.     /**
  93.      * Checks if this book equals to other book.
  94.      *
  95.      * @param other the book to compare this book to
  96.      * @return true if this book and other are the same; false otherwise
  97.      */
  98.     public boolean equals(Book other)
  99.     {
  100.         return (_title.equals(other._title) && _author.equals(other._author) &&
  101.                 _yearPublished == other._yearPublished && _noOfPages == other._noOfPages);
  102.     }
  103.  
  104.     /**
  105.      * Returns a string representation of this book.
  106.      *
  107.      * @return representation of this book in the following format, for example,
  108.      * Title: Pride and Prejudice Author: Jane Austen Year: 1813, 350 pages
  109.      */
  110.     public String toString()
  111.     {
  112.         return "Title: " + _title + "\tAuthor: " + _author + "\tYear: " +
  113.                 _yearPublished + ", " + _noOfPages + " pages";
  114.     }
  115.  
  116.     /**
  117.      * Checks if this book is older than other book.
  118.      *
  119.      * @param other the book to compare to
  120.      * @return true if this book is older than other book; false otherwise
  121.      */
  122.     public boolean olderBook(Book other)
  123.     {
  124.         return this._yearPublished < other._yearPublished;
  125.     }
  126.  
  127.     /**
  128.      * Checks if this book and other book have the same author.
  129.      *
  130.      * @param other the book to compare to
  131.      * @return true if this book and other book have the same author; false otherwise
  132.      */
  133.     public boolean sameAuthor(Book other)
  134.     {
  135.         return this._author.equals(other._author);
  136.     }
  137.  
  138.     /**
  139.      * Gets student name and borrow date and updates the appropriate book attributes.
  140.      *
  141.      * @param name the student name
  142.      * @param d    borrow date
  143.      */
  144.     public void borrowBook(String name, Date d)
  145.     {
  146.         if (!_borrowed & isAvailable(d))
  147.         {
  148.             _studentName = name;
  149.             _borrowDate = new Date(d);
  150.             _borrowed = true;
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * Gets return date and updates the appropriate book attributes
  156.      *
  157.      * @param d return date
  158.      * @return true if student is late or the book is not borrowed; false otherwise
  159.      */
  160.     public boolean returnBook(Date d)
  161.     {
  162.         if (_borrowed)
  163.         {
  164.             boolean isLate = false;
  165.             // Check if the book is returned late
  166.             if (howLongBorrowed(d) > 30)
  167.                 isLate = true;
  168.  
  169.             // Update return date, remove student name, change borrow status
  170.             _returnDate = new Date(d);
  171.             _studentName = null;
  172.             _borrowed = false;
  173.             return isLate;
  174.         }
  175.         return true;
  176.     }
  177.  
  178.     /**
  179.      * Gets today's date and if book is borrowed returns how many days the book is borrowed;
  180.      * otherwise returns 0.
  181.      *
  182.      * @param d today's date
  183.      * @return how many days the book is borrowed
  184.      */
  185.     public int howLongBorrowed(Date d)
  186.     {
  187.  
  188.         if (_borrowed)
  189.         {
  190.             // if today's date is before the borrow date return 0
  191.             if (d.before(_borrowDate))
  192.             {
  193.                 return 0;
  194.             }
  195.             return d.difference(_borrowDate);
  196.         }
  197.         return 0;
  198.     }
  199.  
  200.     /**
  201.      * Checks if the book is available
  202.      *
  203.      * @param d today's date
  204.      * @return false if the book is borrowed; otherwise if the book is not borrowed return
  205.      * false if today's day is Friday or Saturday; otherwise return true
  206.      */
  207.     public boolean isAvailable(Date d)
  208.     {
  209.         if (_borrowed)
  210.         {
  211.             return false;
  212.         }
  213.         int dayInWeek = d.dayInWeek();
  214.         if (dayInWeek == 0 || dayInWeek == 6)
  215.         {
  216.             return false;
  217.         }
  218.         return true;
  219.     }
  220.  
  221.     /**
  222.      * Computes penalty given return date.
  223.      *
  224.      * @param d return date
  225.      * @return penalty if book is borrowed and student is late; 0 otherwise
  226.      */
  227.     public int computePenalty(Date d)
  228.     {
  229.         if (_borrowed)
  230.         {
  231.             int daysBorrowed = howLongBorrowed(d);
  232.  
  233.             if (daysBorrowed < MAX_BORROW_DAYS)
  234.                 return 0;
  235.  
  236.             if (daysBorrowed < DOUBLE_PENALTY_DAYS)
  237.                 return (PENALTY_PER_DAY * (daysBorrowed - MAX_BORROW_DAYS));
  238.  
  239.             return (PENALTY_PER_DAY * (daysBorrowed - MAX_BORROW_DAYS)) +
  240.                     (2 * PENALTY_PER_DAY * (daysBorrowed - DOUBLE_PENALTY_DAYS));
  241.         }
  242.         return 0;
  243.     }
  244.  
  245.     // Getters
  246.  
  247.     /**
  248.      * Returns the book title.
  249.      *
  250.      * @return the book title
  251.      */
  252.     public String getTitle()
  253.     {
  254.         return _title;
  255.     }
  256.  
  257.     /**
  258.      * Returns the book author.
  259.      *
  260.      * @return the book author
  261.      */
  262.     public String getAuthor()
  263.     {
  264.         return _author;
  265.     }
  266.  
  267.     /**
  268.      * Returns the year the book was published.
  269.      *
  270.      * @return the year the book was published
  271.      */
  272.     public int getYear()
  273.     {
  274.         return _yearPublished;
  275.     }
  276.  
  277.     /**
  278.      * Returns the book number of pages.
  279.      *
  280.      * @return the book number of pages
  281.      */
  282.     public int getPages()
  283.     {
  284.         return _noOfPages;
  285.     }
  286.  
  287.     /**
  288.      * Returns true if the book is borrowed; false otherwise.
  289.      *
  290.      * @return true if the book is borrowed; false otherwise
  291.      */
  292.     public boolean getBorrowed()
  293.     {
  294.         return _borrowed;
  295.     }
  296.  
  297.     /**
  298.      * Returns the student name.
  299.      *
  300.      * @return the student name
  301.      */
  302.     public String getStudentName()
  303.     {
  304.         return _studentName;
  305.     }
  306.  
  307.     /**
  308.      * Returns the book borrowed date.
  309.      *
  310.      * @return the book borrowed date
  311.      */
  312.     public Date getBorrowedDate()
  313.     {
  314.         return new Date(_borrowDate);
  315.     }
  316.  
  317.     /**
  318.      * Returns the book return date.
  319.      *
  320.      * @return the book return date
  321.      */
  322.     public Date getReturnDate()
  323.     {
  324.         return new Date(_returnDate);
  325.     }
  326.  
  327.     // Setters
  328.  
  329.     /**
  330.      * Sets the book title.
  331.      *
  332.      * @param s the new book title String to be set
  333.      */
  334.     public void setTitle(String s)
  335.     {
  336.         _title = s;
  337.     }
  338.  
  339.     /**
  340.      * Sets the book author.
  341.      *
  342.      * @param s the new book author to be set
  343.      */
  344.     public void setAuthor(String s)
  345.     {
  346.         _author = s;
  347.     }
  348.  
  349.     /**
  350.      * Sets the year the book was published (only if valid).
  351.      *
  352.      * @param n the book published year to be set
  353.      */
  354.     public void setYear(int n)
  355.     {
  356.         if (n < 2018 && n > 1800)
  357.             _yearPublished = n;
  358.     }
  359.  
  360.     /**
  361.      * Sets the book number of pages (only if valid).
  362.      *
  363.      * @param n the number of pages to be set
  364.      */
  365.     public void setPages(int n)
  366.     {
  367.         if (n > 0)
  368.             _noOfPages = n;
  369.     }
  370. }
Add Comment
Please, Sign In to add comment