CompSci 6 - Classwork - Nov 29, 2006

Start by snarfing L1129.

Problem: Modeling a Library

This program models a library that allows a 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 in the package yourwork: 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.

Part 1: 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, author, and year, and false otherwise.
  3. In a README file, 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. In your README file, explain how both matches methods in the class Book work. Give some test cases that would effectively check that these methods work correctly.

Part 2: 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 process 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 shown below. You can run your program with only one comparator, so to test a new comparator, you will have to comment the current one out (see the Library process method) and uncomment the call to the new Comparator.

  1. One named CountComparator that orders books by the number available for checkout first, and then by the author
  2. One named 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

Submit

Create a README file with both of your names in it if you work with a partner.

Electronically submit the classwork through Eclipse your Java code and README file to the Lab38 folder.