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
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); } }
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