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

Modify the ibrary program as follows: 1. Give your LibraryCard the functionality

ID: 3834420 • Letter: M

Question

Modify the ibrary program as follows:

1. Give your LibraryCard the functionality to be deep copied. Note that that means that you will need to be able to deep copy other objects in your code base as well. Use clone() for all of them, not copy constructors.

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

Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class

3. Create a Catalog class that should contain a mapping from LibraryMaterial to a collection of LibraryMaterialCopy. It should keep track of all the materials and copies of them in the library.

It should support the following operations:

1) 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.

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

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

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

5) Return a collection of all library materials in the library


4. Add a method to the LibraryCard class 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.


5. Create a LibraryCardManager class. It should contain a collection of LibraryCards and support the following operations:

1) add new library cards

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


6. Create a user menu (this can be in main) that supports the following operations:

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 a) the library card ID and b) 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.

5) see all library material copies checked out to the card, given library card 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 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.


7. Test all of this out. Include a copy of your output.

NOTE: In all of this, you may assume that the title of every library material in the library is unique, i.e., there do not exist 2+ books that have the same title, or 2+ DVDs that have the same title, or a book and a DVD that have the same title. This is a simplifying assumption that is obviously not true in the real world.

Modify your previous Library program as follows:


1. Give your LibraryCard the functionality to be deep copied. Note that that means that you will need to be able to deep copy other objects in your code base as well. Use clone() for all of them, not copy constructors.


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

Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class (do you see why?).


3. Create a Catalog class that should contain a mapping from LibraryMaterial to a collection of LibraryMaterialCopy. It should keep track of all the materials and copies of them in the library.

It should support the following operations:

1) 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.

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

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

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

5) Return a collection of all library materials in the library


4. Add a method to the LibraryCard class 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.


5. Create a LibraryCardManager class. It should contain a collection of LibraryCards and support the following operations:

1) add new library cards

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


6. Create a user menu (this can be in main) that supports the following operations:

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 a) the library card ID and b) 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.

5) see all library material copies checked out to the card, given library card 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 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.


7. Test all of this out. Include a copy of your output.

NOTE: In all of this, you may assume that the title of every library material in the library is unique, i.e., there do not exist 2+ books that have the same title, or 2+ DVDs that have the same title, or a book and a DVD that have the same title. This is a simplifying assumption that is obviously not true in the real world.

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)
   //checks out libraryMaterial and sends message to libraryMaterialCopy to check itself out too
   //returns false if libraryMaterial is already checked out
   //takes parameter that reflects the date that the checkout is happening
   {
       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)
   //returns libraryMaterials overdue as of todaysDate
   //which means that they were actually due by yesterday
   {
       return getlibraryMaterialsDueBy(todaysDate.minusDays(1));
   }
  
   public List<LibraryMaterialCopy> getLibraryMaterialsOverdue()
   //default method, returns libraryMaterials overdue as of today, which means that they
   //were due by yesterday
   {
       return getLibraryMaterialsOverdue(LocalDate.now());
   }

   public List<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 libraryMaterials;
   }
}

//Sample solution to HW1
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 + " ");
   }

}

// Sample solution for HW1
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 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);
       }
   }
  

}

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

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)
   //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());
   }
  
   public void print()
   {
       book.print();
       super.print();
   }
  


}


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

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

}

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

public class mainProgram {

   public static void main(String[] args) {
       ArrayList<LibraryMaterial> books = new ArrayList<LibraryMaterial>();
       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<LibraryMaterialCopy> bookCopies = new ArrayList<LibraryMaterialCopy>();
  
       for (LibraryMaterial b: books)
       {
           bookCopies.add(new BookCopy ((Book)b));
           bookCopies.add(new BookCopy ((Book)b));

       }
      
       ArrayList<LibraryCard> 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));
          
       }
      
       List<LibraryMaterialCopy> 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));
       }
              

   }

}

Explanation / Answer

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 List<LibraryMaterialCopy> getLibraryMaterialsOverdue()
   //default method, returns libraryMaterials overdue as of today, which means that they
   //were due by yesterday
   {
       return getLibraryMaterialsOverdue(LocalDate.now());
   }
   public List<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 libraryMaterials;
   }
}

//Sample solution to HW1
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 + " ");
   }
}

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


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

}

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


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

import java.util.ArrayList;
import java.util.List;
public class mainProgram {
   public static void main(String[] args) {
       ArrayList<LibraryMaterial> books = new ArrayList<LibraryMaterial>();
       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<LibraryMaterialCopy> bookCopies = new ArrayList<LibraryMaterialCopy>();
  
       for (LibraryMaterial b: books)
       {
           bookCopies.add(new BookCopy ((Book)b));
           bookCopies.add(new BookCopy ((Book)b));
       }
      
       ArrayList<LibraryCard> 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));
          
       }
      
       List<LibraryMaterialCopy> 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));
       }
              
   }
}

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