Generics & Collections


Generics & Collections

Java API
ArrayList


ListExample.java

read ch 16.1-16.7, 20


Classwork


Create a class Book, which has author and title, and number of copies.  It will need equals, copy constructor, and clone.  Two books are equal if they have the same author and title, but copies don't matter  (remember that equals and copying should be consistent with each other).


Create a class Library, which has a List of Books inventory.  As when we have arrays, we often don't provide accessors/mutators for Collection instance vars -- it gives too much power)

In the constructor for Library, set up the inventory to be an ArrayList, capacity 200. 

The toString can just use the result of the inventory's toString (not pretty, but it works).

In Library, provide a method checkIn, which takes a book.  If it is not in the inventory, add it.  If it is already in the inventory, just update the number of copies. You do not need to do any looping to do this, just use the List's methods.

Also provide a method checkOut,  which takes an author and title.  Check if a book with that author and title is in the inventory (hint: you can always make a book with the right values).  If there are multiple copies, return a copy and decrease the number in the inventory; if there is only one left in the inventory, remove it from the inventory and return it.  Return null if the book is not there.  You do not need to do any looping to do this, just use the List's methods.

Also provide a method curate, which takes an author.  Using an iterator, go through and remove all books with that author.

In a main, create a Library branch1 and create 30 books:  "Title 0" by "Author 0" , "Title 1" by "Author 1" etc.  For each one, have branch1 checkIn a random number of copies from 1 to 3. 

Then curate to remove authors divisible by 7.

Then randomly checkOut 5 books.  Then randomly checkIn 5 books.  When randomly choosing books, generate them so they may or may not already be in the library (so you might allow up to "Title 39" by "Author39").


[EC] Create a class LibraryPatron, which has a name and a List of Books current. 

To LibraryPatron, add a method checkOut which, given an author, a title, and a Library, tries to checkOut that book from that library and add it to their current list (no special behavior for multiple copies of the same book).  No LibraryPatron is allowed to currently have more than 15 books checked out. Print appropriate messages if the library doesn't have the book, or if the patron has too many books checked out already.

Give LibraryPatron  a method returnBatch, which takes a Library.  The first five (or all, if there are fewer) of their currently checked out books should be checked in to that library, removing them from the current list.  (use an iterator).   

In main, create two LibraryPatrons, and have one check out 8 randomly chosen books, and the other 20 from branch1. Create a second Library branch2 (which starts off empty).  Have each of the LibraryPatrons return one batch of books to branch1 and one batch to branch2.  In this code, have the patrons randomly choose books that may or may not be in branch1.