Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 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.  
  18.     /**
  19.      * Set the author and title fields when this object
  20.      * is constructed.
  21.      */
  22.     public Book(String bookAuthor, String bookTitle, int bookPages)
  23.     {
  24.         author = bookAuthor;
  25.         title = bookTitle;
  26.         pages = bookPages;
  27.         refNumber = "";
  28.     }
  29.  
  30.     /**
  31.      * Exercise 2.83
  32.      * Return the author of the book.
  33.      */
  34.     public String getAuthor()
  35.     {
  36.         return author;
  37.     }
  38.    
  39.     /**
  40.      * Exercise 2.83
  41.      * Return the title of the book.
  42.      */
  43.     public String getTitle()
  44.     {
  45.         return title;
  46.     }
  47.    
  48.     /**
  49.      * Exercise 2.84
  50.      * Print the author's name.
  51.      */
  52.     public void printAuthor()
  53.     {
  54.         System.out.println("Author: " + author);
  55.     }
  56.    
  57.     /**
  58.      * Exercise 2.84
  59.      * Print the title of the book.
  60.      */
  61.     public void printTitle()
  62.     {
  63.         System.out.println("Title: " + title);
  64.     }
  65.    
  66.     /**
  67.      * Exercise 2.85
  68.      * Return the number of pages.
  69.      */
  70.     public int getPages()
  71.     {
  72.         return pages;
  73.     }
  74.    
  75.     /**
  76.      * Exercise 2.91
  77.      * Print details about the book.
  78.      */
  79.     public void printDetails()
  80.     {
  81.         if(refNumber.length() > 0) {
  82.             System.out.println("Title: " + title + ", Author: " + author +
  83.                             ", Pages: " + pages + ", Reference Number: "
  84.                             + refNumber + ", Amount of times this book" +
  85.                             " has been borrowed: " + borrowed);
  86.         }
  87.         else {
  88.            System.out.println("Title: " + title + ", Author: " + author +
  89.                             ", Pages: " + pages + ", Reference Number: "
  90.                             + "ZZZ" + ", Amount of times this book has" +
  91.                             " been borrowed: " + borrowed);
  92.         }
  93.     }
  94.    
  95.     /**
  96.      * Exercise 2.90
  97.      * Set the books reference number.
  98.      */
  99.     public void setRefNumber(String ref)
  100.     {
  101.         if(ref.length() >= 3) {
  102.             refNumber = ref;
  103.         }
  104.         else {
  105.             System.out.println("Please enter a reference number that's " +
  106.             "at least three characters.");
  107.         }
  108.     }
  109.    
  110.     /**
  111.      * Exercise 2.87
  112.      * Return the books reference number.
  113.      */
  114.     public String getRefNumber()
  115.     {
  116.         return refNumber;
  117.     }
  118.    
  119.     /**
  120.      * Exercise 2.91
  121.      * Update the borrow field
  122.      */
  123.     public void borrow()
  124.     {
  125.         borrowed = borrowed + 1;
  126.     }
  127.    
  128.     /**
  129.      * Exercise 2.91
  130.      * Return the value of how many times the book
  131.      * has been borrowed
  132.      */
  133.     public int getBorrowed()
  134.     {
  135.         return borrowed;
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement