/** * CS149 - Programming Fundamentals * Department of Computer Science * James Madison University * @version Fall 2019 */
toString
methods that use string formatting.This assignment must be completed individually. Your submission must conform to the JMU Honor Code. You may discuss the project with other students at a high level, but direct help is limited to general discussion on Piazza, the lab assistants assigned to CS149, and the instructor. Copying work from another student or the Internet is an honor code violation and will be grounds for a reduced or failing grade in the course.
This assignment is inspired by the Little Free Library organization which has managed to charter small "libraries" like the one shown above all across the world. Each "library" encourages patrons to take a book or leave a book to promote accessibility of books and reading in general. They are community spaces, many with benches or seats nearby and all are maintained by volunteers. There are eight in Harrisonburg. The one at Thomas Harrison Middle School was built by the JMU Art Department. The one shown above is the Elizabeth Street Little Free Library, and the data file you will be using in the assignment is based on the books available at this writing in the Elizabeth Street box.
In this assignment, imagine that the Little Free Library organization has contracted with the JMU Computer Science department to track how much money they could earn if they were a commercial organization that charged late fees for overdue books. This information will then be used for local LFL librarians to apply for funding from other non-profit foundations or the government. Each book and patron will be modeled by classes with relevant data and methods as outlined in the UML diagram and descriptions shown below. The Library class will then have arrays of books and patrons along with methods that allow for books to be checked in, checked out, and found in addition to determining fines. Your program will be based on the following files:
patrons.txt - Example patrons; feel free to add yourself to this file!
Library.java - You must write this class from scratch.
You don't actually need the Driver to complete the assignment; it is simply provided to show how your classes might be used in a larger application. The following UML diagram illustrates the relationships between the classes.
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.
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."
The Patron constructor will be used to initialize all attributes of the Patron object.
The adjustBalance
will update the current
balance of the patron. For example if the current balance of a
Patron is 2.0 and the amount is 4.5, this method should assign 6.50
to this.balance and return 6.50.
The equals
method checks whether two
patrons have the same id number. If the given object is a Patron,
compare this.idNumber with the other patron's idNumber. If the given
object is an Integer, compare this.idNumber with the Integer.
The toString
method should return a
String representation of the Patron. If a patron is initialized with
the following parameters ("bob", "eee", 2,
5.5), it should return a String exactly as follows: "Name: bob, Email:
eee, ID: 2, Balance: $5.50."
The Library constructor will initialize an array of Book and Patron objects using the book and patron array parameters.
The checkin
method will check in a
book upon return. If the returned book belongs in the Library, and
it is currently checked out, this method should update the status of
the book, reset its patron and due date, determine the fine for this
book (if any), and update the patron's balance. The return value
indicates success or failure. For example, if the book is not in the
library, checkin should return false.
The checkout
method will let a patron
checkout a book that is available in the Library. If the book
belongs to the library and is available, this method will update
it's status and patron, and set the due date to ten days from
today's date. The return value indicates success or failure. For
example, if the book is not in the library, checkout should return
false.
The determineFine
method will compute the fine
currently due for a book that is checked out. For this method, the
fine rate is $0.50 per day for each day after the due date.
The searchBooks
method searches for books, depending on
the key and type, and returns an array of the books found. If no
books are found, the resulting array will have a length of zero.
When type is OVERDUE_SEARCH, key will be a Date object. The method should return all books that will be overdue on that date (if not checked in before then).
*Do not implement any public methods other than what you see in the UML diagram. For example, you should not have a Patron.getBalance()method, even for testing purposes. You may add helper methods, but they must be private.
Do not use any static/instance variables in your test classes. JUnit will run your test methods in a random order, so it's best to use all local variables to avoid corrupting test data.
Make sure you're using the required constants Book.AVAILABLE, Library.TITLE_SEARCH, etc. Your test code should not use magic numbers like 1 or 2, or magic characters like 'A' or 'T'.
JUnit's assertEquals method automatically calls the object's equals method. So instead of writing assertTrue(book1.equals(book2)), you should just write assertEquals(book1, book2).
To implement the equals methods (in both Book and Patron), you'll need to use the instanceof operator. For example:
if (other instanceof Book) {
Book b = (Book) other;
...
}
You'll need to import the java.util.Date class. When you construct a new Date(), it will automatically contain today's date. If you want a relative date in the past, call DateUtils.addDays with a negative number of days.
The provided DateUtils class has all the methods you'll need for adding and subtracting dates. There's no need to reinvent the wheel! You should not be using any Date methods that are deprecated.
In searchBooks, you'll need to search the books array twice: once to count the number of books that will match the search, and a second time to build the resulting array.
Don't wait until the last day to finish this assignment! You'll need some time to prepare for the final exam and ask questions about anything you don't understand.
Submit Book.java and BookTest.java, Patron.java and PatronTest.java via https://autolab.cs.jmu.edu
Submit Book.java and BookTest.java, Patron.java and PatronTest.java, Library.java and LibraryTest.java via https://autolab.cs.jmu.edu.