CIT 590 Assignment 8:
Library
Spring 2010, David Matuszek
This assignment is similar to, but not identical with, one that you have done recently in Python. There are a lot of minor changes to transition from Python to Java, and a few to just improve the program design.
You are going to implement software for your local library. The user of the software will be the librarian, who uses your program to issue library cards, check books out to patrons, check books back in, send out overdue notices, and open and close the library.
In a number of methods, we will make a distinction between what the
program appears to do (its behavior), and what it actually does (its
implementation). For example, when we "issue
a library card" to a patron, we don't create some kind of a "library card
object" and give it to a Patron object; the implementation will achieve the
same result by simply recording the patron's name in a HashMap. The
visible behavior is the same: A person who has been "issued a library card"
will be able to check out books. Don't let yourself be confused by the
differences between the behavior and the implementation.
I don't think you will need any additional classes, but you are likely to need some additional methods, not listed below. Feel free to write more methods (but remember to test them).
class CalendarGregorianCalendar class, but to keep
things simple, we will just use an integer (starting from 0) to keep
track of how many days have passed. Declare this variable as private int date within the class itself, not within any method or constructor of the class.Calendar()int getDate()void advance()class Booktitle, its author (only one author
per book), and its dueDate.
The due date is -1 if the book is not checked
out. These should be instance variables.
Book(String title, String author)String getTitle()String getAuthor()int getDueDate()void checkOut(int date)void checkIn()-1. It is not this Book's job to return itself
to the Library's collection of available books.@Override
public String toString()title, by
author.class PatronPatron(String name, Library library)Library object that he/she uses.String getName()void take(Book book)void giveBack(Book book)return, but I hope you can see why I can't do that.) ArrayList<Book> getBooks()Book objects checked out to this Patron (may be an
empty list).@Override
public String toString()getName() method!)class OverdueNoticeOverdueNotice(Patron patron, int todaysDate)@Override
public String toString()class Libraryprint or println methods described below to do their printing, not the System.out versions.
public Library()okToPrint to true. title :: authorBook from each line, and save these in an ArrayList<Book>. These Books form the
library's collection. Do
not assume that there is only one copy of each book.HashMap (whose keys will be the names of patrons and whose
values will be the corresponding Patron objects). public Library(ArrayList<Book> collection)okToPrint to false, so that the unit tests can be "cleaner" and not do any I/O. With this constructor, the user isn't asked for a list of books to read in; the list is supplied as a parameter. (It is okay to call this constructor as often as desired.)public static void main(String[] args)Library object and calls its start method.void start()open or search saga, call the corresponding method, and print the results.void print(String message)okToPrint is true, prints the message using System.out.print(message), otherwise this method returns without doing anything.void println(String message)allowPrinting okToPrint is true, prints the message using System.out.println(message), otherwise this method returns without doing anything.ArrayList<OverdueNotice> open()createOverdueNotices.ArrayList<OverdueNotice> createOverdueNotices()Patron issueCard(String nameOfPatron)HashMap, with the patron's name as the key. No patron
can have more than one library card. The created Patron object is returned as the value of the method.Patron serve(String nameOfPatron)HashMap, and save the returned Patron object in an instance variable of this
Library. In addition, the method should return the Patron object.ArrayList<Book> checkIn(int... bookNumbers)Patron
(there must be one!), so return them to the collection and remove them from
the list of books currently checked out to the patron. The
bookNumbers are taken from the list printed by the
serve command. Checking in some books should not alter the printed book numbers of any other books. Checking in a Book will involve both telling the Book that it is checked in and
returning the Book to this Library's collection of available
Books. Returns a list of the books just checked in. ArrayList<Book> search(String part)ArrayList<Book> of books whose title or author (or both)
contains this string. For example, the string "tact" might
return, among other things, the book Contact, by Carl Sagan.
The search should be case-insensitive; that is, "saga"
would also return this book. Only books which are currently available
(not checked out) will be returned. In addition, to keep from returning
too many books, require that the search string be at least 4 characters
long. If multiple copies of a book are found (for instance, three copies of Contact), only one copy should be printed. Returns a list of the books just found. ArrayList<Book> checkOut(int... bookNumbers)Patron currently
being served (there must be one!), or tells why the operation is not
permitted. The bookNumbers are one or more of the books
returned by the most recent call to the search method.
Checking out a Book will involve both telling the Book that it
is checked out and removing the Book from this Library's
collection of available Books. Returns a list of the books just checked out. void close()quit) can be used when the library is
closed.void quit()Checking books in and out is slightly complex, so here is what the librarian needs to know:
serve the patron. This will print a numbered list of books
checked out to that patron.checkIn the books by the
numbers given above.serve the patron. You can ignore the list of books that this
will print out.search for a book wanted by
the patron.checkOut zero or more books
by the numbers returned from the search command.search.Two of the methods above, checkOut and
checkIn, have an elllipsis ("...") in front of the parameter. That
elllipsis is new syntax--it means that, when you call the
method, you can give as many book numbers as you please; you don't have to
give just one. Inside the method, the parameter
bookNumbers is an array of all the numbers
(zero or more) that you gave it.
Use Eclipse to create the Library project, the library package, and all the classes. Then add to each class a "stub" for each method. A stub is a method that does nothing or, if it must return something, returns a seriously inappropriate value (null for objects).
I will provide unit tests as soon as I can (almost certainly this weekend).
Java style requires that each class be put in a separate file. Eclipse does this automatically when you create a new class. Tests should normally be kept separate from the program being tested.
Instance variables are variables that belong to an object, and instance methods are methods that are defined for an object. Instance variables and methods may be prefixed with this. , when used by the object itself, but usually you only need to do this to distinguish an instance variable from a local variable or parameter with the same name.
When an object uses the methods of another object, those methods must be prefixed by a variable that refers to the object; for example, book.getTitle().
You and your partner should decide who is to turn in the program, and you should submit one program for the two of you. Both of you will receive the same grade on the program.