I have a \"Library\" program in Java where the user can enter Books or DVDs into
ID: 3837531 • Letter: I
Question
I have a "Library" program in Java where the user can enter Books or DVDs into a library, check in and check out, etc. Can someone please test and fix the code, the main program seems to have a few errors that I cannot figure out.
Here is what each class does
*****************************************************************************************
LibraryCard.java holds the ID, name and collection of BookCopies that are checked out. CheckIn and CheckOut methods.
LibraryCardManager.java contains a collection of LibraryCards and has methods for adding new library cards and lookup by cards by ID.
LibraryMaterials.java consists of a title and ISBN. For each type of LibraryMaterial, there is also a copy class -- Book and BookCopy, DVD and DVDCopy
LibraryMaterialsCopy.java is an abstract class. Has a Library card and due date, but does not contain a LibraryMaterial (unlike Book and BookCopy). Has a getLibraryMaterial() method which will return either a Book or a DVD, depending on the subclass.
Book.java holds the basic information of a book, ISBN, title and author
BookCopy.java refers to the actual copy of the book in the library. A library can have multiple BookCopies of the same book. This class contains a Book and a LibraryCard that is currently borrowing the book. It has an associated DueDate for the book and a balance, which is incurred if books are checked in past their due dates. It also has a renew method.
DVD.java holds all the basic DVD info, such as title, ISBN (for this project all DVD has a ISBN), and Main Actor.
DVDCopy.java same as BookCopy but for DVD
Catalog.java contain a mapping from LibraryMaterial to a collection of LibraryMaterialCopy. It keeps track of all the materials and copies of them in the library. Has methods such as adding new library materials, look for copies, lookup by title, see all available copies and return a collection of all library materials in the library. (I wrote the code but am unsure if it is correct)
main.java is the where the user interacts with the library. It has user input from the console.
****************************************************************************************
main.java
import java.time.LocalDate;
import java.util.Scanner;
public class main {
//public static Catalog catalog;
public static LibraryCardManager Card;
public static void main (String[] args) throws java.lang.Exception
{
// Set up the menu for the user to choose the action to perform
Catalog catalog = new Catalog();
Scanner input = new Scanner(System.in);
int selection = 0;
System.out.println("***************** WElCOME TO THE LIBRARY *******************");
System.out.println(" Please select one of the following options: ");
System.out.println("********* ***************************************** *********");
while (selection != 9){
System.out.println("1: Add Books or DVDs to the Library's Catalog");
System.out.println("2: Add a new LibraryCard");
System.out.println("3: Display a list of the Library Catalog");
System.out.println("4: Check-Out a Book or a DVD");
System.out.println("5: Display a list of Library Materials Checked-Out by you");
System.out.println("6: Check-In a Book or DVD by Material Title");
System.out.println("7: Check-out all Books & DVDs Checked-Out by you");
System.out.println("8: Renew a Book (only)");
System.out.println("9: Exit");
selection = input.nextInt();
switch(selection)
{
case 1 :
System.out.println("How many Books or DVDs Would You like to add to the Library's Catalog?");
int Copies = input.nextInt();
System.out.println("Enter 1 for Type BOOKS | Enter 2 to type DVDS");
int C1 = input.nextInt();
System.out.println("Enter the Material's Title");
String Title = input.next();
System.out.println("Enter the Material's ISBN");
String ISBN = input.next();
if (C1 == 1){
System.out.println("Enter The Book's Author");
String Author = input.next();
Book b = new Book(Title, ISBN, Author);
catalog.add(b, Copies);
}else if (C1 == 2){
System.out.println("Enter The DVD's Main Actor");
String mainActor = input.next();
DVD d = new DVD(Title, ISBN, mainActor);
catalog.add(d, Copies);
}
break;
case 2 :
System.out.println("Enter Your Name");
String Name = input.next();
System.out.println("Enter Your ID Number");
String ID = input.next();
LibraryCard card = new LibraryCard (ID, Name);
break;
case 3 :
catalog.availableCopies();
break;
case 4 :
System.out.println("Enter Your Library Card ID Number");
String ID1 = input.next();
System.out.println("Enter Title of Library Material you want to Check-Out");
String Title1 = input.next();
System.out.println("1. Book 2. DVD Enter the type");
int type = input.nextInt();
String isbn, author, mainActor;
System.out.println("Enter ISBN");
isbn = input.next();
DVD dvd;
Book book;
LibraryMaterialCopy copy;
if(type == 1)
{
System.out.println("Enter the author");
author = input.next();
book = new Book(isbn, Title1, author);
copy = new BookCopy(book);
}
else
{
System.out.println("Enter the main actor");
mainActor = input.next();
dvd = new DVD(isbn, Title1, mainActor);
copy = new DVDCopy(dvd);
}
LibraryCard CheckOutMaterial = Card.getCardByID(ID1);
CheckOutMaterial.checkOutLibraryMaterial(copy);
break;
case 5 :
System.out.println("Enter Your Library Card ID Number");
String ID2 = input.next();
LibraryCard CheckedOutMaterial = Card.getCardByID(ID2);
CheckedOutMaterial.getLibraryMaterialsSorted();
break;
case 6 :
System.out.println("Enter Your Library Card ID Number");
String ID3 = input.next();
System.out.println("Enter Material's Title to be Checked-In");
String Title2 = input.next();
LibraryCard ReturnMaterial = Card.getCardByID(ID3);
ReturnMaterial.returnMaterials(Title2);
break;
case 7 :
System.out.println("Enter Your Library Card ID Number");
String ID4 = input.next();
LibraryCard ReturnMaterial1 = Card.getCardByID(ID4);
case 8 :
System.out.println("Enter Your Library Card ID Number");
String ID5 = input.next();
System.out.println("Enter the Book's Title to Renew");
String Title3 = input.next();
LibraryCard RenewBook = Card.getCardByID(ID5);
RenewBook.renewLibraryMaterial(Title3);
break;
case 9:
System.exit(0);
}
}
}
}
LibraryMaterials.java
public class LibraryMaterial {
private String ISBN;
private String title;
public LibraryMaterial(String i, String t) {
ISBN = i;
title = t;
}
public String getISBN() {
return ISBN;
}
public String getTitle() {
return title;
}
public void print() {
System.out.print("ISBN: " + ISBN + " title: " + title + " ");
}
public boolean isTitle(String s) {
if(this.title.equals(s))
return true;
return false;
}
}
LibraryMaterialsCopy.java
import java.time.LocalDate;
public abstract class LibraryMaterialCopy {
protected LibraryCard card;
protected LocalDate dueDate;
public LibraryMaterialCopy() {
card = null;
dueDate = null;
}
public abstract LibraryMaterial getLibraryMaterial();
public abstract String getTitle();
public abstract String getISBN();
public abstract int getBorrowingWeeks();
public abstract double getFinePerDay();
public abstract boolean isRenewable();
public abstract boolean isTitle(String s);
public LibraryCard getCard() {
return card;
}
public LocalDate getDueDate() {
return dueDate;
}
public boolean checkOut(LibraryCard borrower, LocalDate dateOfBorrowing)
/*
* checks book out by setting card reference to borrower. returns false if
* book is already checked out sets due date to BORROWING_WEEKS after
* current date passed
*/
{
if (card != null)
return false;
card = borrower;
dueDate = dateOfBorrowing.plusWeeks(getBorrowingWeeks());
return true;
}
public boolean checkOut(LibraryCard borrower)
// default check out method that uses todays' date
{
return checkOut(borrower, LocalDate.now());
}
public boolean returnCopy()
// returns book by removing card reference
// returns false if there is no reference to a card
{
if (card == null)
return false;
card = null;
return true;
}
public void print() {
if (card != null) {
System.out.println("Checked out to: " + card.getCardholderName() + ", " + card.getID());
System.out.println("Due: " + dueDate);
}
}
}
LibraryCard.java
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;
private List<LibraryMaterialCopy> libraryMaterialsCheckedOut;
private double balance;
public LibraryCard(String i, String name) {
id = i;
cardholderName = name;
libraryMaterialsCheckedOut = new ArrayList<LibraryMaterialCopy>();
balance = 0;
}
public String getID() {
return id;
}
public String getCardholderName() {
return cardholderName;
}
public List<LibraryMaterialCopy> getlibraryMaterialsCheckedOut() {
return new ArrayList<LibraryMaterialCopy>(libraryMaterialsCheckedOut);
}
public void setCardholderName(String name) {
cardholderName = name;
}
public boolean checkOutLibraryMaterial(LibraryMaterialCopy libraryMaterial, LocalDate todaysDate) {
if (!libraryMaterial.checkOut(this))
return false;
libraryMaterialsCheckedOut.add(libraryMaterial);
return true;
}
public boolean checkOutLibraryMaterial(LibraryMaterialCopy libraryMaterial)
// default check out, uses today's date
{
return checkOutLibraryMaterial(libraryMaterial, LocalDate.now());
}
public boolean returnLibraryMaterial(LibraryMaterialCopy libraryMaterial, LocalDate returnDate)
// returns libraryMaterial and sends message to libraryMaterialCopy to do
// the same
// returns false if libraryMaterial is not checked out
// takes parameter that expresses the date of return
{
LocalDate dueDate = libraryMaterial.getDueDate();
if (!libraryMaterial.returnCopy())
return false;
if (!libraryMaterialsCheckedOut.remove(libraryMaterial))
return false;
long daysBetween = ChronoUnit.DAYS.between(dueDate, returnDate);
if (daysBetween > 0) // libraryMaterial is returned late
{
balance += libraryMaterial.getFinePerDay() * daysBetween;
}
return true;
}
public boolean returnLibraryMaterial(LibraryMaterialCopy libraryMaterial)
// default method, uses today's date as returns date
{
return returnLibraryMaterial(libraryMaterial, LocalDate.now());
}
public boolean renewLibraryMaterial(LibraryMaterialCopy libraryMaterial, LocalDate renewalDate)
// renews libraryMaterial. Returns false if libraryMaterial is not checked
// out already
// takes parameter that expresses date of renewal
// returns false if librayrMaterial is not a book
{
if (!libraryMaterialsCheckedOut.contains(libraryMaterial))
return false;
if (libraryMaterial.isRenewable()) {
if (!((BookCopy) libraryMaterial).renew(renewalDate))
return false;
return true;
}
return false;
}
public boolean renewLibraryMaterial(LibraryMaterialCopy libraryMaterial)
// default renewal method uses today's date as renewal date.
{
return renewLibraryMaterial(libraryMaterial, LocalDate.now());
}
public List<LibraryMaterialCopy> getlibraryMaterialsDueBy(LocalDate date)
// returns an List of libraryMaterials due on or before date
{
List<LibraryMaterialCopy> libraryMaterialsDue = new ArrayList<LibraryMaterialCopy>();
for (LibraryMaterialCopy libraryMaterial : libraryMaterialsCheckedOut) {
if (libraryMaterial.getDueDate().isBefore(date) || libraryMaterial.getDueDate().equals(date)) {
libraryMaterialsDue.add(libraryMaterial);
}
}
return libraryMaterialsDue;
}
public List<LibraryMaterialCopy> getLibraryMaterialsOverdue(LocalDate todaysDate) {
return getlibraryMaterialsDueBy(todaysDate.minusDays(1));
}
public ArrayList<LibraryMaterialCopy> getLibraryMaterialsOverdue()
// default method, returns libraryMaterials overdue as of today, which means
// that they
// were due by yesterday
{
return (ArrayList<LibraryMaterialCopy>) getLibraryMaterialsOverdue(LocalDate.now());
}
public ArrayList<LibraryMaterialCopy> getLibraryMaterialsSorted()
// returns List of libraryMaterials, sorted by due date (earliest due date
// first)
// uses insertion sort
{
List<LibraryMaterialCopy> libraryMaterials = new ArrayList<LibraryMaterialCopy>(libraryMaterialsCheckedOut);
for (int i = 1; i < libraryMaterials.size(); i++) {
int j = i;
while (j > 0 && libraryMaterials.get(j - 1).getDueDate().isAfter(libraryMaterials.get(j).getDueDate())) {
// swap elements in positions j and j-1
LibraryMaterialCopy temp = libraryMaterials.get(j);
libraryMaterials.set(j, libraryMaterials.get(j - 1));
libraryMaterials.set(j - 1, temp);
j = j - 1;
}
}
return (ArrayList<LibraryMaterialCopy>) libraryMaterials;
}
public void returnMaterials(String title) {
for(int i=0; i<libraryMaterialsCheckedOut.size(); i++){
if(libraryMaterialsCheckedOut.get(i).isTitle(title)){
returnLibraryMaterial(libraryMaterialsCheckedOut.get(i));
break;
}
}
}
public void renewLibraryMaterial(String title) {
for(int i=0; i<libraryMaterialsCheckedOut.size(); i++){
if(libraryMaterialsCheckedOut.get(i).isTitle(title)){
renewLibraryMaterial(libraryMaterialsCheckedOut.get(i));
break;
}
}
}
}
LibraryCardManager.java
import java.util.ArrayList;
public class LibraryCardManager {
private ArrayList<LibraryCard> libraryCards;
public LibraryCardManager() {
new ArrayList<LibraryCard>();
}
public void addLibraryCard(LibraryCard card){
libraryCards.add(card);
}
public LibraryCard getCardByID(String id){
for(LibraryCard card: libraryCards){
if(card.getID().equals(id))
return card;
}
return null;
}
}
Catalog.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
public class Catalog {
HashMap<LibraryMaterial,ArrayList<LibraryMaterialCopy>> map = new HashMap<LibraryMaterial,ArrayList<LibraryMaterialCopy>>();
public void add(LibraryMaterial m,int n){
ArrayList<LibraryMaterialCopy> copies = new ArrayList<LibraryMaterialCopy>();
if(m instanceof Book){
for(int i=0;i<n;i++){
LibraryMaterialCopy c = new BookCopy((Book)m);
copies.add(c);
}
}
else{
for(int i=0;i<n;i++){
LibraryMaterialCopy c = new DVDCopy((DVD)m);
copies.add(c);
}
}
map.put(m,copies);
}
public Collection<LibraryMaterialCopy> lookUpByMaterial(LibraryMaterial m){
if(map.containsKey(m))
return map.get(m);
return null;
}
public Collection<LibraryMaterialCopy> lookUpByTitle(String title){
for (LibraryMaterial m : map.keySet()) {
if(m.getTitle().equals(title)){
return map.get(m);
}
}
return null;
}
public Collection<LibraryMaterialCopy> availableCopies(LibraryMaterial m){
ArrayList<LibraryMaterialCopy> copies = map.get(m);
ArrayList<LibraryMaterialCopy> availablecopies = new ArrayList<LibraryMaterialCopy>() ;
for(LibraryMaterialCopy c: copies){
if(c.dueDate==null)
availablecopies.add(c);
}
return availablecopies;
}
public Collection<LibraryMaterial> availableCopies(){
ArrayList<LibraryMaterial> materials = new ArrayList<LibraryMaterial>(map.keySet());
return materials;
}
}
Book.java
public class Book extends LibraryMaterial {
private String author;
public Book(String i, String t, String a) {
super(i, t);
author = a;
}
public String getAuthor() {
return author;
}
public void print() {
super.print();
System.out.println("author: " + author);
}
}
BookCopy.java
import java.time.*;
public class BookCopy extends LibraryMaterialCopy {
private Book book;
public static final int BORROWING_WEEKS = 3;
public static final int RENEWAL_WEEKS = 2;
public static final double FINE_PER_DAY = .10;
public static final boolean IS_RENEWABLE = true;
public BookCopy(Book b) {
super();
book = b;
}
@Override
public LibraryMaterial getLibraryMaterial() {
return book;
}
@Override
public int getBorrowingWeeks() {
return BORROWING_WEEKS;
}
@Override
public double getFinePerDay() {
return FINE_PER_DAY;
}
@Override
public String getTitle() {
return book.getTitle();
}
@Override
public String getISBN() {
return book.getISBN();
}
public String getAuthor() {
return book.getAuthor();
}
@Override
public boolean isRenewable() {
return IS_RENEWABLE;
}
public boolean renew(LocalDate renewalDate) {
if (card == null)
return false;
dueDate = renewalDate.plusWeeks(RENEWAL_WEEKS);
return true;
}
public boolean renew()
// default method uses todays date as renewal date
{
return renew(LocalDate.now());
}
public void print() {
book.print();
super.print();
}
@Override
public boolean isTitle(String s) {
if(this.book.getTitle().equals(s)) {
return true;
}
return false;
}
}
DVD.java
public class DVD extends LibraryMaterial {
private String mainActor;
public DVD(String i, String t, String mA) {
super(i, t);
mainActor = mA;
}
public String getMainActor() {
return mainActor;
}
public void print() {
super.print();
System.out.println("Main actor: " + mainActor);
}
}
DVDCopy.java
public class DVDCopy extends LibraryMaterialCopy {
public static final int BORROWING_WEEKS = 2;
public static final double FINE_PER_DAY = 1;
public static final boolean IS_RENEWABLE = false;
private DVD dvd;
public DVDCopy(DVD d) {
super();
dvd = d;
}
@Override
public LibraryMaterial getLibraryMaterial() {
return dvd;
}
@Override
public String getTitle() {
return dvd.getTitle();
}
@Override
public String getISBN() {
return dvd.getISBN();
}
public String getMainActor() {
return dvd.getMainActor();
}
@Override
public int getBorrowingWeeks() {
return BORROWING_WEEKS;
}
@Override
public double getFinePerDay() {
return FINE_PER_DAY;
}
@Override
public boolean isRenewable() {
return IS_RENEWABLE;
}
public void print() {
dvd.print();
super.print();
}
@Override
public boolean isTitle(String s) {
if(this.dvd.getTitle().equals(s))
return true;
return false;
}
}
Explanation / Answer
PROGRAM CODE:
package library;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
//public static Catalog catalog;
public static LibraryCardManager Card;
public static void main (String[] args) throws java.lang.Exception
{
// Set up the menu for the user to choose the action to perform
Catalog catalog = new Catalog();
Card = new LibraryCardManager();
Scanner input = new Scanner(System.in);
Scanner choiceReader = new Scanner(System.in);
int selection = 0;
System.out.println("***************** WElCOME TO THE LIBRARY *******************");
System.out.println(" Please select one of the following options: ");
System.out.println("********* ***************************************** *********");
while (selection != 9){
System.out.println("1: Add Books or DVDs to the Library's Catalog");
System.out.println("2: Add a new LibraryCard");
System.out.println("3: Display a list of the Library Catalog");
System.out.println("4: Check-Out a Book or a DVD");
System.out.println("5: Display a list of Library Materials Checked-Out by you");
System.out.println("6: Check-In a Book or DVD by Material Title");
System.out.println("7: Check-In all Books & DVDs Checked-Out by you");
System.out.println("8: Renew a Book (only)");
System.out.println("9: Exit");
selection = choiceReader.nextInt();
switch(selection)
{
case 1 :
System.out.println("How many Books or DVDs Would You like to add to the Library's Catalog?");
int Copies = choiceReader.nextInt();
System.out.println("Enter 1 for Type BOOKS | Enter 2 to type DVDS");
int C1 = choiceReader.nextInt();
System.out.println("Enter the Material's Title");
String Title = input.nextLine();
System.out.println("Enter the Material's ISBN");
String ISBN = input.nextLine();
if (C1 == 1){
System.out.println("Enter The Book's Author");
String Author = input.nextLine();
Book b = new Book(ISBN, Title, Author);
catalog.add(b, Copies);
}else if (C1 == 2){
System.out.println("Enter The DVD's Main Actor");
String mainActor = input.nextLine();
DVD d = new DVD(ISBN, Title, mainActor);
catalog.add(d, Copies);
}
break;
case 2 :
System.out.println("Enter Your Name");
String Name = input.nextLine();
System.out.println("Enter Your ID Number");
String ID = input.nextLine();
LibraryCard card = new LibraryCard (ID, Name);
Card.addLibraryCard(card);
break;
case 3 :
catalog.availableCopies();
break;
case 4 :
System.out.println("Enter Your Library Card ID Number");
String ID1 = input.nextLine();
System.out.println("Enter Title of Library Material you want to Check-Out");
String Title1 = input.nextLine();
LibraryMaterialCopy material = catalog.lookUpByTitle(Title1).iterator().next();
if(Card.getCardByID(ID1).checkOutLibraryMaterial(material))
System.out.println(Title1 + " Checked out");
else System.out.println("Material not found in catalog");
break;
case 5 :
System.out.println("Enter Your Library Card ID Number");
String ID2 = input.nextLine();
LibraryCard CheckedOutMaterial = Card.getCardByID(ID2);
ArrayList<LibraryMaterialCopy> items = CheckedOutMaterial.getLibraryMaterialsSorted();
if(items.size() == 0)
System.out.println("There are no items checked out by you");
for(int i=0; i<items.size(); i++)
items.get(i).print();
break;
case 6 :
System.out.println("Enter Your Library Card ID Number");
String ID3 = input.nextLine();
System.out.println("Enter Material's Title to be Checked-In");
String Title2 = input.nextLine();
Card.getCardByID(ID3).returnMaterials(Title2);
break;
case 7 :
System.out.println("Enter Your Library Card ID Number");
String ID4 = input.nextLine();
Card.getCardByID(ID4).returnAllMaterials();
break;
case 8 :
System.out.println("Enter Your Library Card ID Number");
String ID5 = input.nextLine();
System.out.println("Enter the Book's Title to Renew");
String Title3 = input.nextLine();
Card.getCardByID(ID5).renewLibraryMaterial(Title3);
break;
case 9:
System.exit(0);
}
}
}
}
LibraryCard.java
package library;
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;
private List<LibraryMaterialCopy> libraryMaterialsCheckedOut;
private double balance;
public LibraryCard(String i, String name) {
id = i;
cardholderName = name;
libraryMaterialsCheckedOut = new ArrayList<LibraryMaterialCopy>();
balance = 0;
}
public String getID() {
return id;
}
public String getCardholderName() {
return cardholderName;
}
public List<LibraryMaterialCopy> getlibraryMaterialsCheckedOut() {
return new ArrayList<LibraryMaterialCopy>(libraryMaterialsCheckedOut);
}
public void setCardholderName(String name) {
cardholderName = name;
}
public boolean checkOutLibraryMaterial(LibraryMaterialCopy libraryMaterial, LocalDate todaysDate) {
if (!libraryMaterial.checkOut(this))
return false;
libraryMaterialsCheckedOut.add(libraryMaterial);
return true;
}
public boolean checkOutLibraryMaterial(LibraryMaterialCopy libraryMaterial)
// default check out, uses today's date
{
return checkOutLibraryMaterial(libraryMaterial, LocalDate.now());
}
public boolean returnLibraryMaterial(LibraryMaterialCopy libraryMaterial, LocalDate returnDate)
// returns libraryMaterial and sends message to libraryMaterialCopy to do
// the same
// returns false if libraryMaterial is not checked out
// takes parameter that expresses the date of return
{
LocalDate dueDate = libraryMaterial.getDueDate();
if (!libraryMaterial.returnCopy())
return false;
if (!libraryMaterialsCheckedOut.remove(libraryMaterial))
return false;
long daysBetween = ChronoUnit.DAYS.between(dueDate, returnDate);
if (daysBetween > 0) // libraryMaterial is returned late
{
balance += libraryMaterial.getFinePerDay() * daysBetween;
}
return true;
}
public boolean returnLibraryMaterial(LibraryMaterialCopy libraryMaterial)
// default method, uses today's date as returns date
{
return returnLibraryMaterial(libraryMaterial, LocalDate.now());
}
public boolean renewLibraryMaterial(LibraryMaterialCopy libraryMaterial, LocalDate renewalDate)
// renews libraryMaterial. Returns false if libraryMaterial is not checked
// out already
// takes parameter that expresses date of renewal
// returns false if librayrMaterial is not a book
{
if (!libraryMaterialsCheckedOut.contains(libraryMaterial))
return false;
if (libraryMaterial.isRenewable()) {
if (!((BookCopy) libraryMaterial).renew(renewalDate))
return false;
return true;
}
return false;
}
public boolean renewLibraryMaterial(LibraryMaterialCopy libraryMaterial)
// default renewal method uses today's date as renewal date.
{
return renewLibraryMaterial(libraryMaterial, LocalDate.now());
}
public List<LibraryMaterialCopy> getlibraryMaterialsDueBy(LocalDate date)
// returns an List of libraryMaterials due on or before date
{
List<LibraryMaterialCopy> libraryMaterialsDue = new ArrayList<LibraryMaterialCopy>();
for (LibraryMaterialCopy libraryMaterial : libraryMaterialsCheckedOut) {
if (libraryMaterial.getDueDate().isBefore(date) || libraryMaterial.getDueDate().equals(date)) {
libraryMaterialsDue.add(libraryMaterial);
}
}
return libraryMaterialsDue;
}
public List<LibraryMaterialCopy> getLibraryMaterialsOverdue(LocalDate todaysDate) {
return getlibraryMaterialsDueBy(todaysDate.minusDays(1));
}
public ArrayList<LibraryMaterialCopy> getLibraryMaterialsOverdue()
// default method, returns libraryMaterials overdue as of today, which means
// that they
// were due by yesterday
{
return (ArrayList<LibraryMaterialCopy>) getLibraryMaterialsOverdue(LocalDate.now());
}
public ArrayList<LibraryMaterialCopy> getLibraryMaterialsSorted()
// returns List of libraryMaterials, sorted by due date (earliest due date
// first)
// uses insertion sort
{
List<LibraryMaterialCopy> libraryMaterials = new ArrayList<LibraryMaterialCopy>(libraryMaterialsCheckedOut);
for (int i = 1; i < libraryMaterials.size(); i++) {
int j = i;
while (j > 0 && libraryMaterials.get(j - 1).getDueDate().isAfter(libraryMaterials.get(j).getDueDate())) {
// swap elements in positions j and j-1
LibraryMaterialCopy temp = libraryMaterials.get(j);
libraryMaterials.set(j, libraryMaterials.get(j - 1));
libraryMaterials.set(j - 1, temp);
j = j - 1;
}
}
return (ArrayList<LibraryMaterialCopy>) libraryMaterials;
}
public boolean returnMaterials(String title) {
for(int i=0; i<libraryMaterialsCheckedOut.size(); i++){
if(libraryMaterialsCheckedOut.get(i).isTitle(title)){
return returnLibraryMaterial(libraryMaterialsCheckedOut.get(i));
}
}
return false;
}
public void returnAllMaterials() {
for(int i=0; i<libraryMaterialsCheckedOut.size(); i++){
returnLibraryMaterial(libraryMaterialsCheckedOut.get(i));
}
}
public boolean renewLibraryMaterial(String title) {
for(int i=0; i<libraryMaterialsCheckedOut.size(); i++){
if(libraryMaterialsCheckedOut.get(i).isTitle(title)){
return renewLibraryMaterial(libraryMaterialsCheckedOut.get(i));
}
}
return false;
}
}
Catalog.java
package library;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
public class Catalog {
HashMap<LibraryMaterial,ArrayList<LibraryMaterialCopy>> map=new HashMap<LibraryMaterial,ArrayList<LibraryMaterialCopy>>();
public void add(LibraryMaterial m,int n)
{
ArrayList<LibraryMaterialCopy> copies=new ArrayList<LibraryMaterialCopy>();
if(m instanceof Book)
{
for(int i=0;i<n;i++)
{
LibraryMaterialCopy c=new BookCopy((Book)m);
copies.add(c);
}
}
else
{
for(int i=0;i<n;i++)
{
LibraryMaterialCopy c=new DVDCopy((DVD)m);
copies.add(c);
}
}
map.put(m,copies);
}
public Collection<LibraryMaterialCopy> lookUpByMaterial(LibraryMaterial m)
{
if(map.containsKey(m))
{
return map.get(m);
}
return null;
}
public Collection<LibraryMaterialCopy> lookUpByTitle(String title)
{
for (LibraryMaterial m : map.keySet()) {
if(m.getTitle().equals(title))
{
return map.get(m);
}
}
return null;
}
public Collection<LibraryMaterialCopy> availableCopies(LibraryMaterial m)
{
ArrayList<LibraryMaterialCopy> copies=map.get(m);
ArrayList<LibraryMaterialCopy> availablecopies=new ArrayList<LibraryMaterialCopy>() ;
for(LibraryMaterialCopy c: copies)
{
if(c.dueDate==null)
{
availablecopies.add(c);
}
}
return availablecopies;
}
public Collection<LibraryMaterial> availableCopies()
{
ArrayList<LibraryMaterial> materials=new ArrayList<LibraryMaterial>(map.keySet());
for(int i=0; i<materials.size(); i++)
materials.get(i).print();
return materials;
}
}
LibraryCardManager.java
package library;
import java.util.ArrayList;
public class LibraryCardManager {
private ArrayList<LibraryCard> libraryCards;
public LibraryCardManager() {
libraryCards = new ArrayList<LibraryCard>();
}
public void addLibraryCard(LibraryCard card)
{
libraryCards.add(card);
}
public LibraryCard getCardByID(String id)
{
for(LibraryCard card: libraryCards)
{
if(card.getID().equals(id))
return card;
}
return null;
}
}
OUTPUT:
***************** WElCOME TO THE LIBRARY *******************
Please select one of the following options:
********* ***************************************** *********
1: Add Books or DVDs to the Library's Catalog
2: Add a new LibraryCard
3: Display a list of the Library Catalog
4: Check-Out a Book or a DVD
5: Display a list of Library Materials Checked-Out by you
6: Check-In a Book or DVD by Material Title
7: Check-In all Books & DVDs Checked-Out by you
8: Renew a Book (only)
9: Exit
1
How many Books or DVDs Would You like to add to the Library's Catalog?
10
Enter 1 for Type BOOKS | Enter 2 to type DVDS
1
Enter the Material's Title
Sherlock Holmes
Enter the Material's ISBN
12345678
Enter The Book's Author
Sir Arthur Conan Doyle
1: Add Books or DVDs to the Library's Catalog
2: Add a new LibraryCard
3: Display a list of the Library Catalog
4: Check-Out a Book or a DVD
5: Display a list of Library Materials Checked-Out by you
6: Check-In a Book or DVD by Material Title
7: Check-In all Books & DVDs Checked-Out by you
8: Renew a Book (only)
9: Exit
2
Enter Your Name
Kaju
Enter Your ID Number
111
1: Add Books or DVDs to the Library's Catalog
2: Add a new LibraryCard
3: Display a list of the Library Catalog
4: Check-Out a Book or a DVD
5: Display a list of Library Materials Checked-Out by you
6: Check-In a Book or DVD by Material Title
7: Check-In all Books & DVDs Checked-Out by you
8: Renew a Book (only)
9: Exit
4
Enter Your Library Card ID Number
111
Enter Title of Library Material you want to Check-Out
Sherlock Holmes
Sherlock Holmes Checked out
1: Add Books or DVDs to the Library's Catalog
2: Add a new LibraryCard
3: Display a list of the Library Catalog
4: Check-Out a Book or a DVD
5: Display a list of Library Materials Checked-Out by you
6: Check-In a Book or DVD by Material Title
7: Check-In all Books & DVDs Checked-Out by you
8: Renew a Book (only)
9: Exit
5
Enter Your Library Card ID Number
111
ISBN: 12345678 title: Sherlock Holmes author: Sir Arthur Conan Doyle
Checked out to: Kaju, 111
Due: 2017-06-01
1: Add Books or DVDs to the Library's Catalog
2: Add a new LibraryCard
3: Display a list of the Library Catalog
4: Check-Out a Book or a DVD
5: Display a list of Library Materials Checked-Out by you
6: Check-In a Book or DVD by Material Title
7: Check-In all Books & DVDs Checked-Out by you
8: Renew a Book (only)
9: Exit
8
Enter Your Library Card ID Number
111
Enter the Book's Title to Renew
Sherlock Holmes
1: Add Books or DVDs to the Library's Catalog
2: Add a new LibraryCard
3: Display a list of the Library Catalog
4: Check-Out a Book or a DVD
5: Display a list of Library Materials Checked-Out by you
6: Check-In a Book or DVD by Material Title
7: Check-In all Books & DVDs Checked-Out by you
8: Renew a Book (only)
9: Exit
5
Enter Your Library Card ID Number
111
ISBN: 12345678 title: Sherlock Holmes author: Sir Arthur Conan Doyle
Checked out to: Kaju, 111
Due: 2017-05-25
1: Add Books or DVDs to the Library's Catalog
2: Add a new LibraryCard
3: Display a list of the Library Catalog
4: Check-Out a Book or a DVD
5: Display a list of Library Materials Checked-Out by you
6: Check-In a Book or DVD by Material Title
7: Check-In all Books & DVDs Checked-Out by you
8: Renew a Book (only)
9: Exit
7
Enter Your Library Card ID Number
111
1: Add Books or DVDs to the Library's Catalog
2: Add a new LibraryCard
3: Display a list of the Library Catalog
4: Check-Out a Book or a DVD
5: Display a list of Library Materials Checked-Out by you
6: Check-In a Book or DVD by Material Title
7: Check-In all Books & DVDs Checked-Out by you
8: Renew a Book (only)
9: Exit
5
Enter Your Library Card ID Number
111
There are no items checked out by you
1: Add Books or DVDs to the Library's Catalog
2: Add a new LibraryCard
3: Display a list of the Library Catalog
4: Check-Out a Book or a DVD
5: Display a list of Library Materials Checked-Out by you
6: Check-In a Book or DVD by Material Title
7: Check-In all Books & DVDs Checked-Out by you
8: Renew a Book (only)
9: Exit
9
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.