Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the library program as follows: Modify the main program (mainProgram.java

ID: 3836617 • Letter: M

Question

Modify the library program as follows:

Modify the main program (mainProgram.java) to create a user menu for the library. The user menu must include ouput and input for the following:

a) add new books and DVDs to the library
b) add new library cards
c) see all library materials in the catalog
d) Check out by entering the library card ID AND the title of the library material to check out. If no copies of the material are available, a message should be displayed to the user indicating that. If more than one copy of the material is available, check out any available copy.
After the book is checked out, the due date should be displayed.
e) See all library material copies checked out to the card, given library card ID number
f) Return ("check in") a single library material copy, given title and library card ID
g) Return ("check in") all library material copies checked out to library card ID
h) Renew a book by specifying the title and library card ID. If the user enters the title of a DVD instead, a message should be displayed saying that DVDs may not be renewed.
After the book is renewed, the new due date should be displayed.

The Code is complete, only main needs to be modify to make use of all of its methods in a usable user interface

Book class has a title, author, and ISBN number.

A BookCopy refers to the actual copy of the book that the library has. A library can have multiple BookCopies of the same Book. BookCopy contains a Book. It also has a LibraryCard that is currently borrowing the book (or null if it is not checked out). Also has Also has an associated DueDate (which is null if the book is not checked out)

LibraryCard class has a method that performs a lookup by title: given a title, the library material copy checked out to the card that match that title should be returned, or null if none exist.

LibraryCard has an ID number, the name of the cardholder, and a collection of BookCopies that are checked out to that account. LibraryCard has methods for checking books in and out. It has a non-negative balance, which is incurred if books are checked in past their due dates. The library charges $.10 per day per book.

For each type of  LibraryMaterial, there is also a copy class -- Book and BookCopy, DVD and DVDCopy. LibraryMaterials (and all superclasses) prints all of the basic information: ISBN, title and (if any): author, main actor.

LibraryMaterial class has a method that accepts a string s as a parameter and returns true if the title of the string is equal to s.

The same method is in library material copies. It is abstract in the LibraryMaterialCopy class.

Each LibraryMaterial consists of a title and an ISBN.* LibraryMaterialCopy has a library card and due date, but they DO NOT contain a LibraryMaterial. Instead, LibraryMaterialCopy is an abstract class. The reason behind this is because there's no such thing as a LibraryMaterialCopy; it must be either a Book or a DVD. So LibraryMaterialCopy is in charge of all basic functionality for checking in and out, but is not associated with a specific  LibraryMaterial at the superclass level. LibraryMaterialCopy also prints LibraryCard checked out to and dueDate.

LibraryMaterialCopy should print LibraryCard checked out to and dueDate.  
BookCopy and DVDCopy should print all of the details of the Book/DVD plus the LibraryCard and dueDate.

The abstract LibraryMaterialCopy class have an abstract method, getLibraryMaterial() which will return either a Book or a DVD, depending on the subclass. In addition to all of the standard methods needed for accessing data and checking out / checking in / renewing / etc., each class have a print method.

Catalog class contains a mapping from LibraryMaterial to a collection of LibraryMaterialCopy. It keeps track of all the materials and copies of them in the library.

It has the following funtions

Adding new library materials: given a book or dvd and a number n, it should create n copies of the material and add them to the catalog.

Lookup copies by material: given a library material, it should return the collection of the copies of that material.

Lookup material by title: given a title, it should return the library material that matches that title, or null if none exist.

See all available copies: given a library material, it should return a collection of all copies of that material that are not checked out.

Return a collection of all library materials in the library

LibraryCardManager class contains a collection of LibraryCards and support the following operations:

Add new library cards

Lookup by ID: given an ID, return the library card associated with that ID, or null if none exist.

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

mainProgram.java

import java.util.ArrayList;
import java.util.List;
import java.time.LocalDate;

public class mainProgram {
public static void main(String[] args) {
ArrayList books = new ArrayList();
books.add(new Book("12345678910", "Harry Potter", "J. K. Rowling"));
books.add(new Book("98765432", "Berenstein Bears", "Stan and Jan"));
books.add(new Book("6547901", "Curious George", "No Clue"));
books.add(new Book("5678322222", "Samantha", "Me Myself"));

ArrayList bookCopies = new ArrayList();
for (LibraryMaterial b : books) {
bookCopies.add(new BookCopy((Book) b));
}

ArrayList cards = new ArrayList<>();
cards.add(new LibraryCard("123456", "Devorah Kletenik"));
cards.add(new LibraryCard("87654", "Me and Me"));
cards.add(new LibraryCard("8887654", "Sarah Kletenik"));

for (LibraryMaterialCopy bc : bookCopies) {
System.out.println(cards.get(0).checkOutLibraryMaterial(bc));
System.out.println(cards.get(1).checkOutLibraryMaterial(bc));
System.out.println(cards.get(2).checkOutLibraryMaterial(bc));
System.out.println("Is Berenstein Bears title of the book:"+bc.isTitle("Berenstein Bears"));
}

List bookCopies2 = cards.get(0).getLibraryMaterialsSorted();
for (LibraryMaterialCopy book : bookCopies2)
System.out.println(book.getTitle() + " " + book.getDueDate());

System.out.println("got here");
System.out.println(bookCopies.get(3).getTitle());
((BookCopy) bookCopies.get(3)).renew();

System.out.println("renewed");

bookCopies2 = cards.get(0).getLibraryMaterialsSorted();
System.out.println("and here again");
for (LibraryMaterialCopy book : bookCopies2)
System.out.println(book.getTitle() + " " + book.getDueDate());

System.out.println("here again");

for (LibraryMaterialCopy bc : bookCopies) {
System.out.println(cards.get(0).returnLibraryMaterial(bc));
System.out.println(cards.get(1).returnLibraryMaterial(bc));
System.out.println(cards.get(2).returnLibraryMaterial(bc));
}
}
}

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

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

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

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

