Question: I am working on this assignment. For this assignment, I am updating a Book class to keep track of the rental information. C reate an
I am working on this assignment. For this assignment, I am updating a Book class to keep track of the rental information. Create an additional constructor which has three string parameters: the name and author of the book and the name of a renter. If the original constructor is called, the resulting object should initially be considered not rented. Next, add the following methods to the class:
- boolean isRented() - has no parameters and returns a boolean. Returns true if the book is currently rented and false otherwise.
- rent(String renterName) - has one String type parameter and no return type. Rents the book out to the specified renter and sets the due date to be a week from the current date.
- returnToLibrary() - has no parameters and no return type. Removes rental information from the book. After this call, isRented() will return false until the next time the book is rented.
- String getRenter() - has no parameters and returns a String object representing the name of the renter of this book. If the book is not rented, this method should return null.
- LocalDate getDueDate() - has no parameters and returns a LocalDate (Links to an external site.) object representing the date on which the book is due. If the book is not rented, this method should return null.
For some reason when I test my code it is failing to execute properly. Is there a semantic error in my code? This is programmed in Java and it is modeled after a POJO. It is failing to pass this test:
var book1 = new Book("Some title", "Some author"); assertEquals("some renter", book1.getRenter());
Here is my code:
import java.time.LocalDate; public class Book { private String name; private String author; private String renter; private boolean rentstatus; private LocalDate dueDate; public Book(String name, String author) { this.name = name; this.author = author; renter = null; rentstatus = false; } public Book(String name, String author, String renter, LocalDate dueDate) { this.name = name; this.author = author; this.renter = renter; rentstatus = true; this.dueDate = dueDate; this.dueDate = LocalDate.now().plusDays(7); } public String getName() { //This method is good return name; } public String getAuthor() { // This method is good return author; } public boolean isRented(){ //This method is good return this.rentstatus; } public LocalDate getDueDate() { if(this.rentstatus){ this.dueDate = LocalDate.now().plusDays(7); return this.dueDate; } else { return null; } } public String getRenter() { // This method is good if (this.rentstatus) { return renter; } else{ return null; } } public void returnToLibrary() { // This method is good renter = null; dueDate = null; rentstatus = false; name = null; author = null; } public void rent(String renter) { this.name = renter; this.renter = name; this.dueDate = LocalDate.now().plusDays(7); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
