Question

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 a time. If the patron already has 3 books checked out, they should not be allowed to check out another book.

Book interface - Create an interface for a book. A book can be checked out and checked in. Remember that a Java interface can only contain method signatures (not method implementations). For this project, the interface helps to ensure that anything that should be treated as a "book" has the ability to be checked in and out. For example, maybe the way physical books and electronic books should handle being checked in and out differently. You might want to have different classes to define each type of book. However, if they both implement the Book interface, you can be sure that both classes will have the ability to check the book in and out, regardless of the implementation.

Book - Implement the Book interface. A book has a title, author, and due date, and can be checked in and out. Books are due one week (7 days) after being checked out. There are a number of different ways to handle storing the due date. One option would be to use the GregorianCalendar class, as described in Section 13.4. Another option might be to use the Date class together with the Calendar class.

Library - Create a menu so that the user can add new patrons and books, edit patron and book info, display patrons and books, and check out and check in books. In reality, it wouldn't be very practical to list out all patrons and books, but in our example we will only have a few of each so the list shouldn't be too overwhelming.

For example, you might start with a menu like this:

Please select from the following options:
1. Add new patron
2. Add new book
3. Edit patron
4. Edit book
5. Display all patrons
6. Display all books
7. Check out book
8. Check in book
9. Exit

Your menu should repeat until the user chooses to exit the program.

Please note that your "Edit patron" and "Edit book" options should only edit patron name, book author, and book title. These options should not be able to change the due dates of books, etc. Your "display all books" option should include whether or not the book is checked out.

For your edit (and also display) options, I would recommend printing out an index number so that the user can see/select which book/patron they want to edit. For example, if the user chooses to edit the patrons, your program could display this list of patrons and ask the user to select the patron they would like to edit:

PATRON LIST:
0 - John Smith
1 - Jane Doe
2 - Ken Adams
Please enter the ID of the patron to edit:

That way, the user can enter the ID, and you can find it fairly easily.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:-

package com.java.bookAssignment:

import java.util.ArrayList;

public class Patron {

private String firstName;

private String lastName;

private int maxBooks = 3;

private ArrayList<Book> books = new ArrayList<>();

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public Patron(String firstName, String lastName) {

this.firstName = firstName;

this.lastName = lastName;

}

// Borrow a book

public boolean checkout(Book book) {

if (books.size() <= maxBooks) {

books.add(book);

return book.checkout();

} else {

System.out.println("You have reached the limit of max books. Cant borrow more books");

return false;

}

}

// return a book

public boolean checkin(Book book) {

if (books.contains(book)) {

books.remove(book);

book.checkin();

return true;

} else {

System.out.println("Book doesnt exist so cant checkin");

return false;

}

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());

result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Patron other = (Patron) obj;

if (firstName == null) {

if (other.firstName != null)

return false;

} else if (!firstName.equals(other.firstName))

return false;

if (lastName == null) {

if (other.lastName != null)

return false;

} else if (!lastName.equals(other.lastName))

return false;

return true;

}

public ArrayList<Book> getBooks() {

return books;

}

@Override

public String toString() {

return "Patron firstName=" + firstName + ", lastName=" + lastName;

}

}

////////////////////////////////Patron.java//////////////////////////

//////////////////////////////////////////Book.java/////////////////////////////////////

package com.java.bookAssignment;

import java.util.Calendar;

import java.util.Date;

public class Book implements IBook {

public static final int AVAILABLE = 1;

public static final int UNAVAILABLE = 2;

private String title;

private String author;

private Date dueDate;

private int status;

/**

* Constructs a book from the given values.

*

* @param title

* the book's title

* @param author

* the book's author (or authors)

*/

public Book(String title, String author) {

this.title = title;

this.author = author;

this.dueDate = null;

this.status = AVAILABLE;

}

/**

* @return the title

*/

public String getTitle() {

return title;

}

/**

* @return the author

*/

public String getAuthor() {

return author;

}

public void setTitle(String title) {

this.title = title;

}

public void setAuthor(String author) {

this.author = author;

}

/**

* @return the due date

*/

public Date getDue() {

return dueDate;

}

/**

* Checks the book out. Due date is 7 days after issuing the books

*

*/

@Override

public boolean checkout() {

if (getStatus() != UNAVAILABLE) {

Date d = new Date();

Calendar c = Calendar.getInstance();

c.setTime(d);

c.setTime(new Date()); // Now use today date.

c.add(Calendar.DATE, 7); // Adding 5 days

this.dueDate = c.getTime();

this.status = UNAVAILABLE;

return true;

} else {

System.out.println("Book is already checked out.");

return false;

}

}

/**

* @return the basic info of the Book as a String

*/

public String toString() {

String s = "Title: " + title + ", " + "Author: " + author + ", " + " Due Date :" + dueDate;

return s;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((author == null) ? 0 : author.hashCode());

result = prime * result + ((dueDate == null) ? 0 : dueDate.hashCode());

result = prime * result + ((title == null) ? 0 : title.hashCode());

return result;

}

/**

* Compares this book to the specified object.

*

* @param other

* the object to compare this book against

* @return true if the books have the same Author , title

*/

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Book other = (Book) obj;

if (author == null) {

if (other.author != null)

return false;

} else if (!author.equals(other.author))

return false;

if (title == null) {

if (other.title != null)

return false;

} else if (!title.equals(other.title))

return false;

return true;

}

