Question
Create Book class that will include a name, Author, quantity in stock and price. Use the UML diagrams that are shown bellow for a guide
Create Book class that will include a name, Author, quantity in stock and price. Use the UML diagrams that are shown bellow for a guide of what methods and constructors to possibly create in the book. For the equals method, two books are equal when their name and author is the same. The starting point are listed down below. (This Author.java includes equals and clone methods. You can use them as examples for the Book methods.)
Author: Ernest Hemingway (M) can be reached at eh@outlook.com Book: Old Man and the Sea by Ernest Hemingway, $15.99 (0 in stock) Book: Old Man and the Sea by Ernest Hemingway, $15.99 (5 in stock) Book: Old Man and the Sea by Ernest Hemingway, $11.9925 (5 in stock) Cloned book: Book: Old Man and the Sea by Ernest Hemingway, $11.9925 (0 in stock) The books are the same
Author.java
public class Author { //---------data members---------------------------- private String name; private String email; private char gender; //---------constructors----------------------------- public Author(String name) { this.name = name; } public Author(String name, String email, char gender) { this.name = name; this.email = email; if(gender == 'M' || gender == 'F') this.gender = gender; } //---------methods--------------------------------- public String getName() { return this.name; } public String getEmail() { return this.email; } public char getGender() { return this.gender; } public boolean hasGender() { return (gender == 'M' || gender == 'F'); } public boolean hasEmail() { return (email != null); } public void setName(String name){ this.name = name; } public void setEmail(String email) { this.email = email; } public void setGender(char gender) { if(gender == 'M' || gender == 'F') this.gender = gender; }
//---------general Object methods------------------- public String toString() { String description = "Author: " + this.name; if(hasGender()) description += " (" + this.gender; if(hasEmail()) description += ") can be reached at " + this.email;
return description; } public boolean equals(Author otherAuthor) { if(this.name.equals(otherAuthor.getName()) && this.email.equals(otherAuthor.getEmail()) && this.gender == otherAuthor.getGender()) return true; else return false; }
public Author clone() { Author authorCopy = new Author(this.name, this.email, this.gender); return authorCopy; }
}
BookAuthorTest.java
/** * This class tests the Author and Book classes used in the Unit 4 Lab * @author Jen Rosato **/ public class BookAuthorTest {
public static void main(String args[]) { //creating an author and book Author hemingway = new Author("Ernest Hemingway", "eh@outlook.com", 'M'); Book oldMan = new Book("Old Man and the Sea", hemingway, 15.99); //printing them out to see initial values System.out.println(hemingway.toString()); System.out.println(oldMan.toString()); //changing the number of books in stock oldMan.setQtyInStock(5); System.out.println(oldMan.toString()); //changing the price, add formatting if you know how - but its ok if you don't oldMan.setPrice(oldMan.getPrice()*.75); System.out.println(oldMan); //creating clones and testing Book oldMan2 = oldMan.clone(); System.out.println("Cloned book: " + oldMan2); if(oldMan2.equals(oldMan)) System.out.println("The books are the same"); else System.out.println("The books are different"); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started