Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. /**
  2.  * A class that maintains information on a book.
  3.  * This might form part of a larger application such
  4.  * as a library system, for instance.
  5.  *
  6.  * @author (Jonah Broyer)
  7.  * @version (1/18/2020)
  8.  */
  9. class Book
  10. {
  11.     // The fields.
  12.     private String author;
  13.     private String title;
  14.     private int pages;
  15.     private String refNumber;
  16.     private int borrowed;
  17.     private boolean courseText;
  18.  
  19.     /**
  20.      * Set the author and title fields when this object
  21.      * is constructed.
  22.      */
  23.     public Book(String bookAuthor, String bookTitle, int bookPages, boolean bookText)
  24.     {
  25.         author = bookAuthor;
  26.         title = bookTitle;
  27.         pages = bookPages;
  28.         refNumber = "";
  29.         courseText = bookText;
  30.     }
  31.  
  32.     /**
  33.      * Exercise 2.83
  34.      * Return the author of the book.
  35.      */
  36.     public String getAuthor()
  37.     {
  38.         return author;
  39.     }
  40.    
  41.     /**
  42.      * Exercise 2.83
  43.      * Return the title of the book.
  44.      */
  45.     public String getTitle()
  46.     {
  47.         return title;
  48.     }
  49.    
  50.     /**
  51.      * Exercise 2.84
  52.      * Print the author's name.
  53.      */
  54.     public void printAuthor()
  55.     {
  56.         System.out.println("Author: " + author);
  57.     }
  58.    
  59.     /**
  60.      * Exercise 2.84
  61.      * Print the title of the book.
  62.      */
  63.     public void printTitle()
  64.     {
  65.         System.out.println("Title: " + title);
  66.     }
  67.    
  68.     /**
  69.      * Exercise 2.85
  70.      * Return the number of pages.
  71.      */
  72.     public int getPages()
  73.     {
  74.         return pages;
  75.     }
  76.    
  77.     /**
  78.      * Exercise 2.91
  79.      * Print details about the book.
  80.      */
  81.     public void printDetails()
  82.     {
  83.         if(refNumber.length() > 0) {
  84.             System.out.println("Title: " + title + ", Author: " + author +
  85.                             ", Pages: " + pages + ", Reference Number: "
  86.                             + refNumber + ", Amount of times this book" +
  87.                             " has been borrowed: " + borrowed);
  88.         }
  89.         else {
  90.            System.out.println("Title: " + title + ", Author: " + author +
  91.                             ", Pages: " + pages + ", Reference Number: "
  92.                             + "ZZZ" + ", Amount of times this book has" +
  93.                             " been borrowed: " + borrowed);
  94.         }
  95.     }
  96.    
  97.     /**
  98.      * Exercise 2.90
  99.      * Set the books reference number.
  100.      */
  101.     public void setRefNumber(String ref)
  102.     {
  103.         if(ref.length() >= 3) {
  104.             refNumber = ref;
  105.         }
  106.         else {
  107.             System.out.println("Please enter a reference number that's " +
  108.             "at least three characters.");
  109.         }
  110.     }
  111.    
  112.     /**
  113.      * Exercise 2.87
  114.      * Return the books reference number.
  115.      */
  116.     public String getRefNumber()
  117.     {
  118.         return refNumber;
  119.     }
  120.    
  121.     /**
  122.      * Exercise 2.91
  123.      * Update the borrow field.
  124.      */
  125.     public void borrow()
  126.     {
  127.         borrowed = borrowed + 1;
  128.     }
  129.    
  130.     /**
  131.      * Exercise 2.91
  132.      * Return the value of how many times the book
  133.      * has been borrowed.
  134.      */
  135.     public int getBorrowed()
  136.     {
  137.         return borrowed;
  138.     }
  139.    
  140.     /**
  141.      * Exercise 2.92
  142.      * Return whether or not the book is being used
  143.      * as a textbook.
  144.      */
  145.     public boolean isCourseText()
  146.     {
  147.         return courseText;
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement