Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this homework you will implement a Library class that uses your Book and Person class from homework 2, with slight modifications. The Library class

In this homework you will implement a Library class that uses your Book and Person class from homework 2, with slight modifications. The Library class will keep track of people with membership and the books that they have checked out.

Book.java

You will need to modify your Book.java from homework 2 in the following ways:

field: dueDate (private)

A String containing the date book is due. Dates are given in the format "DD MM YYYY", such as "01 02 2017" (Feb. 1, 2017.) This will help when calculating the number of days a book is overdue.

Field: checkedOut (private)

A Boolean that is true if the book is checked out and false otherwise.

Field: bookId (private)

An int that holds the unique Id for the book. Because it is unique it will not be changed so you can declare it as final. This will be used to distinguish separate copies of the same book (with the same title and author).

Field: bookValue (private)

A double that holds the current value of the book in dollars.

Replace the old constructor with one that has the following method hearder

public Book(String title, String author, int bookId, double bookValue)

Add accessors and mutators for each of the new fields except for a mutator for bookId. Note that getters for boolean fields use "is" instead of the normal "get". For example the getter for checkedOut in the book class should be called isCheckedOut(). Update the equals method to only use the bookId since it is unique for each book. We do this because it is possiblie to have multiple copies of the same book (same title and author) and in this set up they would not be equal. Also, update the toString method.

Person.java

You will need to modify your Person.java from homework 2 in the following ways:

Field: address (private)

A String that contains the address for the person, e.g. "101 Main St." For simplicity, do not worry about including the city, state, or zip code.

Field: libraryCardNum (private)

An int that is the unique library id number for the person. Note that this replaces the id field from homework 2. Once this is set it will never change, so you can make if final if you would like.

Change the old constructor so that it has the following header and sets the correct fields:

public Person(String name, String address, int libraryCardNum)

Create accessors for the fields and mutators for the address field. Update equals method and toString. The ArrayList will now be called checkedOut and be a list of books the Person has checked out from the library. The addBook method will change slightly (see below). You can update the other methods as needed but they will not be used for this assignment and they will not be tested.

public boolean addBook(Book b)

Add the Book b to the list of the current object's checked out books if and only if it was not already in that list. Return true if the Book was added; return false if it was not added.

Library

Field: libraryBooks (private)

An ArrayList that holds all books for the library. Note that the library may have multiple copies of the same book (same title and author) but the bookId for each copy must be unique.

Field: patrons (private)

An ArrayList that holds all the people who are patrons of the library.

Field: name (private)

A String that holds the name of the library

Field: numBooks (private)

An int that keeps track of the number of books that are currently available to be checked out. A book that is checked out is not included in this count.

Field: numPeople (private)

An int that keeps the number of patrons to the library.

Field: currentDate (private)

A String that represents the current date. Dates are given in the format "DD MM YYYY", such as "01 10 2016" (Oct. 1, 2016) just like the due date.

Constructor

Write a constructor that takes in a String for the name of the library.

Accessors

Write accessors for each of the fields.

Mutators

Write a mutator for each field except for numBooks and numPeople as these depend on the other fields and should only be updated when the fields are updated or accessed.

Public int checkNumCopies( String title, String author)

A method that takes in a string for the title and author of a book and returns the number of copies of this book in the library (both checked out and available)

Public int totalNumBooks()

Returns the number of books in the library (either checked out or not)

Public boolean checkOut(Person p, Book b, String dueDate)

A method that checks out the book to the given person only if the person is a patron and the book is in the library and not currently checked out. Note that the book object being passed in may be equal to a book in the library but not the actual book in the library. That means you need to be sure to update the book in the library not the book being passed into the method. It will also update the due date for the book object. It will return true if the book is successfully checked out and false otherwise.

Public ArrayList booksDueOnDate(String date)

A method that returns an Array List of books due on the given date.

Public double lateFee(Person p)

This method will calculate the late fee the person owes. The late fee is based on the value of the book and the number of days the book is overdue. The fee is 1% of the book value for every day the book is overdue. For example if a book is worth $20 and is nine days late the late fee will be $1.80. If the Person has multiple books overdue then the late fees are simply added together.

To calculate the number of days between the current date and the date the book is due, we recommend that you look at the GregorianCalendar class. Google the API for help. If you want to use/write another method to calculate the number of days between the current date and the due date you are welcome to do so! If you get ideas from online, please remember to site your sources at the top of your .java file(s).

Testing

You will need to write at least two JUnit tests for each of the following methods in the Library class:

checkNumCopies

checkOut

booksDueOnDate and

lateFee

You are encouraged to write tests for the other methods but we will not require it. You will need to submit these to Web-CAT along with the rest of your code. Use standard naming conventions for these JUnit tests (include the word 'test' in the name somewhere - such as "testCheckOut"), otherwise we do not mind what you call them. Remember, it would be best if you do not have more than one assert statement per JUnit test case (method). There is no upper limit on how many JUnit test cases you write. Place all your JUnit test cases in one single file ("JUnit Test Case") - you do not need separate files to test Book.java, Person.java, and Library.java.

Constraints

Each .java file must have the assignment name (Homework 3)

Do not put your classes into a package - leave it as default. (If you don't know what this means, don't worry about it.)

Observe the basic naming conventions you've been shown in class.

Your code must be correctly indented. Most code editors have a way of doing this for you. In Eclipse, select all text with CTRL-A (Command-A on the Mac), and then do Source->Correct Indentation (which is CTRL-I on Windows and Command-I on the Mac).

If two methods share identical logic, you should factor that out into a separate method (a helper method).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

What is React Query?

Answered: 1 week ago

Question

3. Evaluate your listeners and tailor your speech to them

Answered: 1 week ago