/**

* Checks the book in.

*/

@Override

public void checkin() {

this.dueDate = null;

this.status = AVAILABLE;

}

public int getStatus() {

return status;

}

public void setStatus(int status) {

this.status = status;

}

}

////////////////////////////////////ENd Book.java///////////////////////////////////

///////////////////////////////////////IBook.java//////////////////////////////////////

package com.java.bookAssignment;

//Interface for Book

public interface IBook {

public void checkin();

public boolean checkout();

}

/////////////////////////////ENd IBook.java//////////////////////

///////////////////////////////////Library.java//////////////////////////

package com.java.bookAssignment;

import java.util.ArrayList;

import java.util.Scanner;

public class Library {

private static ArrayList<Book> books = new ArrayList<>();

private static ArrayList<Patron> patrons = new ArrayList<>();

boolean isCheckedOut = false;

// Constructor used for unit test

public Library(ArrayList<Book> deesBooks, ArrayList<Patron> patrons) {

this.books = deesBooks;

this.patrons = patrons;

}

public boolean checkout(Book book, Patron patron) {

return patron.checkout(book);

}

public boolean checkin(Patron p, Book book) {

ArrayList<Book> p_books = p.getBooks();

for (int i = 0; i < p_books.size(); i++) {

if (p_books.get(i).equals(book)) {

if (book.getStatus() == Book.AVAILABLE) {

p.checkin(book);

}

}

}

return false;

}

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

String ans = "";

do {

System.out.println("Please select an option:");

System.out.println(

"1. Add new Patron \n2.Add new Book \n3.Edit Patron \n4.Edit Book \n5.Display all Patrons \n6. Display All books \n7.Checkout Book \n8. Checkin Book \n9. Exit");

int a = sc.nextInt();

sc.nextLine();

switch (a) {

case 1:

addPatron(sc);

break;

case 2:

addBook(sc);

break;

case 3:

editPatron(sc);

break;

case 4:

editBook(sc);

break;

case 5:

displayPatron();

break;

case 6:

displayBooks();

break;

case 7:

checkoutBook(sc);

break;

case 8:

checkinBook(sc);

break;

case 9:

System.exit(0);

}

} while (!ans.equalsIgnoreCase("n"));

}

private static void checkinBook(Scanner sc) {

displayBooks();

System.out.println("Please select an id to checkin book:");

int index = sc.nextInt();

sc.nextLine();

Book b = books.get(index);

displayPatron();

System.out.println("Please select an id check in book to patron:");

index = sc.nextInt();

sc.nextLine();

Patron p = patrons.get(index);

boolean result = p.checkin(b);

if (result) {

System.out.println("Book checked in successfully");

} else {

System.out.println("Book can not be checked in");

}

}

private static void checkoutBook(Scanner sc) {

displayBooks();

System.out.println("Please select an id to checkout book:");

int index = sc.nextInt();

sc.nextLine();

Book b = books.get(index);

displayPatron();

System.out.println("Please select an id check out book to patron:");

index = sc.nextInt();

sc.nextLine();

Patron p = patrons.get(index);

boolean result = p.checkout(b);

if (result) {

System.out.println("Book checked out successfully");

} else {

System.out.println("Book can not be checked out");

}

}

private static void editBook(Scanner sc) {

displayBooks();

System.out.println("Please select an id to edit:");

int index = sc.nextInt();

sc.nextLine();

Book b = books.get(index);

System.out.println("Enter new title :");

String title = sc.nextLine();

System.out.println("Enter new author:");

String author = sc.nextLine();

b.setAuthor(author);

b.setTitle(title);

}

private static void editPatron(Scanner sc) {

displayPatron();

System.out.println("Please select an id to edit:");

int index = sc.nextInt();

sc.nextLine();

Patron p = patrons.get(index);

System.out.println("Enter Patron new first Name :");

String fName = sc.nextLine();

System.out.println("Enter Patron new last Name :");

String lName = sc.nextLine();

p.setFirstName(fName);

p.setLastName(lName);

}

private static void displayBooks() {

for (int i = 0; i < books.size(); i++) {

System.out.println(i + " = " + books.get(i));

}

}

private static void displayPatron() {

for (int i = 0; i < patrons.size(); i++) {

System.out.println(i + "=" + patrons.get(i));

}

}

private static void addBook(Scanner sc) {

System.out.println("Enter author name:");

String author = sc.nextLine();

System.out.println("Enter book title");

String title = sc.nextLine();

Book b = new Book(title, author);

books.add(b);

}

private static void addPatron(Scanner sc) {

System.out.println("Add FIrst Name");

String fName = sc.nextLine();

System.out.println("Add Last Name");

String lName = sc.nextLine();

Patron p = new Patron(fName, lName);

patrons.add(p);

}

}