LibraryMaterial.java

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

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

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 {
LibraryMaterialCopy c=new BookCopy((Book)m);
copies.add(c);
}
}
else   {
for(int i=0;i {
LibraryMaterialCopy c=new DVDCopy((DVD)m);
copies.add(c);
}
}

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

Current Output

true
false
false
true
false
false
true
false
false
true
false
false
true
false
false
true
false
false
true
false
false
true
false
false
Harry Potter 2017-05-26
Harry Potter 2017-05-26
Berenstein Bears 2017-05-26
Berenstein Bears 2017-05-26
Curious George 2017-05-26
Curious George 2017-05-26
Samantha 2017-05-26
Samantha 2017-05-26
got here
Berenstein Bears
renewed
and here again
Berenstein Bears 2017-05-19
Harry Potter 2017-05-26
Harry Potter 2017-05-26
Berenstein Bears 2017-05-26
Curious George 2017-05-26
Curious George 2017-05-26
Samantha 2017-05-26
Samantha 2017-05-26
here again
true
false
false
true
false
false
true
false
false
true
false
false
true
false
false
true
false
false
true
false
false
true
false
false

Explanation / Answer

DriverProgram.java

import java.util.*;
import java.time.*;

public class DriverProgram {
   public static void main (String[] args) {
      
       //assign values to the LibraryMaterial subclasses   -Randolph Cisneros
       Book catHat = new Book("001","Cat hat", "Seuss");
       Book lionWitch = new Book("002","Lion Witch", "Lewis");
       Book gatsby = new Book("003","Gatsby", "Fitzgerald");
       Book wonka = new Book("004","Wonka", "Dahl");

       DVD catInHat = new DVD("005","Cat hat", "Meyers");
       DVD batMan = new DVD("006","Batman", "Ledger");
       DVD rickMorty = new DVD("007","Rick and Morty", "Harmon");
       DVD imitationGame = new DVD("008","The Imitation Game", "Cumberbatch");

       //assign LibraryMaterial to the LibraryMaterialCopy subclasses   -Randolph Cisneros
       BookCopy lW= new BookCopy(lionWitch);
       BookCopy gat= new BookCopy(gatsby);
       BookCopy won = new BookCopy(wonka);
       BookCopy cH = new BookCopy(catHat);

       DVDCopy cIH = new DVDCopy(catInHat);
       DVDCopy bM = new DVDCopy(batMan);
       DVDCopy rM = new DVDCopy(rickMorty);
       DVDCopy iG = new DVDCopy(imitationGame);

       //assign cardholders. Really can just do with one.   -Randolph Cisneros
       LibraryCard bobSmith = new LibraryCard("01","Bob Smith");
       LibraryCard janeDoe = new LibraryCard("02","Jane Doe");
       LibraryCard johnDoe = new LibraryCard("03","John Doe");

       //testerDates   -Randolph Cisneros
       LocalDate today = LocalDate.now();
       LocalDate earlyDueDate = today.minusWeeks(5);

       //some material is given early checkout dates to test the late return methods   -Randolph Cisneros
       janeDoe.checkOutLibraryMaterial(gat, today);
       janeDoe.checkOutLibraryMaterial(lW, earlyDueDate);
       janeDoe.checkOutLibraryMaterial(won, earlyDueDate);
       janeDoe.checkOutLibraryMaterial(cH, earlyDueDate);
       janeDoe.checkOutLibraryMaterial(cIH, today);
       janeDoe.checkOutLibraryMaterial(iG, earlyDueDate);

       //this block tests a dvd for renewal; it should print a message   -Randolph Cisneros
       janeDoe.renewLibraryMaterial(gat);
       janeDoe.renewLibraryMaterial(iG);

       //checks these in to system   -Randolph Cisneros
       janeDoe.returnLibraryMaterial(gat);
       janeDoe.returnLibraryMaterial(lW);
      
       //tests list sort   -Randolph Cisneros
       ArrayList<LibraryMaterialCopy> mainSortedList = janeDoe.getLibraryMaterialSorted();
       traverseAndPrintDate(mainSortedList);

       //tests the method to get overdue material   -Randolph Cisneros
       ArrayList<LibraryMaterialCopy> lateLibraryMaterial = janeDoe.getLibraryMaterialOverdue();
       traverseAndPrintDate(lateLibraryMaterial);
      
   }

   //Simple traverse and print method. Makes use of print methods in the classes   -Randolph Cisneros
   public static void traverseAndPrintDate(ArrayList<LibraryMaterialCopy> libraryMaterialList){
       for (int i = 0; i < libraryMaterialList.size(); i++){
           LibraryMaterialCopy itLibraryMaterialCopy = libraryMaterialList.get(i);
           LibraryMaterial itLibraryMaterial = itLibraryMaterialCopy.getLibraryMaterial();
           itLibraryMaterial.print();
           itLibraryMaterialCopy.print();
       }
   }

}


Book.java

//Sample solution to HW2
class Book extends LibraryMaterial
{
   private String author;
  
   //constructor for Book calls constructor for superclass first   -Randolph Cisneros
   public Book (String i, String t, String a)
   {
       super(i,t);
       author = a;
   }
  
   //mutators actually need to call super to access elements of superclass   -Randolph Cisneros
   public String getIsbn () {return super.getIsbn();}
   public String getTitle() {return super.getTitle();}
   public String getAuthor() {return author;}
  
   //print method   -Randolph Cisneros
   public void print() {
      super.print();
       System.out.println("Author: " + getAuthor());
   }

}


LibraryMaterial.java

public class LibraryMaterial {
   private String title;
   private String ISBN;
  
   //constructor for LibraryMaterial class; must use super(i,t) in sublcasses   -Randolph Cisneros
   public LibraryMaterial(String i, String t){
       ISBN = i;
       title = t;
   }
  
   //accessors. This is the highest level and has direct access to title and ISBN   -Randolph Cisneros
   public String getTitle() {return title;}
   public String getIsbn() {return ISBN; }
  
   //print method   -Randolph Cisneros
   public void print(){
       System.out.println("Title: " + title + "ISBN: " + ISBN);
   }
}
  
DVD.java

class DVD extends LibraryMaterial {

   private String mainActor;

   public DVD (String i, String t, String mA) {
       super(i,t);
       mainActor = mA;
   }

   //accessors for subclass call accessors for superclass if necessary   -Randolph Cisneros
   public String getIsbn () {return super.getIsbn();}
   public String getTitle() {return super.getTitle();}
   public String getMainActor() {return mainActor;}
  
   //print method, calls super first.   -Randolph Cisneros
   public void print() {
       super.print();
       System.out.println("Main Actor: " + mainActor);
   }

}

BookCopy.java

// Sample solution for HW2
import java.time.LocalDate;

public class BookCopy extends LibraryMaterialCopy {
   public static final int BORROWING_WEEKS = 3;
   public static final int RENEWAL_WEEKS = 2;
   public static final double FINE_PER_DAY = .10;
   private Book book;
   private LibraryCard card;
   private LocalDate dueDate;
  
   public BookCopy(Book b)
   {
       book = b;
       card = null;
       dueDate = null;
   }
  
   //accessors. we also need access to static variables because they will be different
   //between subclasses   -Randolph Cisneros
   public LibraryMaterial getLibraryMaterial() {return book;}
   public String getTitle() {return book.getTitle();}
   public String getIsbn() {return book.getIsbn();}
   public LibraryCard getCard() {return card;}
   public LocalDate getDueDate() {return dueDate;}
   public double getFinePerDay(){ return FINE_PER_DAY; }
   public int getBorrowingPeriod() { return BORROWING_WEEKS; }
  
   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(BORROWING_WEEKS);
       return true;
   }
  
   public boolean checkOut (LibraryCard borrower)
   //default check out method that uses todays' date  
   {
       return checkOut(borrower, LocalDate.now());
   }
  
   public boolean returnLibraryMaterial()
           //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 boolean renew (LocalDate renewalDate)
   //renews book using RENEWAL_WEEKS as interval
   //returns false if books is not checked out  
   {
       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());
   }
  
   //users other class's method, which calls super class's print method  
   public void print()
   {
       book.print();
       LibraryCard c = getCard();
       LocalDate dD = getDueDate();
       System.out.println("Card: " + c.getCardholderName() + "Due Date: " + dD.toString());
   }

}

DVDCopy.java

// Sample solution for HW2
import java.time.LocalDate;

public class DVDCopy extends LibraryMaterialCopy {
   public static final int BORROWING_WEEKS = 2;
   public static final double FINE_PER_DAY = 1.00;
   private DVD dvd;
   private LibraryCard card;
   private LocalDate dueDate;
  
   public DVDCopy(DVD d)
   {
       dvd = d;
       card = null;
       dueDate = null;
   }
  
   //Accessors. Note that they call accessors to the other class as well  
   public LibraryMaterial getLibraryMaterial() {return dvd;}
   public String getTitle() {return dvd.getTitle();}
   public String getIsbn() { return dvd.getIsbn();}
   public LibraryCard getCard() {return card;}
   public LocalDate getDueDate() {return dueDate;}
   //Here again, we have accessors for the public static variables. It
   //seems like this would be a smart standard practice, especially for
   //any subclass.   -Randolph Cisneros
   public double getFinePerDay(){ return FINE_PER_DAY; }
   public int getBorrowingPeriod() { return BORROWING_WEEKS; }
  
   public boolean checkOut(LibraryCard borrower, LocalDate dateOfBorrowing)
  
   /*checks dvd out by setting card reference to borrower.
   returns false if dvd is already checked out
   sets due date to BORROWING_WEEKS after current date passed   */
  
   {
       if (card != null)
           return false;
       card = borrower;
       dueDate = dateOfBorrowing.plusWeeks(BORROWING_WEEKS);
       return true;
   }
  
   public boolean checkOut (LibraryCard borrower)
   //default check out method that uses todays' date  
   {
       return checkOut(borrower, LocalDate.now());
   }
  
   public boolean returnLibraryMaterial()
           //returns dvd by removing card reference
           //returns false if there is no reference to a card
   {
       if (card == null)
           return false;
       card = null;
       return true;
   }
  
   //a simple class that returns false. I decided to override this method rather than omit,
   //since it felt like a safer bet  
   public boolean renew (LocalDate renewDate){
       System.out.println("Cannot renew DVDs");
       return false;
   }
  
   public boolean renew ()
   //same here   -Randolph Cisneros
   {
       System.out.println("Cannot renew DVDs");
       return false;
   }
  
   //print method for this class. uses dvd print method, then has to create local librarycard and duedate variables
   //to use their accessors  
   public void print() {
       dvd.print();
       LibraryCard c = getCard();
       LocalDate dD = getDueDate();
       System.out.println("Card: " + c.getCardholderName() + "Due Date" + dD.toString());
   }
  
}

LibraryMaterialCopy.java

import java.time.LocalDate;

public abstract class LibraryMaterialCopy {
   private LibraryCard card;
   private LocalDate dueDate;
  
   public LibraryMaterialCopy() {
       card = null;
       dueDate = null;
   }
  
   //Many abstract classes had to be added in order to replace BookCopy with LibraryMaterialCopy in the
   //LibraryCard class. This is something to note when replacing a class with an abstract class.  
   abstract LibraryMaterial getLibraryMaterial();
   abstract String getTitle();
   abstract String getIsbn();
   abstract LocalDate getDueDate();
   abstract boolean returnLibraryMaterial();
   abstract boolean renew();
   abstract boolean renew(LocalDate date);
   abstract boolean checkOut(LibraryCard borrower, LocalDate dateOfBorrowing);
   abstract boolean checkOut (LibraryCard borrower);

   //accessors for 2 different static final values of the subclasses; allows for polymorphism  
   abstract double getFinePerDay();
   abstract int getBorrowingPeriod();
  
   abstract void print();
}

LibraryCard.java

import java.util.ArrayList;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;


public class LibraryCard {
  
   private String id;
   private String cardholderName;
   private ArrayList<LibraryMaterialCopy> libraryMaterialCheckedOut;
   private double balance;
  
