This program models a library that allows user to search its inventory of books based on keywords (i.e., any words that match any in the book's author or title). This can be seen as a small step towards building something like amazon.com
There are several classes as part of this project, but the two classes you will modify are Book and Library. After they have been completed, you will write
several different Comparator classes that sort the search results in a variety of ways.
Complete the following exercises in order to make the library program functional.
loadData in the class Library such that it creates a Book object based on the data in a file of book information and then adds it to the library's inventory by calling the method insertBook. The format of the book data in the file is such that each book is represented by three lines: the title, the author, and the year.
equals in the class Book such that it returns true only if both books have the exact same title and author, and false otherwise.
insertBook in the class Library works. To help your explanation, draw a picture of the instance variable myInventory and show how it is updated in both the true and false cases of the conditional after each call made using this example data.
findBooks in the class Library such that it returns a list of all
books in the inventory that match the given keywords (i.e., by calling the
Book method matches).
matches in the class Book.
Give some test cases that would effectively check that this method works
correctly.
Currently, the results are printed in alphabetical order based on their title.
You can see how to to sort by any criteria, by creating a class
that defines how to compare two objects in a collection, in the
processByTitle method
of the Library class. To do this, you will need to create a class that implements
Comparator.
We have provided an example comparator in the class
TitleComparator. You should
write two new comparator sub-classes:
CountComparator: that orders books by the number available for checkout first,
and then by the author. Using processByTitle as a
model, Complete processByCount so that it displays the
results of sorting using CountComparator.
AuthorComparator: that orders books based on their author first, and then if those are
the same, by title; and if those are the same, by most recently
published. Complete processByAuthor so that it displays the
results of sorting using AuthorComparator.For the last part, you should make a copy of the project and rename it 18_libraryPart2_cps006_spring10 as you will make structural changes to the classes. You should turn in this project when you submit.
For this part, you should change the Book class so that it is Comparable, so that the object itself can be compared rather than passing a separate Comparator to the sort method. Specifically, you should change the class header to be:
If you add this line, the class will no longer compile because thepublic class Book implements Comparable
compareTo method must be implemented. You can let Eclipse
add the method stub for you by highlighting the error as in the image
below.
compareTo method in the Book
class so that orders elements based on the title.
processByTitle method to use Book's new
compareTo method through the one-argument Collections.sort method.