Guest User

Untitled

a guest
Jan 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. /**
  2. * A clas 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 Oskar T
  7. * @version 0.0.1
  8. */
  9. public class Book
  10. {
  11. // MY FIELDS, MY FIELDS, WHAT HAVE YE DONE WITH MY FIELDS?
  12. private int pages;
  13. private int borrowed;
  14. private String author;
  15. private String title;
  16. private String refNumber;
  17.  
  18. /**
  19. * Set the author, title and pages fields when this object
  20. * is constructed. Also sets refNumber to "".
  21. */
  22. public Book(String author, String title, int pages) {
  23. this.author = author;
  24. this.title = title;
  25. this.pages = pages;
  26.  
  27. this.refNumber = "";
  28. }
  29.  
  30. //Methods
  31.  
  32.  
  33. /*
  34. * Set refNumber to some string (length >= 3).
  35. */
  36. public void setRefNumber(String ref) {
  37.  
  38. if (ref.length() >= 3) {
  39. this.refNumber = ref;
  40. }
  41. else {
  42. System.out.println("ERROR: Minimum of 3 characters needed, no reference number set");
  43. }
  44. }
  45.  
  46. /*
  47. * Returns the number of pages.
  48. */
  49. public int getPages() {
  50. return this.pages;
  51. }
  52.  
  53. /*
  54. * Returns the reference string.
  55. */
  56. public String getRefNumber() {
  57. return this.refNumber;
  58. }
  59.  
  60. /*
  61. * Returns number of times a book has been borrowed.
  62. */
  63. public int getBorrowed() {
  64. return this.borrowed;
  65. }
  66.  
  67.  
  68. /*
  69. * Adds one to the borrow variable, which keeps track of how many times a book has been borrowed.
  70. */
  71. public void borrow() {
  72. this.borrowed = this.borrowed + 1;
  73. }
  74.  
  75. /*
  76. * Prints author to terminal.
  77. */
  78. public void printAuthor() {
  79. System.out.println(this.author);
  80. }
  81.  
  82. /*
  83. * Prints title to terminal.
  84. */
  85. public void printTitle() {
  86. System.out.println(this.title);
  87. }
  88.  
  89. /*
  90. * Prints details (author, title and pages) to terminal.
  91. * Also print refNumber, but only if it has been set. Else print "ZZZ".
  92. */
  93. public void printDetails() {
  94. String ref = "ZZZ";
  95.  
  96. if (this.refNumber.length() > 0) {
  97. ref = this.refNumber;
  98. }
  99.  
  100. // should have used string append...
  101. System.out.println("Title: " + this.title + ", " +
  102. "Author: " + this.author + ", " +
  103. "Pages: " + this.pages + ", " +
  104. "Reference number: " + ref + ", " +
  105. "Popularity count: " + this.borrowed);
  106. }
  107. }
Add Comment
Please, Sign In to add comment