CompSci 100E - Classwork 7 - Feb 17, 2011

In this classwork, you will write code to search and sort items based on different criteria. Snarf the 07ClwkLibrary 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. You can see sample data files here..
  2. Complete the method equals in the class Book such that it returns true only if both books have the exact same title, author, and year, and false otherwise.
  3. Explain how the method insertBook in the class Library works.
  4. Explain how the method matches in the class Book works.
  5. 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).

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.

Another Way - Making Book a Comparable object

For the last part, you should make a copy of the project and rename it 07ClwkLibrary2 as you will make structural changes to the classes.

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<Book>
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 07ClwkLibrary2 as clwk07-0217.