   public LibraryCard(String i, String name)
   {
       id = i;
       cardholderName = name;
       libraryMaterialCheckedOut = new ArrayList<LibraryMaterialCopy>();
       balance = 0;
   }
  
   //accessors  
   public String getID() {return id;}
   public String getCardholderName() {return cardholderName;}
   public ArrayList<LibraryMaterialCopy> getlibraryMaterialCheckedOut() {return libraryMaterialCheckedOut;}
   //mutator  
   public void setCardholderName (String name) {cardholderName = name;}
  
   public boolean checkOutLibraryMaterial (LibraryMaterialCopy lM, LocalDate todaysDate)
   //checks out LibraryMaterial and sends message to LibraryCopy to check itself out too
   //returns false if lM is already checked out
   //takes parameter that reflects the date that the checkout is happening  
   {
       if (!lM.checkOut(this,todaysDate))
           return false;
       libraryMaterialCheckedOut.add(lM);
       return true;
   }
  
   public boolean checkOutLibraryMaterial(LibraryMaterialCopy lM)
   //default check out, uses today's date  
   {
       return checkOutLibraryMaterial(lM, LocalDate.now());
   }
  
   public boolean returnLibraryMaterial (LibraryMaterialCopy lM, LocalDate returnDate)
   //returns lM and sends message to LibraryMaterialCopy to do the same
   //returns false if lM is not checked out
   //takes parameter that expresses the date of return  
   {
       LocalDate dueDate = lM.getDueDate();
       if (!lM.returnLibraryMaterial())
           return false;
       if (!libraryMaterialCheckedOut.remove(lM))
           return false;
       long daysBetween = ChronoUnit.DAYS.between(dueDate, returnDate);
       if (daysBetween > 0) //book is returned late
       {
           balance += lM.getFinePerDay() * daysBetween;
       }

       return true;
   }
  
   public boolean returnLibraryMaterial (LibraryMaterialCopy lM)
   //default method, uses today's date as returns date  
   {
       return returnLibraryMaterial(lM, LocalDate.now());
   }
  
   public boolean renewLibraryMaterial(LibraryMaterialCopy lM, LocalDate renewalDate)
   //renews lM. Returns false if lM is not checked out already
   //takes parameter that expresses date of renewal  
   {
       if (!libraryMaterialCheckedOut.contains(lM))
           return false;
       if (!lM.renew(renewalDate))
           return false;
       return true;
   }
  
   public boolean renewLibraryMaterial (LibraryMaterialCopy lM)
   //default renewal method uses today's date as renewal date.  
   {
       return renewLibraryMaterial(lM, LocalDate.now());
   }
  
   public ArrayList<LibraryMaterialCopy> getLibraryMaterialDueBy(LocalDate date)
   //returns an ArrayList of LibraryMaterial due on or before date  
   {
       ArrayList<LibraryMaterialCopy> LibraryMaterialDue = new ArrayList();
       for (LibraryMaterialCopy lM: libraryMaterialCheckedOut)
       {
           if (lM.getDueDate().isBefore(date) || lM.getDueDate().equals(date))
           {
               LibraryMaterialDue.add(lM);
           }
       }
      
       return LibraryMaterialDue;
   }
  
   public ArrayList<LibraryMaterialCopy> getLibraryMaterialOverdue (LocalDate todaysDate)
   //returns LibraryMaterial overdue as of todaysDate
   //which means that they were actually due by yesterday  
   {
       return getLibraryMaterialDueBy(todaysDate.minusDays(1));
   }
  
   public ArrayList getLibraryMaterialOverdue()
   //default method, returns LibraryMaterial overdue as of today, which means that they
   //were due by yesterday  
   {
       return getLibraryMaterialOverdue(LocalDate.now());
   }

   public ArrayList<LibraryMaterialCopy> getLibraryMaterialSorted()
   //returns ArrayList of LibraryMaterial, sorted by due date (earliest due date first)
   //uses insertion sort   
   {
       for (int i = 1; i < libraryMaterialCheckedOut.size(); i++)
       {
           int j = i;
           while (j > 0 && libraryMaterialCheckedOut.get(j-1).getDueDate().isAfter(libraryMaterialCheckedOut.get(j).getDueDate()))
           {
               //swap elements in positions j and j-1
               LibraryMaterialCopy temp = libraryMaterialCheckedOut.get(j);
               libraryMaterialCheckedOut.set(j, libraryMaterialCheckedOut.get(j-1));
               libraryMaterialCheckedOut.set(j-1, temp);
              
               j = j-1;
           }
       }

       return libraryMaterialCheckedOut;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote