Write Book.java program The Book constructor will be used to initialize all attr
ID: 3914544 • Letter: W
Question
Write Book.java program
The Book constructor will be used to initialize all attributes of the Book object. The initial status of the book should be AVAILABLE, and the patron and due date should be null.
Like the constructor, the checkin method should set the book's status to AVAILABLE and assign null to both patron and due date.
The checkout method should make the book UNAVAILABLE and save the given patron and due date in the object.
The equals method checks whether two books have the same isbn. If the given object is a Book, compare this.isbn with the other book's isbn. If the given object is a String, compare this.isbn with the string.
In the UML diagram, getX is actually eight methods: one accessor method for each of the eight attributes. These should be easy to implement (Eclipse will generate them automatically). Note that the class should have no "setX" methods.
The toString method should return a String representation of the book. If a book is initialized with the following parameters ("aaa", "bbb", "1234BBBBBBBBB", 2013, 10), it should return a String exactly as follows: "Title: aaa, Author: bbb, ISBN: 1234BBBBBBBBB, Year: 2013, Pages: 10."
////////////////////////////////////////////////////////////////
Junit code for testing:
import junit.framework.TestCase;
import java.util.Date;
/**
* Tests Book: currently one test method.
*
* @author
* @version
*
*/
public class BookTest extends TestCase {
/** A single test for book. **/
public void testBook() {
String title = "Starting Out With Java";
String author = "Tony Gaddis";
String isbn = "978-0-13-395705-1";
int year = 2016;
int pages = 1188;
//Test constructor and getters
Book book1 = new Book(title, author, isbn, year, pages);
assertEquals("Book: getTitle check ", book1.getTitle(), title);
assertEquals("Book: getAuthor check ", book1.getAuthor(), author);
assertEquals("Book: getIsbn check ", book1.getIsbn(),
isbn);
assertEquals("Book: getYear check ", book1.getYear(),
year);
assertEquals("Book: getPages check ", book1.getPages(),
pages);
assertEquals("Book: getStatus check ", book1.getStatus(),
book1.AVAILABLE);
assertEquals("Book: getDue ", book1.getDue(), null);
assertEquals("Book: getPatron ", book1.getPatron(), null);
String title2 = "Starting Out With Java Edition 4";
String author2 = "T. Gaddis";
String isbn2 = "978-0-13-608020-6";
int year2 = 2008;
int pages2 = 977;
// Test equals
Book book2 = new Book(title2, author2, isbn2, year2, pages2);
Book book3 = new Book(title, author, isbn, year, pages);
assertFalse("Book: book1 != book2", book1.equals(book2));
assertTrue("Book: book1 == book3", book1.equals(book3));
assertFalse("Book: string not equal test",
book1.equals(book2.getIsbn()));
assertTrue("Book: string equal test", book1.equals(book3.getIsbn()));
// Test toString
assertEquals("Book: toString", book1.toString(),
"Title: Starting Out With Java, Author: Tony Gaddis, "
+ "ISBN: 978-0-13-395705-1, Year: 2016, Pages: 1188.");
// Test checkout
Patron aPatron = new Patron("Dee A. B. Weikle", "weikleda@jmu.edu",
2, 1.50);
Patron ePatron = new Patron("Dee A. B. Weikle", "weikleda@jmu.edu",
2, 1.50);
Date dueDate = new Date();
book1.checkout(ePatron, dueDate);
assertTrue("Book: checkout", ePatron.equals(book1.getPatron()));
assertEquals("Book: checkout date", book1.getDue(), dueDate);
assertEquals("Book: checkout status", book1.getStatus(),
book1.UNAVAILABLE);
// Test checkin
book1.checkin();
assertEquals("Book: checkin", book1.getPatron(), null);
assertEquals("Book: checkout date", book1.getDue(), null);
assertEquals("Book: checkout status", book1.getStatus(),
book1.AVAILABLE);
}
}
Explanation / Answer
I have designed the Book class according to the specifications. I could not test it since you have not provided the Patron class.
Let me know in case of any issues, I'll help.
If the answer helped, please do rate the answer. Thank you.
Book.java
----------
import java.util.Date;
public class Book {
enum Status{ AVAILABLE, UNAVAILABLE};
private String title;
private String author;
private String isbn;
private int year;
private int pages;
private Status status;
private Patron patron;
private Date due;
public Book(){
patron = null;
due = null;
status = Status.AVAILABLE;
}
public void checkin(){
patron = null;
due = null;
status = Status.AVAILABLE;
}
public String toString(){
return String.format("Title: %s, Author: %s, ISBN: %s, Year: %d, Pages: %d.", title, author, isbn, year, pages);
}
public void checkout(Patron p, Date dueDate){
patron = p;
due = dueDate;
status = Status.UNAVAILABLE;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof String)
return isbn.equals(obj);
else if(obj instanceof Book)
{
return isbn.equals(((Book)obj).isbn);
}
else
return false;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getIsbn() {
return isbn;
}
public int getYear() {
return year;
}
public int getPages() {
return pages;
}
public Status getStatus() {
return status;
}
public Patron getPatron() {
return patron;
}
public Date getDue() {
return due;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.