In this assignment you will 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.
class Calendar
We need to deal with the passage of time, so we need to keep
track of Java. Declare this variable as private int date within the
class itself, not within any method or constructor of the
class.
We only need a couple of methods:
Calendar()
The constructor. Sets the date to 0. You should create
one and only one Calendar object; time is the same
for everyone.
int getDate()
Returns (as an integer) the current date.
void advance()
Increment the date (move ahead to the next day).
class Book
A book has these attributes (instance variables): its title, 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)
The constructor. Saves the provided information. When created,
this book is not checked out.
String getTitle()
Returns this book's title.
String getAuthor()
Returns this book's author.
int getDueDate()
Returns the date (as an integer) that this book is due.
void checkOut(int date)
Sets the due date of this Book to the specified date. That's
all. It is not this Book's job to remove itself from the
Library's collection of available books.
void checkIn()
Sets the due date of this Book to -1. That's all. It is
not this Book's job to return itself to the Library's
collection of available books.
@Override
public String toString()
Returns a string of the form title, by
author.
class Patron
A patron is a "customer" of the library. A patron must have a
library card in order to check out books. A patron with a card may
have no more than three books at any time.
Patron(String name, Library library)
Constructs a patron with the given name, and no books. The
patron must also have a reference to the Library object that he/she
uses (therefore, you must have a Library before you can have any
Patrons).
String getName()
Returns this patron's name.
void take(Book book)
Adds this book to the list of books checked out by this
Patron.
void giveBack(Book book)
Removes this Book object from the list of books checked out by
this Patron. (Since patrons are usually said to "return" books, I
wish I could name this method return, but I hope you can see why I
can't do that.)
ArrayList<Book> getBooks()
Returns the list of Book objects checked out to this Patron (may
be an empty list).
@Override
public String toString()
Returns this patron's name. (Yes, this is the same as the
getName() method!)
class OverdueNotice
Represents an "overdue notice" that contains a list of all the
books checked out to this user, along with their due dates, and
indicates which ones are overdue.
OverdueNotice(Patron patron, int todaysDate)
Constructs an overdue notice for the given Patron. (What it
actually does is save the patron in an instance variable,
and saves today's date in another instance variable.)
@Override
public String toString()
Returns as a String, in a nice, humanly readable format, an
overdue notice. The notice should list all the books currently
checked out by the patron, along with their due dates, and call
attention to which ones are overdue. (This method allows the Patron
to easily print out the overdue notice.)
class Library
This is the "master" class. It has a number of methods which are
called by the "librarian" (the user), not by patrons. The librarian
does all the work. Since these methods (other than the constructor)
will be typed in by the librarian, all the parameters must be
strings or numbers, not objects (how would you type in a Book
object?). Furthermore, to reassure the librarian that his/her
action has worked, all these methods should result in an
informative printout, for example, "Library card issued to Amy
Gutmann."
To enable I/O free unit testing, all these methods should call
either the local print or println methods described below to do
their printing, not the System.out versions.
public Library()
This is the constructor that will be used by the main method,
once, to "create the library. It also
sets a private instance variable okToPrint to true.
To construct the library object, read in from file books.txt a list
of title :: author lines, create a Book 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.
Of course, when the library is created, none of the books are
checked out. There are not yet any patrons, either, but you should
define an empty HashMap<String, Patron> whose keys will be
the names of patrons and whose values will be the corresponding
Patron objects.
public Library(ArrayList<Book> collection)
This is a second constructor, used by the test methods.
This constructor sets the private instance variable okToPrint to
false, so that the unit tests can be "cleaner" and not do any I/O.
This constructor does not read in a list of books; the list is
supplied as a parameter. (It is okay to call this constructor as
often as desired.)
public static void main(String[] args)
This creates one Library object and calls its start method.
void start()
In Java, the user cannot call methods directly
from the keyboard. This method will read commands from the user,
such as open or search saga, call the corresponding method, and
print the results. . Commands are:
- open Open the library in the morning. (This advances the
date.)
- issueCard Patron's name Issue a library card.
- serve Patron's name Prepare to check books in or out. Prints a
numbered list of books currently checked out to this Patron. (This
command remains in effect until it's time to serve another Patron,
or to close the Library.)
- checkIn Book numbers Check in books (by number) held by
user.
- search search string Search for books, and print a numbered
list of books that are found.
- checkOut Book numbers Check out books (by number) found by
search.
- close Close the library in the evening.
- quit Exit this program.
Every command, whether successful, unsuccessful, or illegal,
should inform the librarian of the result
void print(String message)
If the instance variable okToPrint is true, prints the message
using System.out.print(message), otherwise this method returns
without doing anything.
void println(String message)
If the instance variable okToPrint is true, prints the message
using System.out.println(message), otherwise this method returns
without doing anything.
ArrayList<OverdueNotice> open()
Starts the day (by advancing the calendar). Then sends overdue
notices to all patrons with overdue books (by calling the next
method, and printing the results). Sets an instance variable to
indicate that the library is now open. Returns the
list of notices that it got from calling createOverdueNotices.
ArrayList<OverdueNotice> createOverdueNotices()
Checks each Patron to see whether he/she has books which were
due yesterday. For each such patron,
creates and returns a list of
overdue notices. (The librarian doesn't have to do this as a
separate command; it's done whenever the library is opened.)
Patron issueCard(String nameOfPatron)
Issues a library card to the person with this name. (What this
actually does is, it creates a Patron object, and saves it
as the value in a 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)
Begin checking books out to (or in from) the named patron. The
purpose of this method is so that the librarian doesn't have to
type in the person's name again and again for every book that is to
be checked in or checked out. What the method should actually do is
look up the patron's name in the 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)
The listed books are being returned by the current 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)
Prints out, and saves in an instance variable,
an 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)
Either checks out the book to the 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()
Shut down operations and go home for the night. None of the
other operations (except quit) can be used when the library is
closed.
void quit()
End the program. The mayor, citing a budget crisis, has stopped
all funding for the library. Can happen at any time.
Checking books in and out is slightly complex, so here is what
the librarian needs to know:
To check books in:
1. If you have not already done so,
serve the patron. This will print a numbered list of books checked
out to that patron.
2. checkIn the books by the numbers
given above.
To check books out:
1. If you have not already done so,
serve the patron. You can ignore the list of books that this will
print out.
2. search for a book wanted by the
patron.
3. checkOut zero or more books by the
numbers returned from the search command.
4. If more books are desired, you can
do another search.