Write a Patron class in a file named Patron.java . This class will utilize the B
ID: 3850753 • Letter: W
Question
Write a Patron class in a file named Patron.java . This class will utilize the Book class you wrote for HW5.
This class models someone who can check books out of a library or book lending facility. For the purposes
of this assignment, a Patron has a name and can have up to 3 unique books checked out at any given time.
The class also needs to implement the following instance methods:
a constructor: takes a String argument: the patron's rst and last name
checkout(Book b) : assigns a book to a specic patron and marks the book as borrowed
returnBook(Book b) : unassigns a book from a patron and marks a book as NOT borrowed
getBooks() : returns an array of Book objects representing the books the patron currently has checked
out
toString() : displays information about the Patron, including name and books checked out.
Spend some time thinking about these methods. What should they return? What should they do in cases of
weird input (for example, what happens if you try to return a book which a patron has never checked out?
What happens if you try to check out a book twice?) To help you out, take a look at the TestPatron.java
le. This le has one method (main) which instantiates several books and a patron and then invokes methods
to test the behavior. You should not change TestPatron.java other than to comment out portions as you
work on your methods in Patron.java .
Notes and Hints:
Begin by implementing the instance variables you need. At minimum, you'll need a String to store
the patron's name and a way to store 3 Book objects (there are multiple ways to do this part).
Do NOT modify the main method in any substantial way.
Don't forget to implement the toString method and test it.
import java.util.Arrays;
public class TestPatron {
public static void main(String[] args) {
//make some books
Book b1 = new Book("The Hobbit", "J.R.R. Tolkien");
Book b2 = new Book("Alas Babylon", "Pat Frank");
Book b3 = new Book("I Robot", "Isaac Assimov");
Book b4 = new Book("Winnie The Pooh", "A.A. Milne");
Patron p1 = new Patron("Dudley Doright");
Patron p2 = new Patron("Rachel Reader");
p1.checkout(b1);
Book[] books = p1.getBooks();
System.out.println("Should display 1 book (Hobbit):");
System.out.println(Arrays.toString(books));
System.out.println();
//test checking out same book again
p1.checkout(b1);
books = p1.getBooks();
System.out.println("Should still display 1 book (Hobbit):");
System.out.println(Arrays.toString(books));
System.out.println();
//test checking out 3 books
p1.checkout(b2);
p1.checkout(b3);
books = p1.getBooks();
System.out.println("Should display 3 books: (Hobbit, Alas,Babylon, and I, Robot)");
System.out.println(Arrays.toString(books));
System.out.println();
//test checking out more than 3 books
p1.checkout(b4);
books = p1.getBooks();
System.out.println("Should still display 3 books: (Hobbit, Alas,Babylon, and I, Robot)");
System.out.println(Arrays.toString(books));
System.out.println();
//test returning a book
p1.returnBook(b2);
books = p1.getBooks();
System.out.println("Should display 2 books (Hobbit and I,Robot):");
System.out.println(Arrays.toString(books));
System.out.println();
//test returning a book that is not checked out
p1.returnBook(b2);
books = p1.getBooks();
System.out.println("Should still display 2 books (Hobbit and I,Robot):");
System.out.println(Arrays.toString(books));
System.out.println();
//test checking out a book that is checked out by someone else
p2.checkout(b2);
p1.checkout(b2);
books = p2.getBooks();
System.out.println("Should display 1 book (Alas, Babylon):");
System.out.println(Arrays.toString(books));
books = p1.getBooks();
System.out.println("Should display 2 books (Hobbit, and I, Robot):");
System.out.println(Arrays.toString(books));
System.out.println(p1);
}
}
Explanation / Answer
Answer for Question:
Written the below java code as per given in each method and it's defination with comments and i am not aware of the what is the books class but based on problem statement i have written the below. Please check it
public class Patron
{
// instance variables
private String name;
private Book book1;
private Book book2;
private Book book3;
// Constructor for objects of class Patron
public Patron(String newName)
{
// initialise instance variables
name=newName;
book1=null;
book2=null;
book3=null;
}
// hasChecked() will return true if book is checked out or false, otherwise.
public boolean hasChecked(Book newBook)
{
if (book1==null && book2 == null && book3==null)
return false;
if (book1!=null && book1.getTitle().equalsIgnoreCase(newBook.getTitle()))
return true;
else if (book2!=null && book2.getTitle().equalsIgnoreCase(newBook.getTitle()))
return true;
else if (book3!=null && book3.getTitle().equalsIgnoreCase(newBook.getTitle()))
return true;
else
return false;
}
//checked) will check out a book, unless it was already been checked out.
public boolean checked(Book newBook)
{
if (hasChecked(newBook))
{
System.out.println("Sorry. The book is not in your possession");
return false;
}
if (book1==null)
book1=newBook;
else if(book2==null)
book2=newBook;
else
book3=newBook;
return true;
}
//returnBook() will check out a book, unless it is not in the client's possession.
public boolean returnBook(Book newBook)
{
if(hasChecked(newBook))
{
return true;
}
if (!hasChecked(newBook))
{
System.out.println("Sorry. The book is not in your possession.");
return false;
}
if (book1.getTitle().equalsIgnoreCase(newBook.getTitle()))
book1=null;
else if(book2.getTitle().equalsIgnoreCase(newBook.getTitle()))
book2=null;
else
book3=null;
return true;
}
//toString() will return the patron's name and their list of books.
public String toString()
{
if (book1==null && book2==null && book3==null)
return name + "has no checked-out books at this time.";
String finalString = name;
if (book1!=null)
finalString += " " + book1.toString();
if (book2!=null)
finalString += " " + book2.toString();
if (book3!=null)
finalString += " " + book3.toString();
return finalString;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.