Question

In this assignment you will implement software for your local library. The user of the software...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.*;
public class Library
{
String libraryAddress;
ArrayList<Book> books;
public Library(String address)
{
libraryAddress = address;
books = new ArrayList<Book>();
}
public void addBook(Book book)
{
books.add(book);
}
public void printAddress()
{
System.out.println(libraryAddress);
}
// This method checks the books ArrayList to see if the book with the title exists.
// if it does and it's not borrowed, it borrows it and alerts the user they've successfully checked it out.
// If it does exists and its borrowed, it alerts the user that the book is already borrowed.
// If the book does not exist in the library, the user is alerted.
// My reason for making the return type of this method not void is so that I could immediately exit the method
// if the book was found in the library. The fact that it's a String is arbitrary; it could have been any data type
// since I return null for any case.
public String borrowBook(String bookTitle)
{
Book libraryBook;
String libraryBookTitle;
for(int i = 0; i < books.size(); i+=1)
{
libraryBook = books.get(i);
libraryBookTitle = libraryBook.getTitle();
if(libraryBookTitle.equals(bookTitle))
{
if(!(libraryBook.isBorrowed()))
{
libraryBook.borrowed();
System.out.println("You successfully borrowed " + libraryBookTitle);
return null;
}
else
{
System.out.println("Sorry, this book is already borrowed.");
return null;
}
}
}
System.out.println("Sorry, this book is not in our catalog.");
return null;
}
// This method walks through the books ArrayList and prints the title
// of any book object that is not borrowed. If the library is empty or
// all of the books are checked out, it will alert the user.
public void printAvailableBooks()
{
Book libraryBook;
boolean libraryIsEmpty = true;
for(int i = 0; i < books.size(); i+=1)
{
libraryBook = books.get(i);
if(!(libraryBook.isBorrowed()))
{
System.out.println(libraryBook.getTitle());
libraryIsEmpty = false;
}
}
if(libraryIsEmpty)
{
System.out.println("No books in catalog.");
}
}
// This method walks through the books ArrayList, searching for the input bookTitle.
// When found, the book object will be returned and the user will be notified. If the
// book is not found in the library, the user will be alerted.
public void returnBook(String bookTitle)
{
Book libraryBook;
String libraryBookTitle;
Boolean found = false;
for(int i = 0; i < books.size(); i+=1)
{
libraryBook = books.get(i);
libraryBookTitle = libraryBook.getTitle();
if(libraryBookTitle.equals(bookTitle))
{
if(libraryBook.isBorrowed())
{
libraryBook.returned();
System.out.println("You successfully returned " + libraryBookTitle);
found = true;
break;
}
}
}
if(!found)
{
System.out.println("Your book was not fond in the library catalog.");
}
}
public static void printOpeningHours()
{
System.out.println("Libraries are open daily from 9am to 5pm.");
}
public static void main(String[] args)
{
// Create two libraries
Library firstLibrary = new Library("10 Main St.");
Library secondLibrary = new Library("228 Liberty St.");
//Add four books to the first library
firstLibrary.addBook(new Book("The Da Vinci Code"));
firstLibrary.addBook(new Book("Le Petit Prince"));
firstLibrary.addBook(new Book("A Tale of Two Cities"));
firstLibrary.addBook(new Book("The Lord of the Rings"));
// Print opening hours and the addresses
System.out.println("Library hours:");
printOpeningHours();
System.out.println();
System.out.println("Library addresses:");
firstLibrary.printAddress();
secondLibrary.printAddress();
System.out.println();
// Try to borrow The Lords of the Rings from both libraries
System.out.println("Borrowing The Lord of the Rings:");
firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings");
secondLibrary.borrowBook("The Lord of the Rings");
System.out.println();
// Print the titles of all available books from both libraries
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
System.out.println();
System.out.println("Books available in the second library:");
secondLibrary.printAvailableBooks();
System.out.println();
// Return The Lords of the Rings to the first library
System.out.println("Returning The Lord of the Rings:");
firstLibrary.returnBook("The Lord of the Rings");
System.out.println();
// Print the titles of available from the first library
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
}

}

Add a comment
Know the answer?
Add Answer to:
In this assignment you will implement software for your local library. The user of the software...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will be writing a Library simulator involving multiple classes. You will write the LibraryItem, Patron,...

    You will be writing a Library simulator involving multiple classes. You will write the LibraryItem, Patron, and Library classes and the three classes that inherit from LibraryItem (Book, Album and Movie). All data members of each class should be marked as private and the classes should have any get or set methods that will be needed to access them. **USE PYTHON 3 ONLY!!! Here are descriptions of the three classes: LibraryItem: id_code - a unique identifier for a LibraryItem -...

  • Subject: Java Program You are writing a simple library checkout system to be used by a...

    Subject: Java Program You are writing a simple library checkout system to be used by a librarian. You will need the following classes: Patron - A patron has a first and last name, can borrow a book, return a book, and keeps track of the books they currently have checked out (an array or ArrayList of books). The first and last names can be updated and retrieved. However, a patron can only have up to 3 books checked out at...

  • In this project, you will construct an Object-Oriented framework for a library system. The library must...

    In this project, you will construct an Object-Oriented framework for a library system. The library must have books, and it must have patrons. The patrons can check books out and check them back in. Patrons can have at most 3 books checked out at any given time, and can only check out at most one copy of a given book. Books are due to be checked back in by the fourth day after checking them out. For every 5 days...

  • The CIS Department at Tiny College maintains the Free Access to Current Technology (FACT) library of...

    The CIS Department at Tiny College maintains the Free Access to Current Technology (FACT) library of e- books. FACT is a collection of current technology e-books for use by faculty and students. Agreements with the publishers allow patrons to electronically check out a book, which gives them exclusive access to the book online through the FACT website, but only one patron at a time can have access to a book. A book must have at least one author but can...

  • You are going to model a Book in a library. The Book class should contain a...

    You are going to model a Book in a library. The Book class should contain a title and an author (exactly 1 author is OK). Also, a book can be checked out and returned by a student. A Book object should be able to identify the student who currently holds the book. 1.Design the public interface (public constructor and public methods) for a class Book. You can use String to represent title, author, and student. 2. Implement the class Book...

  • You need to program a simple book library system. There are three java classes Book.java   //...

    You need to program a simple book library system. There are three java classes Book.java   // book object class Library.java   //library class A2.java //for testing The Book.java class represents book objects which contain the following fields title: a string which represents the book title. author: a string to hold the book author name year: book publication year isbn: a string of 10 numeric numbers. The book class will have also 3 constructors. -The default no argument constructor - A constructor...

  • Case Study:UniversityLibrarySystem This case is a simplified (initial draft) of a new system for the University...

    Case Study:UniversityLibrarySystem This case is a simplified (initial draft) of a new system for the University Library. Of course, the library system must keep track of books. Information is maintained about both book titles and the individual book copies. Book titles maintain information about title, author, publisher, and catalog number. Individual copies maintain copy number, edition, publication year, ISBN, book status (whether it is on the shelf or loaned out), and date due back in. The library also keeps track...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • Programming Assignment This is a database course using Open Office Database. Assume that you have been...

    Programming Assignment This is a database course using Open Office Database. Assume that you have been given the task of creating a system for a library to keep track of their books, the borrowers of the books, and the books that are currently lent out. Your first step will be to create the relations necessary for this system. Book will need information such as a unique identifier for each book, title, author, ISBN number, date of publication, cost. Borrower will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT