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

Answer must be written in Java, thankl you! A book consists of an author, title,

ID: 3878856 • Letter: A

Question

Answer must be written in Java, thankl you!

A book consists of an author, title, and ISBN number (all of which can never change once the book is created).

A library book is a book that also contains a due date and the current holder of the book, which is either a String representing a person who has checked the book out or null if the book is currently not checked out. Both the due date and holder of the book can change over time.

A library contains library books and supports the following operations:

1. Add a library book to the library.

2. Check out a library book by specifying its ISBN number and new holder and the due date.

3. Determine the current holder of a library book given its ISBN number.

a. Write two interfaces: Book and LibraryBook that abstract the functionality described above.

b. Write a library class that includes the three methods specified. In implementing the Library class, you should maintain the library books in an ArrayList. You may assume that there are never any requests to add duplicate books.

Explanation / Answer

Hey, please follow along, I have comments wherever required..

Book.java

public class Book {

private String author;

private String title;

private String ISBN;

public Book(String author, String title, String ISBN) {

this.author = author;

this.title = title;

this.ISBN = ISBN;

}

// Do not provide setters, as we do not want book contents to be changed,

// Also Strings are immutable, so providing getters are safe

public String getAuthor() {

return author;

}

public String getTitle() {

return title;

}

public String getISBN() {

return ISBN;

}

}

LibraryBook.java

import java.util.Date;

public class LibraryBook extends Book {

private Date dueDate;

private String currentHolder;

public LibraryBook(String author, String title, String ISBN) {

super(author, title, ISBN);

dueDate = null;

currentHolder = null;

}

public Date getDueDate() {

return dueDate;

}

public void setDueDate(Date dueDate) {

this.dueDate = dueDate;

}

public String getCurrentHolder() {

return currentHolder;

}

public void setCurrentHolder(String currentHolder) {

this.currentHolder = currentHolder;

}

}

ILibraryBook.java

import java.util.Date;

public interface ILibraryBook {

public boolean addBookToLibrary(LibraryBook book);

public boolean checkOutABook(String ISBN, String newHolder, Date date);

public LibraryBook currentHolder(String ISBN);

}

Library.java

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

public class Library implements ILibraryBook {

private List<LibraryBook> libraryBooks = new ArrayList<>();

@Override

public boolean addBookToLibrary(LibraryBook book) {

return libraryBooks.add(book);

}

@Override

public boolean checkOutABook(String ISBN, String newHolder, Date date) {

LibraryBook book = currentHolder(ISBN);

if (book == null) {

System.out.println("Book with " + ISBN + " not found!!");

return false;

}

if (book.getCurrentHolder() == null) {

book.setCurrentHolder(newHolder);

book.setDueDate(date);

return true;

} else { // Do not allow if the book has a holder..

System.out.println("Book is reserved till " + book.getDueDate() + " by " + book.getAuthor());

return false;

}

}

@Override

public LibraryBook currentHolder(String ISBN) {

for (LibraryBook book : libraryBooks) {

if (book.getISBN().equals(ISBN)) {

return book;

}

}

return null;

}

}

Thanks and Happy Learning!!!!

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