I\'m creating a Library simulation in Java. My current code has a Book class tha
ID: 3811498 • Letter: I
Question
I'm creating a Library simulation in Java. My current code has a Book class that stores the book information, BookCopy class that handles copies of the book, LibraryCard class that handles the checkout, list of books checked out, due dates, etc. I need to make the following changes to the program.
Create a LibraryMaterial class. Each LibraryMaterial consists of a title and an ISBN.* LibraryMaterialCopy has a library card and due date, they DO NOT contain a LibraryMaterial. Instead, declare LibraryMaterialCopy an abstract class.
What sort of LibraryMaterials does the library have?
* Books. These have the same basics as a LibraryMaterial, plus an author.
* DVDs are NOT Books (for starters, they don't have authors). They areLibraryMaterials, and they have a "main actor" .
For each type of LibraryMaterial, there is also a copy class -- Book and BookCopy, DVD and DVDCopy.
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 LibraryMaterial is in charge of all basic functionality for checking in and out, but is not associated with a specific LibraryMaterial at the superclass level.
The abstract LibraryMaterialCopy class should have an abstract method, getLibraryMaterial() which will return either a Book or a DVD, depending on the subclass. You may also have abstract getTitle() and getISBN().
In addition to all of the standard methods needed for accessing data and checking out / checking in / renewing / etc., each class should have a print method.
LibraryMaterials (and all superclasses) should print all of the basic information: ISBN, title and (if any): author, main actor.
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.
Print to standard output. The only time that you should do that in a class is within a print() method.
To make matters more complicated, the library has set the following rules for checking out materials:
1. Overdue fines for books are $.10 per day, $1.00 per day for DVDs. An abstract getFinePerDay() method.
2. Books are checked out for three weeks. DVDs are checked out for only two weeks. An abstract getBorrowingPeriod() method.
3. Books are renewable for two weeks. DVDs may not be renewed.
The LibraryCard class should not have any additional functionalities, but will need to be changed to accommodate the changes to the library materials. Please think carefully about the inheritance hierarchy and polymorphism .
My Code
****************************************************
//Book class stores the title, author and ISBN of the book
public class Book {
private String Title;
private String Author;
//Using String for ISBN instead of a int because it is 13 digits long
private String Isbn;
//Constructor
public Book(String title, String author, String isbn)
{
Title = title;
Author = author;
Isbn = isbn;
}
//No need for setters because a book's title, author and ISBN will never change
//Getters
public String getTitle()
{
return Title;
}
public String getAuthor()
{
return Author;
}
public String getIsbn()
{
return Isbn;
}
}
*********************************************************
import java.time.LocalDate;
//BookCopy class stores the current copy of the book that is being checked in or out and the current library card that is being used.
public class BookCopy {
private Book Book;
private LibraryCard LibraryCard;
private LocalDate dueDate;
//Constructor
//If book is not checked out, pass null for library card
public BookCopy(Book book, LibraryCard libraryCard)
{
Book = book;
LibraryCard = libraryCard;
dueDate = null;
}
//Getters and setters
public Book getBook()
{
return Book;
}
public LibraryCard getLibraryCard()
{
return LibraryCard;
}
public LocalDate getDueDate()
{
return dueDate;
}
public void setDueDate(LocalDate dueDate)
{
this.dueDate = dueDate;
}
//We don't need setter for Book as a book's copy won't change
//But we need setter for LibraryCard, as multiple people can borrow
//a single book copy
public void setLibraryCard(LibraryCard libraryCard)
{
LibraryCard = libraryCard;
}
}
**********************************************************************
//LibraryCard class stores the library card holder's ID, name and a list of the books that are currently checked out on the card
//Prints the user and checkout information onto the console
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
public class LibraryCard {
private int ID;
private String CardHolderName;
private double charges;
//List that stores the books that are checked out on the card
private ArrayList<BookCopy> BookCopyList = new ArrayList<>();
//Constructor
public LibraryCard(int id, String cardHolderName, BookCopy bookCopy, double charges)
{
ID = id;
CardHolderName = cardHolderName;
BookCopyList.add(bookCopy);
this.charges = charges;
}
//Getters and Setters
//We don't need setter for id as it wont change
public int getId()
{
return ID;
}
public String getCardHolderName()
{
return CardHolderName;
}
public void setCardHolderName(String cardHolderName)
{
CardHolderName = cardHolderName;
}
public double getCharges()
{
return charges;
}
public void addBookCopy(BookCopy bookCopy)
{
addBookCopy(bookCopy, LocalDate.now());
}
//Methods to Add and remove bookcopies to LibraryCard
public void addBookCopy(BookCopy bookCopy, LocalDate checkoutDate)
{
BookCopyList.add(bookCopy);
//Adding libraryCard to bookcopy
bookCopy.setLibraryCard(this);
//Set due date for 3 weeks later
bookCopy.setDueDate(checkoutDate.plusWeeks(3));
//Printing on screen that the book has been checked out
System.out.println("CheckOut Information: ");
System.out.println("Borrower Name: "+this.CardHolderName);
System.out.println("Book Title: "+bookCopy.getBook().getTitle());
System.out.println("Author Name: "+bookCopy.getBook().getAuthor());
System.out.println("ISBN No.: "+bookCopy.getBook().getIsbn());
System.out.println("Due date: " + bookCopy.getDueDate());
System.out.println("");
}
public boolean removeBookCopy(BookCopy bookCopy)
{
return removeBookCopy(bookCopy, LocalDate.now());
}
//Method returns true if bookCopy was successfully removed from LibraryCard
public boolean removeBookCopy(BookCopy bookCopy, LocalDate returnDate)
{
if(BookCopyList.contains(bookCopy))
{
BookCopyList.remove(bookCopy);
//Checking if due date is of later time, and we need to subtract
//charges
if (bookCopy.getDueDate().isBefore(returnDate))
{
long noOfDays = ChronoUnit.DAYS.between(bookCopy.getDueDate(), returnDate);
System.out.println("Deduting balance for " + noOfDays + " Days.");
charges -= noOfDays * .10; //Have not figured out the right formatting for the output when displaying charges.
}
//Resetting librarycard to null as its checked in now
bookCopy.setLibraryCard(null);
//Resetting the due date
bookCopy.setDueDate(null);
System.out.println("Book Checkd In Successfully:");
System.out.println("Book Title: "+bookCopy.getBook().getTitle());
System.out.println("Charges: " + "$" + charges);
System.out.println("");
return true;
}
System.out.println("This book is not borrowed by "+this.CardHolderName);
System.out.println("");
return false;
}
//Renews books due date for 2 weeks more.
public void renewal(BookCopy bookCopy)
{
bookCopy.setDueDate(bookCopy.getDueDate().plusDays(14));
System.out.println("Renewal successful.");
// Printing on screen that the book has been checked out
System.out.println("Borrower Name: " + this.CardHolderName);
System.out.println("Book Title: " + bookCopy.getBook().getTitle());
System.out.println("Due date: " + bookCopy.getDueDate());
System.out.println("");
}
//Get the books which are overdue on a particular date
public void getListOfBooksDue(LocalDate date)
{
ArrayList<BookCopy> dueBooks = new ArrayList<>();
for (BookCopy b : BookCopyList) {
if (!b.getDueDate().isAfter(date)) {
dueBooks.add(b);
System.out.println("Book Title: " + b.getBook().getTitle());
}
}
}
//Return the books overdue on the calling date
public void getOverdueBooks()
{
getListOfBooksDue(LocalDate.now());
}
//For testing the above method
public void getOverdueBooks(LocalDate d) {
getListOfBooksDue(d);
}
//For testing the above method
public void getCheckedOutBooks()
{
ArrayList<BookCopy> CheckedOutList = new ArrayList<>(BookCopyList);
Collections.sort(CheckedOutList, new Comparator<BookCopy>() {
public int compare(BookCopy b1, BookCopy b2) {
return b1.getDueDate().compareTo(b2.getDueDate());
}
});
for (BookCopy b : CheckedOutList) {
System.out.println("Book Title: " + b.getBook().getTitle());
}
//Return CheckedOutList;
}
}
***********************************************************
import java.time.LocalDate;
//Test program for the Book class, Library class and BookCopy class
public class Main {
public static void main (String[] args) throws java.lang.Exception
{
//List of Books and their bookCopies
Book Book1 = new Book("Diary of a Wimpy Kid # 11: Double Down", "Jeff Kinney", "9781419723445");
BookCopy BookCopy1a = new BookCopy(Book1, null);
BookCopy BookCopy1b = new BookCopy(Book1, null);
Book Book2 = new Book("A Man Called Ove: A Novel", "Fredrik Backman", "9781476738024");
BookCopy BookCopy2a = new BookCopy(Book2, null);
BookCopy BookCopy2b = new BookCopy(Book2, null);
Book Book3 = new Book("To Kill a Mockingbird", "Harper Lee", "9780446310789");
BookCopy BookCopy3a = new BookCopy(Book3, null);
BookCopy BookCopy3b = new BookCopy(Book3, null);
Book Book4 = new Book("A Dance with Dragons", "George R. R. Martin", "9780553801477");
BookCopy BookCopy4a = new BookCopy(Book4, null);
BookCopy BookCopy4b = new BookCopy(Book4, null);
Book Book5 = new Book("One Flew Over the Cuckoo's Nest", "Ken Kesey", "9780451163967");
BookCopy BookCopy5a = new BookCopy(Book5, null);
BookCopy BookCopy5b = new BookCopy(Book5, null);
Book Book6 = new Book("The Amber Spyglass: His Dark Materials", "Philip Pullman", "9780440418566");
BookCopy BookCopy6a = new BookCopy(Book6, null);
BookCopy BookCopy6b = new BookCopy(Book6, null);
Book Book7 = new Book("The Help", "Kathryn Stockett", "9780399155345");
BookCopy BookCopy7a = new BookCopy(Book7, null);
BookCopy BookCopy7b = new BookCopy(Book7, null);
Book Book8 = new Book("Of Mice and Men", "John Steinbeck", "9780140177398");
BookCopy BookCopy8a = new BookCopy(Book8, null);
BookCopy BookCopy8b = new BookCopy(Book8, null);
Book Book9 = new Book("Harry Potter and the Deathly Hallows", "J. K. Rowling", "9780545010221");
BookCopy BookCopy9a = new BookCopy(Book9, null);
BookCopy BookCopy9b = new BookCopy(Book9, null);
Book Book10 = new Book("A Thousand Splendid Suns", "Khaled Hosseini", "9781594489501");
BookCopy BookCopy10a = new BookCopy(Book10, null);
BookCopy BookCopy10b = new BookCopy(Book10, null);
//Library Cards
LibraryCard libraryCard1 = new LibraryCard(1, "Tony Stark", null, 0);
LibraryCard libraryCard2 = new LibraryCard(2, "Bruce Wayne", null, 0);
LibraryCard libraryCard3 = new LibraryCard(3, "Jon Snow", null, 0);
libraryCard1.addBookCopy(BookCopy10a, LocalDate.of(2017, 01, 15));
libraryCard1.addBookCopy(BookCopy6b, LocalDate.of(2017,01, 15));
libraryCard1.addBookCopy(BookCopy5a);
// Renew due date of book 6b
libraryCard1.renewal(BookCopy6b);
//This book has been borrowed by libraryCard1.
//It is returned after the due date, so the charges is added
libraryCard1.removeBookCopy(BookCopy10a);
//This book is returned before the due date, so the charges stay the same
libraryCard1.removeBookCopy(BookCopy5a);
libraryCard2.addBookCopy(BookCopy3b, LocalDate.of(2017, 05, 18));
libraryCard2.addBookCopy(BookCopy4a, LocalDate.of(2017, 05, 18));
//This book hasn't been borrowed by libraryCard3Holder
libraryCard3.removeBookCopy(BookCopy9b);
}
}
*****************************************************************************
Output:
CheckOut Information:
Borrower Name: Tony Stark
Book Title: A Thousand Splendid Suns
Author Name: Khaled Hosseini
ISBN No.: 9781594489501
Due date: 2017-02-05
CheckOut Information:
Borrower Name: Tony Stark
Book Title: The Amber Spyglass: His Dark Materials
Author Name: Philip Pullman
ISBN No.: 9780440418566
Due date: 2017-02-05
CheckOut Information:
Borrower Name: Tony Stark
Book Title: One Flew Over the Cuckoo's Nest
Author Name: Ken Kesey
ISBN No.: 9780451163967
Due date: 2017-03-22
Renewal successful.
Borrower Name: Tony Stark
Book Title: The Amber Spyglass: His Dark Materials
Due date: 2017-02-19
Deduting balance for 24 Days.
Book Checkd In Successfully:
Book Title: A Thousand Splendid Suns
Charges: $-2.4000000000000004
Book Checkd In Successfully:
Book Title: One Flew Over the Cuckoo's Nest
Charges: $-2.4000000000000004
CheckOut Information:
Borrower Name: Bruce Wayne
Book Title: To Kill a Mockingbird
Author Name: Harper Lee
ISBN No.: 9780446310789
Due date: 2017-06-08
CheckOut Information:
Borrower Name: Bruce Wayne
Book Title: A Dance with Dragons
Author Name: George R. R. Martin
ISBN No.: 9780553801477
Due date: 2017-06-08
This book is not borrowed by Jon Snow
Explanation / Answer
import java.time.LocalDate;
//Test program for the Book class, Library class and BookCopy class
public class Main {
public static void main (String[] args) throws java.lang.Exception
{
//List of Books and their bookCopies
Book Book1 = new Book("Diary of a Wimpy Kid # 11: Double Down", "Jeff Kinney", "9781419723445");
BookCopy BookCopy1a = new BookCopy(Book1, null);
BookCopy BookCopy1b = new BookCopy(Book1, null);
Book Book2 = new Book("A Man Called Ove: A Novel", "Fredrik Backman", "9781476738024");
BookCopy BookCopy2a = new BookCopy(Book2, null);
BookCopy BookCopy2b = new BookCopy(Book2, null);
Book Book3 = new Book("To Kill a Mockingbird", "Harper Lee", "9780446310789");
BookCopy BookCopy3a = new BookCopy(Book3, null);
BookCopy BookCopy3b = new BookCopy(Book3, null);
Book Book4 = new Book("A Dance with Dragons", "George R. R. Martin", "9780553801477");
BookCopy BookCopy4a = new BookCopy(Book4, null);
BookCopy BookCopy4b = new BookCopy(Book4, null);
Book Book5 = new Book("One Flew Over the Cuckoo's Nest", "Ken Kesey", "9780451163967");
BookCopy BookCopy5a = new BookCopy(Book5, null);
BookCopy BookCopy5b = new BookCopy(Book5, null);
Book Book6 = new Book("The Amber Spyglass: His Dark Materials", "Philip Pullman", "9780440418566");
BookCopy BookCopy6a = new BookCopy(Book6, null);
BookCopy BookCopy6b = new BookCopy(Book6, null);
Book Book7 = new Book("The Help", "Kathryn Stockett", "9780399155345");
BookCopy BookCopy7a = new BookCopy(Book7, null);
BookCopy BookCopy7b = new BookCopy(Book7, null);
Book Book8 = new Book("Of Mice and Men", "John Steinbeck", "9780140177398");
BookCopy BookCopy8a = new BookCopy(Book8, null);
BookCopy BookCopy8b = new BookCopy(Book8, null);
Book Book9 = new Book("Harry Potter and the Deathly Hallows", "J. K. Rowling", "9780545010221");
BookCopy BookCopy9a = new BookCopy(Book9, null);
BookCopy BookCopy9b = new BookCopy(Book9, null);
Book Book10 = new Book("A Thousand Splendid Suns", "Khaled Hosseini", "9781594489501");
BookCopy BookCopy10a = new BookCopy(Book10, null);
BookCopy BookCopy10b = new BookCopy(Book10, null);
//Library Cards
LibraryCard libraryCard1 = new LibraryCard(1, "Tony Stark", null, 0);
LibraryCard libraryCard2 = new LibraryCard(2, "Bruce Wayne", null, 0);
LibraryCard libraryCard3 = new LibraryCard(3, "Jon Snow", null, 0);
libraryCard1.addBookCopy(BookCopy10a, LocalDate.of(2017, 01, 15));
libraryCard1.addBookCopy(BookCopy6b, LocalDate.of(2017,01, 15));
libraryCard1.addBookCopy(BookCopy5a);
// Renew due date of book 6b
libraryCard1.renewal(BookCopy6b);
//This book has been borrowed by libraryCard1.
//It is returned after the due date, so the charges is added
libraryCard1.removeBookCopy(BookCopy10a);
//This book is returned before the due date, so the charges stay the same
libraryCard1.removeBookCopy(BookCopy5a);
libraryCard2.addBookCopy(BookCopy3b, LocalDate.of(2017, 05, 18));
libraryCard2.addBookCopy(BookCopy4a, LocalDate.of(2017, 05, 18));
//This book hasn't been borrowed by libraryCard3Holder
libraryCard3.removeBookCopy(BookCopy9b);
}
}
Book.java
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);
}
}
BookCopy.java
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 -Randolph Cisneros */
{
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 -Randolph Cisneros
{
return checkOut(borrower, LocalDate.now());
}
public boolean returnLibraryMaterial()
//returns book by removing card reference
//returns false if there is no reference to a card -Randolph Cisneros
{
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 -Randolph Cisneros
{
if (card == null)
return false;
dueDate = renewalDate.plusWeeks(RENEWAL_WEEKS);
return true;
}
public boolean renew ()
//default method uses todays date as renewal date -Randolph Cisneros
{
return renew(LocalDate.now());
}
//users other class's method, which calls super class's print method -Randolph Cisneros
public void print()
{
book.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. -Randolph Cisneros
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 -Randolph Cisneros
abstract double getFinePerDay();
abstract int getBorrowingPeriod();
abstract void print();
}
LibraryCard.java
import java.util.ArrayList;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
//A lot of changes were pretty much just swapping Book with LibraryMaterial, though there were some methods
//pertaining to the LibraryMaterialCopy class that I forgot to implement and didn't find out until I tried
//to compile this class. -Randolph Cisneros
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 -Randolph Cisneros
public String getID() {return id;}
public String getCardholderName() {return cardholderName;}
public ArrayList<LibraryMaterialCopy> getlibraryMaterialCheckedOut() {return libraryMaterialCheckedOut;}
//mutator -Randolph Cisneros
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 -Randolph Cisneros
{
if (!lM.checkOut(this,todaysDate))
return false;
libraryMaterialCheckedOut.add(lM);
return true;
}
public boolean checkOutLibraryMaterial(LibraryMaterialCopy lM)
//default check out, uses today's date -Randolph Cisneros
{
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 -Randolph Cisneros
{
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 -Randolph Cisneros
{
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 -Randolph Cisneros
{
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. -Randolph Cisneros
{
return renewLibraryMaterial(lM, LocalDate.now());
}
public ArrayList<LibraryMaterialCopy> getLibraryMaterialDueBy(LocalDate date)
//returns an ArrayList of LibraryMaterial due on or before date -Randolph Cisneros
{
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 -Randolph Cisneros
{
return getLibraryMaterialDueBy(todaysDate.minusDays(1));
}
public ArrayList getLibraryMaterialOverdue()
//default method, returns LibraryMaterial overdue as of today, which means that they
//were due by yesterday -Randolph Cisneros
{
return getLibraryMaterialOverdue(LocalDate.now());
}
public ArrayList<LibraryMaterialCopy> getLibraryMaterialSorted()
//returns ArrayList of LibraryMaterial, sorted by due date (earliest due date first)
//uses insertion sort -Randolph Cisneros
{
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;
}
}
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);
}
}
DVDCopy.java
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 -Randolph Cisneros
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 -Randolph Cisneros */
{
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 -Randolph Cisneros
{
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 -Randolph Cisneros
public boolean renew (LocalDate renewDate){
System.out.println("Cannot renew DVDs");
return false;
}
public boolean renew ()
{
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 -Randolph Cisneros
public void print() {
dvd.print();
LibraryCard c = getCard();
LocalDate dD = getDueDate();
System.out.println("Card: " + c.getCardholderName() + "Due Date" + dD.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.