CompSci 6- Classwork 18 - April 1, 2010
10 pts
NOTE: This classwork must be completed by April 13.

In this classwork, you will write code to search and sort items based on different criteria. Snarf the 18_library_cps006_spring10 project or browse the code directory.

Modeling a Library

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.

Finding Books

Complete the following exercises in order to make the library program functional.

  1. Complete the method 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.
  2. Complete the method equals in the class Book such that it returns true only if both books have the exact same title and author, and false otherwise.
  3. Explain how the method 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.
  4. Complete the method 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).
  5. Complete the method matches in the class Book. Give some test cases that would effectively check that this method works correctly.

Sorting the Results

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:

  1. 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.
  2. 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:

public class Book implements Comparable
If you add this line, the class will no longer compile because the compareTo method must be implemented. You can let Eclipse add the method stub for you by highlighting the error as in the image below.

  1. Complete the compareTo method in the Book class so that orders elements based on the title.
  2. Change the processByTitle method to use Book's new compareTo method through the one-argument Collections.sort method.

Submitting

Submit only the revised project (part 2) as ClassworkApr01Library.