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.
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 Calendar
GregorianCalendar
class, but it's more complicated than we need. 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 Book
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)
String getTitle()
String getAuthor()
int getDueDate()
void checkOut(int date)
void checkIn()
-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()
title, by
author
.class Patron
Patron(String name, Library library)
Library
object that he/she uses (therefore, you must have a Library before you can have any Patrons). 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 OverdueNotice
OverdueNotice(Patron patron, int todaysDate)
@Override
public String toString()
class Library
print
or println
methods described below to do their printing, not the System.out
versions.
public Library()
main
method, once, to "create the library. It also sets a private instance variable okToPrint
to true
. title :: author
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. (Because reading a file is so messy in Java, here's a method you can use.) 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)
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)
Library
object and calls its start
method.void start()
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. 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)
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 ellipsis ("...
") in front of the parameter. That
ellipsis 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.
ArrayList<Book>
is a parameterized type (or "generic"), and means a list of books. HashMap<String, Patron>
is the type of a dictionary-like object--you "index" into it with the String, and get back the Patron. See the Java API for the methods you can use with these types (they are in the java.util
package).
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).
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()
.