The objective of this assignment is to give you practice in using
inheritance in Java
In this assignment you will write a collection of classes that manage a library.
The library has a collection of items, and for simplicity we will assume this library only has books and DVDs. We will model the items in the library using three classes: an Item class that collects information and behavior that is common to both types of items (id, title, and number of copies available). You will also have Book and DVD classes that extend the basic Item class, where a book contains the name of the author, and number of pages, and a DVD will have a playing time in minutes.
The items in the library will be stored in a class called Catalog which will also allow patrons to search and borrow items.
Patrons are divided into two groups according to age: Youth and Adult. The difference between an adult and youth is in their quota, i.e. the number of items they are allowed to borrow (2 for Youth, 3 for Adult). The following diagram summarizes the relationships between the classes:
Item Patron Catalog
/ \ / \
DVD Book Youth Adult
Here are the details for the methods that each class needs to implement (note that in this assignment we are not modeling due dates):
The
Item class:
The item class stores the following information:
- An item's id (of class String) and provide an appropriate get method called getID()
- An item's title (of class String) and provide an appropriate get method called getTitle()
- The number of copies available for that item (of type int) in the library with a get method called getCopiesAvailable()
- boolean equals(Object other): returns true if the other object is of the same type, and has the same id, and false otherwise (make this method work regardless of which subclass of Item calls the method).
- The constructor for the class should have the following signature:
Item(String id, String title, int copiesAvailable)
The
Book class (extends Item) stores information that is specific to a book:
- The book's author with a get method called getAuthor(). The instance variable author should be a String.
- The number of pages in the book with a get method called getPages(). The instance variable numPages should be an int.
- The constructor for the class should have the following signature:
Book (String id, String title, int copiesAvailable, String, author, int numPages)
The
DVD class also extends Item and stores DVD-specific information:
- The playing time for the DVD with a get method called getPlayingTime() (the instance variable playingTime should be of type int).
- The constructor for the class should have the following signature:
DVD (String id, String title, int copiesAvailable, int playingTime)
The base class for library patrons is called
Patron and has the following methods and data:
- A patron's String id with an associated getID() get method.
- A patron's name (of class String) which can be accessed using getName().
- An arrayList<String> called borrowed that stores the the ids of the items borrowed by the patron.
- boolean borrow(String id): adds an item with the given id to the list of items borrowed by the patron if the number of items they have already borrowed is less than their quota. This method should return true if the operation was successful, i.e. the patron is able to borrow the item, and false otherwise.
- int getQuota(): returns the patron's quota (3 for Adult, 2 for Youth). Have the subclasses of Patron (Adult and Youth) define their quota by appropriately setting a quota variable.
- boolean returnItem(String id): removes the item with the given id from the list of borrowed items. Returns true if the item was on the list of borrwed items, and false otherwise.
- boolean belowQuota(): returns true if the number of items borrowed by a patron is less than their quota, and false otherwise.
- The constructor for the class Patron should have the signature Patron(String id, String name).
Your Patron class should have two subclasses:
Adult and
Youth whose quotas are 3 and 2, respectively.
The final class you need to implement represents the library's catalog. This class, called
Catalog, should have the following data and methods:
- arrayList<Item> called items that stores the items available in the library.
- A constructor Catalog(String fileName) that loads the library's
catalog from a file, and populates the items arrayList with the
items that the library has available and their number. Here's an
example catalog.
Each line represents an item in the catalog.
Tokens are delimited by semi-colons, and each token is of the form:
identifier=value, where identifier indicates which attribute it
refers to, and value is the value associated. For example
itemType=book, indicates that the line refers to an item which
should be stored in an object of class Book.
- boolean borrow(Patron patron, String itemID): when a user wants to borrow an item this is the method that gets called. This method should raise an exception if the itemID is not in the catalog. If the item is available, i.e. the number of such items is less than the number borrowed, the user's borrow method is called. If it is successful, the availability of the item is updated (the number of copies the library has at the library) and it returns true; false otherwise.
- boolean returnItem(Patron patron, String itemID): when a user wants to return an item this is the method that gets called.
This method should call the patron's returnItem method and update the number of copies available at the library if the user indeed borrowed the given item.
An exception should be thrown if itemID was not borrowed by the user, or the item is not in the catalog.
If the operation is successful the method should return true (which would happen if no exception is raised, so is sort of redundant).
- boolean available(String itemID): returns true if the item with
the given id has copies available for borrowing. It raises an
exception if the id is not one of an item in the catalog.
- arrayList<String> search(String title): returns all the IDs of items whose titles equal the given string.
To help you in writing your code, here is some
code for exercising your classes.
Submission:
Your classes should be stored in three Java files: Item.java,
Patron.java, and Catalog.java that need to be submitted as a single tar
file called pa6.tar and submitted via checkin as PA6.
Put the Youth and Adult classes in the file Patron.java, and Book and DVD in Item.java.
Update: you can also write your code such that each class is in its own file.
To create the tar file proceed as follows:
## Create a directory called PA6 using the command
mkdir PA6
Copy the .java files into that directory and then run the following command to create the tar file:
tar -cvf pa6.tar PA6
Now you are ready to submit
pa6.tar using
checkin.
Note that your code should compile on department Linux machines, and make sure that you are submitting the source code rather than the Java class file.