Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I created a Library in Java. The code has 10 classes. I need help with the Catalog class and the main program. Below is the

I created a "Library" in Java. The code has 10 classes. I need help with the Catalog class and the main program. Below is the code and 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

These next 2 classes is what I need help with.

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 (This is the main question. I need help writing main) is the where the user interacts with the library. It needs to be able to have user input from the console and needs to be able to do the following:

1- Add new books and DVDs to the library

2- Add new Library Cards

3- See all library materials in the catalog

4- Check out by entering the library card ID nad the title of the library materials. After the material is checked out, the due date should be displayed.

5- See all library materials copies checked out to the card, given the ID number

6- Return ("check in") a single library material copy, given title and library card ID

7- Return ("check in") all library material copies checked out to library card ID

8- Renew a book by specifying the title and the card ID. If the user enters the title of a DVD, a message should display saying DVDs cannot be renewed. After books are renewed, the due date should be displayed.

MY CODE

*****************************************

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 libraryMaterialsCheckedOut; private double balance; public LibraryCard(String i, String name) { id = i; cardholderName = name; libraryMaterialsCheckedOut = new ArrayList(); balance = 0; }

public String getID() { return id; }

public String getCardholderName() { return cardholderName; }

public List getlibraryMaterialsCheckedOut() { return new ArrayList(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 getlibraryMaterialsDueBy(LocalDate date) // returns an List of libraryMaterials due on or before date {

List libraryMaterialsDue = new ArrayList(); for (LibraryMaterialCopy libraryMaterial : libraryMaterialsCheckedOut) { if (libraryMaterial.getDueDate().isBefore(date) || libraryMaterial.getDueDate().equals(date)) { libraryMaterialsDue.add(libraryMaterial); } } return libraryMaterialsDue; }

public List getLibraryMaterialsOverdue(LocalDate todaysDate) { return getlibraryMaterialsDueBy(todaysDate.minusDays(1)); }

public List getLibraryMaterialsOverdue() // default method, returns libraryMaterials overdue as of today, which means // that they // were due by yesterday { return getLibraryMaterialsOverdue(LocalDate.now()); }

public List getLibraryMaterialsSorted() // returns List of libraryMaterials, sorted by due date (earliest due date // first) // uses insertion sort { List libraryMaterials = new ArrayList(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 libraryMaterials; } }

LibraryCardManager.java

import java.util.ArrayList;

public class LibraryCardManager { private ArrayList libraryCards; public LibraryCardManager() { new ArrayList(); }

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; } }

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; } }

LibraryMaterialCopy.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); } } }

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; } }

Catalog.java

import java.util.ArrayList; import java.util.Collection; import java.util.HashMap;

public class Catalog { HashMap> map = new HashMap>();

public void add(LibraryMaterial m,int n){ ArrayList copies = new ArrayList(); if(m instanceof Book){ for(int i=0;i

map.put(m,copies); } public Collection lookUpByMaterial(LibraryMaterial m){ if(map.containsKey(m)) return map.get(m); return null; } public Collection lookUpByTitle(String title){ for (LibraryMaterial m : map.keySet()) { if(m.getTitle().equals(title)){ return map.get(m); } } return null; } public Collection availableCopies(LibraryMaterial m){

ArrayList copies = map.get(m);

ArrayList availablecopies = new ArrayList() ; for(LibraryMaterialCopy c: copies){ if(c.dueDate == null) availablecopies.add(c);

} return availablecopies; } public Collection availableCopies(){ ArrayList materials = new ArrayList(map.keySet()); return materials; } }

main.java (what I have so far, no need to keep this, rewrite entirely if needed)

import java.util.ArrayList; import java.util.List;

public class main {

public static void main(String[] args) { ArrayList books = new ArrayList(); ArrayList bookCopies = new ArrayList(); ArrayList cards = new ArrayList<>(); Catalog catalog = new Catalog(); java.util.Scanner scannerObject = new java.util.Scanner(System.in); System.out.println ("How many books do you want to add to the library?"); int bkNum = scannerObject.nextInt(); Book[] book = new Book[bkNum]; java.util.Scanner scannerObject1 = new java.util.Scanner(System.in); for (int i = 0; i < book.length; i++){ //User enters values into set methods in Library class System.out.println("Enter ISBN"); String number = scannerObject1.nextLine(); System.out.println("Enter title"); String title = scannerObject1.nextLine(); System.out.println("Enter author"); String author = scannerObject1.nextLine(); books.add(new Book(number , title , author )); }

for (LibraryMaterial b : books) bookCopies.add(new BookCopy((Book) b)); java.util.Scanner scannerObject2 = new java.util.Scanner(System.in); System.out.println("How many Library cards do you want to add?"); int lcNum = scannerObject2.nextInt(); LibraryCard[] libraryCard = new LibraryCard[lcNum];

java.util.Scanner scannerObject3 = new java.util.Scanner(System.in); for (int i = 0; i < libraryCard.length; i++){ //User enters values into set methods in Library class System.out.println("Enter ID"); String id = scannerObject3.nextLine(); System.out.println("Enter Name"); String Name = scannerObject3.nextLine(); cards.add(new LibraryCard(id , Name)); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions

Question

What else could they be contributing to the organization?

Answered: 1 week ago

Question

How much time/resource do we think this piece of work will take?

Answered: 1 week ago

Question

What is our budget?

Answered: 1 week ago