Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java: Create a book Library in Java. I have a Java code that emulates a library. It does simple functions such as checkout, check in,

Java: Create a book Library in Java.

I have a Java code that emulates a library. It does simple functions such as checkout, check in, set due dates, return list if books, etc.

Revise the code to take into account the fact that libraries typically contain other materials in addition to books.

Create a LibraryMaterial 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, we're going to declare LibraryMaterialCopy an abstract class. More on this later.

***My understanding of this question is that the code needs to be revised where a new class, LibraryMaterials, should hold all the data, Book name, ISBN, Author, LibraryCard user, etc. All of which is in my Book, BookCopy and LibraryCard class. Those classes will become subclasses using inheritance to access those information. So LibraryMaterials is a superclass, with 5 subclasses, Book, BookCopy, DVD, DVDCopy and Library class. Each subclass uses inheritance to access information relating to them, such as ISBN or DVD name. This is the best I can explain it.

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 are LibraryMaterials, and they have a "main actor" (on the BPL website, there is a list of "additional contributors," but I'm simplifying here).

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. Hint: you probably want an abstract getFinePerDay() method.

2. Books are checked out for three weeks. DVDs are checked out for only two weeks. Hint: you probably want 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 .

* Note that DVDs do not typically have ISBN and have ISAN instead, and that really the library should just give unique ID numbers to each library material . But for simplicity, we're just going to call everything ISBN

My Code ************************************************ class Book { private String isbn; private String title; private String author; public Book (String i, String t, String a) { isbn = i; title = t; author = a; } public String getIsbn () {return isbn;} public String getTitle() {return title;} public String getAuthor() {return author;} }

************************************************* import java.time.LocalDate; public class BookCopy { 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; } public Book getBook() {return book;} public String getTitle() {return book.getTitle();} 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(BORROWING_WEEKS); return true; } public boolean checkOut (LibraryCard borrower) //default check out method that uses todays' date { return checkOut(borrower, LocalDate.now()); } public boolean returnBook() //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()); } }

************************************************ import java.util.ArrayList; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class LibraryCard { private String id; private String cardholderName; private ArrayList booksCheckedOut; private double balance; public LibraryCard(String i, String name) { id = i; cardholderName = name; booksCheckedOut = new ArrayList(); balance = 0; } public String getID() {return id;} public String getCardholderName() {return cardholderName;} public ArrayList getBooksCheckedOut() {return booksCheckedOut;} public void setCardholderName (String name) {cardholderName = name;} public boolean checkOutBook (BookCopy book, LocalDate todaysDate) //checks out book and sends message to BookCopy to check itself out too //returns false if book is already checked out //takes parameter that reflects the date that the checkout is happening { if (!book.checkOut(this,todaysDate) return false; booksCheckedOut.add(book); return true; } public boolean checkOutBook(BookCopy book) //default check out, uses today's date { return checkOutBook(book, LocalDate.now()); } public boolean returnBook (BookCopy book, LocalDate returnDate) //returns book and sends message to BookCopy to do the same //returns false if book is not checked out //takes parameter that expresses the date of return { LocalDate dueDate = book.getDueDate(); if (!book.returnBook()) return false; if (!booksCheckedOut.remove(book)) return false; long daysBetween = ChronoUnit.DAYS.between(dueDate, returnDate); if (daysBetween > 0) //book is returned late { balance += BookCopy.FINE_PER_DAY * daysBetween; } return true; } public boolean returnBook (BookCopy book) //default method, uses today's date as returns date { return returnBook(book, LocalDate.now()); } public boolean renewBook(BookCopy book, LocalDate renewalDate) //renews book. Returns false if book is not checked out already //takes parameter that expresses date of renewal { if (!booksCheckedOut.contains(book)) return false; if (!book.renew(renewalDate)) return false; return true; } public boolean renewBook (BookCopy book) //default renewal method uses today's date as renewal date. { return renewBook(book, LocalDate.now()); } public ArrayList getBooksDueBy(LocalDate date) //returns an ArrayList of books due on or before date { ArrayList booksDue = new ArrayList(); for (BookCopy book: booksCheckedOut) { if (book.getDueDate().isBefore(date) || book.getDueDate().equals(date)) { booksDue.add(book); } } return booksDue; } public ArrayList getBooksOverdue (LocalDate todaysDate) //returns books overdue as of todaysDate //which means that they were actually due by yesterday { return getBooksDueBy(todaysDate.minusDays(1)); } public ArrayList getBooksOverdue() //default method, returns books overdue as of today, which means that they //were due by yesterday { return getBooksOverdue(LocalDate.now()); } public ArrayList getBooksSorted() //returns ArrayList of books, sorted by due date (earliest due date first) //uses insertion sort { for (int i = 1; i < booksCheckedOut.size(); i++) { int j = i; while (j > 0 && booksCheckedOut.get(j-1).getDueDate().isAfter(booksCheckedOut.get(j).getDueDate())) { //swap elements in positions j and j-1 BookCopy temp = booksCheckedOut.get(j); booksCheckedOut.set(j, booksCheckedOut.get(j-1)); booksCheckedOut.set(j-1, temp); j = j-1; } } return booksCheckedOut; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions

Question

Determine miller indices of plane A Z a/2 X a/2 a/2 Y

Answered: 1 week ago