Library Revisited
You should modify the Library program done
previously such that it models a library's inventory that can contain more than just
books (e.g, other media such as CDs, videos, DVDs, newspapers, or even equipment
like projectors, DVD players, iPods, etc.). Additionally, when the user searches for
items by keyword, they should see the positive matches sorted by name, creator,
and number of copies available in the library.
Inheritance
There are currently only two classes defined for this activity:
Book
and Library. You should add at
least three more: one to model music albums (i.e,. CDs), one for an additional kind of
thing the library might lend, and a super-class for all items the library might
lend.
- A music album should include the names of all its tracks in addition to
the album's title, artist, and publication year. Two albums are equal when all of their attributes are equal (including track names and order). When printed, an
album should print all the basic information as well as each of its tracks. Likewise, when searching for
an album by keyword, the keywords should be able to match any word in any
track name.
To read in an album from a file,
the first three lines will be the same as those given for a book. The fourth line will give the number of tracks on the album and then be followed by that number of lines, one track name per line. Try making an additional constructor that takes a Scanner as a parameter and reads the complete data for an album.
- Design a library item super-class that represents all the commonalities
between a book and an album, such that those classes have a common set of
methods. Additionally, you will need to refactor each new sub-class so that the only
additional code in each is that which differs from any generic library item.
Your super-class should also have two constructors: one that takes just a Scanner object, and uses it to read the initial values of the instance variables from a file; and one that takes the necessary initial state values of the instance variables and simply assigns them. Also, consider whether it makes sense to create a generic library item (i.e., should the super-class be abstract)?
- For your additional item, try come up with something that has yet
different data from a Book or an album. Be creative and see if it fits
reasonably into the inheritance hierarchy you have created. Also try to come up with a reasonable format for it to be represented in a file.
- Finally, consider how you would add all of these objects to the Library's inventory. What methods are currently hard-coded to work for only Books? How would you change them to work for any library item you have made in this exercise? The most tricky method to modify will be the one that reads in the data from a file --- not because it is hard to read in the data differently (you should have already done that in your separate constructors), but because you need to have a way to decide which constructor to call (i.e., which kind of library item to create using new). Suggest a way to explicitly represent this choice in the format of file. Does it make sense to have files that mix item types (i.e., a file containing both books and albums)? How does this decision affect data files and the code you have written?
Sorting
Currently, the results are printed in the order they were added to the inventory. However, it is possible to sort by any criteria by
creating a class that defines how to compare two objects in a collection. 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 comparators:
- one that orders library items by the number available for checkout
- one that orders library items based on their creator first, and then if the
creator's are the same, by title