Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A clas that maintains information on a book.
- * This might form part of a larger application such
- * as a library system, for instance.
- *
- * @author Oskar T
- * @version 0.0.1
- */
- public class Book
- {
- // MY FIELDS, MY FIELDS, WHAT HAVE YE DONE WITH MY FIELDS?
- private int pages;
- private int borrowed;
- private String author;
- private String title;
- private String refNumber;
- /**
- * Set the author, title and pages fields when this object
- * is constructed. Also sets refNumber to "".
- */
- public Book(String author, String title, int pages) {
- this.author = author;
- this.title = title;
- this.pages = pages;
- this.refNumber = "";
- }
- //Methods
- /*
- * Set refNumber to some string (length >= 3).
- */
- public void setRefNumber(String ref) {
- if (ref.length() >= 3) {
- this.refNumber = ref;
- }
- else {
- System.out.println("ERROR: Minimum of 3 characters needed, no reference number set");
- }
- }
- /*
- * Returns the number of pages.
- */
- public int getPages() {
- return this.pages;
- }
- /*
- * Returns the reference string.
- */
- public String getRefNumber() {
- return this.refNumber;
- }
- /*
- * Returns number of times a book has been borrowed.
- */
- public int getBorrowed() {
- return this.borrowed;
- }
- /*
- * Adds one to the borrow variable, which keeps track of how many times a book has been borrowed.
- */
- public void borrow() {
- this.borrowed = this.borrowed + 1;
- }
- /*
- * Prints author to terminal.
- */
- public void printAuthor() {
- System.out.println(this.author);
- }
- /*
- * Prints title to terminal.
- */
- public void printTitle() {
- System.out.println(this.title);
- }
- /*
- * Prints details (author, title and pages) to terminal.
- * Also print refNumber, but only if it has been set. Else print "ZZZ".
- */
- public void printDetails() {
- String ref = "ZZZ";
- if (this.refNumber.length() > 0) {
- ref = this.refNumber;
- }
- // should have used string append...
- System.out.println("Title: " + this.title + ", " +
- "Author: " + this.author + ", " +
- "Pages: " + this.pages + ", " +
- "Reference number: " + ref + ", " +
- "Popularity count: " + this.borrowed);
- }
- }
Add Comment
Please, Sign In to add comment