///////////////////////////////ENd Library.java//////////////////////////////////////////////

//////////////////////////////////LibraryTest.java/////////////////////////////////////////

package com.java.bookAssignment;

import java.util.ArrayList;

import java.util.Date;

import junit.framework.TestCase;

/**

* Tests Library: currently one test method.

*

* @author

* @version

*

*/

public class LibraryTest extends TestCase {

/** A single test for Library. **/

public void testLibrary() {

String title1 = "Starting Out With Java";

String author1 = "Tony Gaddis";

String title2 = "Starting Out With Java Edition 4";

String author2 = "T. Gaddis";

String title3 = "The C++ Language ";

String author3 = "Bjarne Stroustrup";

String title4 = "Think Python: ";

String author4 = "Allen B.";

Book b1 = new Book(title2, author2);

Book b2 = new Book(title1, author1);

Book b3 = new Book(title3, author3);

Book b4 = new Book(title4, author4);

ArrayList<Book> booksList = new ArrayList<>();

booksList.add(b1);

booksList.add(b2);

booksList.add(b3);

booksList.add(b4);

Patron p0 = new Patron("Dee A. B.", "Weikle");

Patron p1 = new Patron("Chris", "Mayfield");

Patron p2 = new Patron("Alvin", "Chao");

ArrayList<Patron> patronList = new ArrayList<>();

patronList.add(p0);

patronList.add(p1);

patronList.add(p2);

Library deesLibrary = new Library(booksList, patronList);

// Patron1 borrows 1 book and return it as well. Check the status

if (deesLibrary.checkout(b1, p1)) {

assertEquals("Library checkout1:", Book.UNAVAILABLE, booksList.get(0).getStatus());

if (deesLibrary.checkin(p1, booksList.get(0))) {

assertEquals("Library checkout5", Book.AVAILABLE, booksList.get(0).getStatus());

}

deesLibrary.checkout(b1, p1);

deesLibrary.checkout(b2, p1);

deesLibrary.checkout(b3, p1);

assertEquals("Library checkout1:", Book.UNAVAILABLE, booksList.get(0).getStatus());

assertEquals("Library checkout2:", Book.UNAVAILABLE, booksList.get(1).getStatus());

assertEquals("Library checkout3:", Book.UNAVAILABLE, booksList.get(2).getStatus());

assertFalse("Cant issue the book. You have reached max limit", deesLibrary.checkout(b4, p1));

assertEquals("Library checkout3:", Book.AVAILABLE, booksList.get(3).getStatus());

}

}

}

Add a comment
Know the answer?
Add Answer to:
Subject: Java Program You are writing a simple library checkout system to be used by a...
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
  • 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...

  • // C programming Create a system managing a mini library system. Every book corresponds to a...

    // C programming Create a system managing a mini library system. Every book corresponds to a record (line) in a text file named "mylibrary.txt". Each record consists of 6 fields (Book ID, Title, Author, Possession, checked out Date, Due Date) separated by comma: No comma '', "in title or author name. This mini library keeps the record for each book in the library. Different books can share the book "Title". But the "Book ID" for each book is unique. One...

  • 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 -...

  • 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...

  • Ashtown Public Library computerized their checkout system a few years ago. Patron requests for holds or...

    Ashtown Public Library computerized their checkout system a few years ago. Patron requests for holds or interlibrary loans, however, still are done at the circulation desk, and patrons still use the card catalogs to look up call numbers for books. The library received funds to add patron computers that would facilitate patrons personally carrying out these functions online. One of the library employees is concerned about the privacy of patrons and wants to know how a user system can be...

  • Draw a activity diagram for Use Case: Check Out Books Primary Actor: Worker Stakeholders and Interests:...

    Draw a activity diagram for Use Case: Check Out Books Primary Actor: Worker Stakeholders and Interests: Worker: wants fast, and easy check out to books. Patron: wants fast checkout, and does not want to be charged for books they did not checkout. Library: wants fast checkout of books, and wants to make sure that all books that leave the library have been checked out. Wants to allocate books fairly. Government: wants to protect investment in books and keep costs down....

  • A library system has three offices: A) a central administrative office, B) the book circulation desk,...

    A library system has three offices: A) a central administrative office, B) the book circulation desk, and C) the media circulation desk. The database is distributed so that patrons who check out primarily books are stored in a fragment with the book information and that the patrons who check out primarily media are stored with the media records. A query was executed at the central office for a list of all patrons who have any type of material that is...

  • Part I. Create a library check-out database using Microsoft SQL Server that stores data about books,...

    Part I. Create a library check-out database using Microsoft SQL Server that stores data about books, patrons, and the check-out process. Books (BookID, BookName, Author, YearPublished) Patrons (PatronsID, PatronsName, PatronsAddress, PatronsBirthday) CheckInOut (TransactionID, PatronID, BookID, CheckOutDate, NumDay, ReturnDate, Late, Fees, Paid) - the NumDay field contains the number of days patrons can keep the book, if the return date is over the number of day, then the Late field will have a Y value and a fee of $1.00 per...

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