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

I\'m creating a Library simulation in Java. My current code has a Book class tha

ID: 3807094 • 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 are LibraryMaterials, 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

public Library( ) Creates an empty ArrayList of books.

public boolean upload(book ebook)

First exams for null or empty Strings and calls the fine exception. in any other case it provides the e-book argument to the surrender of the library ArrayList. note it returns a boolean (whether or not or not or no longer the upload operation succeeded) See the add method of ArrayList:

public ArrayList findTitles(String identify)

Generates an ArrayList of all books which have titles that healthy (exactly) with the exceeded argument and returns this listing to the calling utility. The String compareTo technique is useful right here.

public void kind( )

sorts the library’s ebook ArrayList in ascending order in keeping with the name subject (kind those titles simply as they're, i.e. don’t worry approximately articles which incorporates The or A, and so forth.). As illustrated in the textbook, p. 666 (Don’t allow this huge variety difficulty you :) ), you can use the Collections kind.

public String toString( )

returns a well formatted String illustration of all the books in the library

import java.util.ArrayList;

import java.util.Collections;

public elegance Library

    personal ArrayList<Book> allBook = new ArrayList<Book>();

    public Library(ArrayList<Book> different)

        if (other == null)

            throw new NullPointerException("null pointer");

         else

            this.allBook = other

    public Library()

        this.allBook = new ArrayList<Book>();

    public boolean add(ebook book)

        if (e-book != null && !book.equals(""))

            throw new IllegalArgumentException("cannot be empty");

        allBook.add(e book);

        return proper;

    public ArrayList<Book> findTitles(String title)

        for(book b: allBook)

            if(name.compareTo(b.getTitle())== zero)

                return allBook;

        return null;

   public void kind()

        Collections.sort(allBook);

        public String toString()

        return Library.this.toString();

        public elegance e-book implements comparable<Book>

    private String bookTitle;

    private ArrayList<String> bookAuthor;

    public e-book(String title, ArrayList<String> authors)

        if(identify == null && authors == null)

            throw new IllegalArgumentException("cannot be null");

          if(identify.isEmpty() && authors.isEmpty())

throw new IllegalArgumentException("can not be empty");

      bookTitle = name;

        bookAuthor = authors;

        public String getTitle()

        go back bookTitle;

    public ArrayList<String> getAuthors( )

        return bookAuthor;

        public String toString( )

        go back bookTitle + bookAuthor;

    public int compareTo(e-book other)

        return bookTitle.compareTo(other.bookTitle);

public boolean equals(item o)

         if(!(o instanceof e book))

             go back fake;

          e-book b = (ebook) o;

         return b.bookTitle.equals(bookTitle)

                 && b.bookAuthor.equals(bookAuthor);

Now you have got special constructors that do the proper component different methods. To store your self preservation headaches, you would commonly favor to have one code route that does "the whole lot", so it would be true to combine the two. There are extraordinary approaches of doing that, relying at the requirements....do not forget this take a look at.

ArrayList supply = new ArrayList();

source.add(book1);

supply.upload(book2);

source.add(book3);

Library library = new Library(source);

library.add(book4);

At this point, the library ought to comprise four books. however what number of books ought to the source array incorporate?

If the solution is four, the the library is meant to be enhancing the array it changed into passed. in that case, the easy answer to the constructor trouble is to have the default constructor call the other, like so:

Library ()

    this(new ArrayList<Book>);

then again, if the supply array must nevertheless incorporate three books, then the Library should include a copy of the ArrayList, in preference to holding onto the authentic. If it is your requirement, then the coolest answer is to head the alternative manner round - initialize the allBook member wherein you claim it, however use replica semantics whilst you are surpassed the ArrayList

ArrayList<Book> allBook = new ArrayList<Book> ();

Library ()

Library (ArrayList<Book> books)

    allBook.addAll(books);

Your code in this model of the puzzle could be very confused approximately Books, titles, and Strings.

public ArrayList<Book> findTitles(book title)

The code in some of places shows that, as soon as upon a time, you idea all books have been simply Strings, and you then modified your thoughts. Your compiler should be telling you which you are not being regular.

The necessities for findTitles says which you should be returning an ArrayList of books with matching titles. meaning you probably want to be growing a brand new ArrayList, after which add()ing the matching Books to it. The code you have written returns all of the books, however takes extra time to examine on the books first. You probable don't want to call e book.compareTo (although you may implement the answer effectively that way), but as an alternative book.getTitle().equals(otherBook.getTitle())

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

public Library(ArrayList<Book> different)

    if (other == null)

        throw new NullPointerException("null pointer");

     else

        allBook = different;

The else right here is redundant after the guard clause, this approach will be written like this:

public Library(ArrayList<Book> different)

    if (other == null)

        throw new NullPointerException("null pointer");

   allBook = other;

Library.toString() (spoiler):

In Library.upload(), your if-condition is inaccurate. also, the trouble specifies that it must go back a boolean indicating whether the operation succeeded. it truly is open to interpretation: what constitutes a failure? jogging out of memory? now not probably; the commonplace movement in that case might be to helplessly permit the OutOfMemoryException propagate. What in case you attempt to add the equal e book to the library two times? that is an affordable failure mode. What if you upload a second reproduction of a e book that is already within the library? That must succeed, i'd assume.

In Library.findTitles(), your for-loop is inaccurate. A commonplace idiom in languages with 0-primarily based arrays is…

for (int i = 0; i < n; i++) ...

anyhow, modern-day Java code would commonly say rather…

for (ebook b : allBook) ...

The specification says that .findTitles() ought to go back a list of matching books — so wherein's your new listing?

a touch for Library.toString(): you want to build a protracted string, so use a StringBuilder object. For every e-book in the library, you will need to appends its string illustration to the result.

inside the book() constructor, your validation in all likelihood does not behave the manner you intended.

In e book.toString(), just slapping bookTitle and bookAuthor collectively is not likely to yield a exceptional result.

because you overrode e book.equals(), you should also implement book.hashCode() to be constant. in any other case, you'll no longer be capable of keep Books in a HashMap or Hashtable.

i might rename your instance variables allBook and bookAuthor using their plurals. The code will read more smoothly, and logic errors can be less probable.

I do no longer like the fact of killing objects within the constructor. The activity of a constructor is: to initialize the object in a sane country - now not killing it for person's faults.

Naming you may enhance your naming a bit bit: * allBooks must be renamed to just books, for the reason that ArrayList consists of all books. allBooks is a tautology.

•             upload may be renamed to addBook - of course you may item, that this is a tautologytoo; due to the fact every IDE suggests you, which type is expected. but now not every body makes use of an IDE. From this i might opt for addBook

real Code

First exams for null or empty Strings and calls the right exception. otherwise it provides the ebook argument to the quit of the library ArrayList. observe it returns a boolean (whether or not the upload operation succeeded) See the add method of ArrayList:

once more: those are very terrible specifications! Why on this planet do you have to go back a boolean to signal, that there has been no error, while the preceding sentence really states, you need to throw an Exception if something goes incorrect? And extra: Who cares for a boolean return-price? The only issue, which might make any experience, could be returning some form of identification in a persistence-context or for heavens sake the overall variety of books, however not a boolean.

i'm sad with that implementation. Of course, your e book has a toString()-method and you're checking if the book is literaly a blank e-book. but doing it this manner:

public boolean upload(book ebook)

    if (book != null && !e-book.equals(""))

        throw new IllegalArgumentException("cannot be empty");

      allBook.add(ebook);

    return real;

It reads incorrect: »check, whether or not the book is an empty string!« that does not make any sense in any respect. And really: before everything, I failed to get it, until I noticed, the toString().

A higher manner of expressing this will be, including a technique to e book:

public boolean isBlank()

    go back bookAuthor.isEmpty() && bookTitle.isEmpty();

And the check turns into extra readable and makes sense

public boolean addBook(book e-book)

    if (book e-book.isBlank())

        throw new IllegalArgumentException("cannot be empty");

       allBook.add(e-book);

    return real;

»If the book is null or the e book is clean throw an IllegalArgumentException« Btw. Why did you you operate the negative from: !a && !b as opposed to just a b?

public ArrayList<Book> findTitles(String title)

    for(book b: allBook)

        if(name.compareTo(b.getTitle())== zero)

            go back allBook;

return null;

You iterate over all books in the library, and if one e book matches, you return the complete library. it is no longer, what you intended. And greater Do now not go back null! Please, in no way ever! that does not make any experience and ends in immoderate use of guard Clauses towards null. it's far a clean antipattern. In Java8 there is a nicer answer if you want to hide null in an optional.

Say, your instructor asks you to look for a particular book at the shelf and also you do surely nothing - what does that mean for your teacher? A) you are deaf or b) you're dumb or c) you are neither, but there may be no ebook. If someone asks you for a list of books, while there is none, virtually return an empty list, and say with courtesy: »Sorry, the listing is empty, because there has been no such e-book«.

public ArrayList<Book> findTitles(String become aware of)

    ArrayList<Book> end result=new ArrayList<>();

    for(e book b: allBook)

        if(title.compareTo(b.getTitle())== 0)

            quit result.add(b);

      cross again end result;

How does this make revel in in any way? consider someone going to a library, with a wizards hat on and a big staff: »kind you, I, as your master, command you!«.

public void type()

    Collections.type(allBook);

Of direction, the implementation is in step with the specification correct. but that doesn't make simply sense. i might select options:

•             If it's miles critical, that your library is looked after in a few manner, you can select to normally have a taken care of series of books

•             otherwise, i would generate a taken care of listing on the fly The implementation relies upon at the usage.

And what is that?

public String toString()

    return Library.this.toString();

The specification have become for once clean at this factor:

returns a nicely formatted String representation of all of the books in the library (identify accompanied by using authors).

public String toString()

    StringBuilder sb=new StringBuilder();

    for(ebook b:books) sb.append(b.toString());

    return sb.toString();

That need to do the trick.

permit's communicate approximately ebook:

public e-book(String perceive, ArrayList<String> authors)

    if (pick out == null && authors == null)

        throw new IllegalArgumentException("can't be null");

      if (become aware of.isEmpty() && authors.isEmpty())

        throw new IllegalArgumentException("can not be empty");

        bookTitle = name;

    bookAuthor = authors;

much like above: do not throw exceptions in a constructor! furthermore: in case you do not allow blank books, why take a look at for blank books in the Library? Are you afraid of malicious subclassesof books, which permit clean books?

public int compareTo(e-book extraordinary)

    go back bookTitle.compareTo(other.bookTitle);

Why most effective have a look at the name? There are at least books calledFrench Kitchen, but they may be no longer the same, for the reason that authors range.

public ArrayList<String> getAuthors()

    move again bookAuthor;

this is naughty and could bite you:

public static void important(String[] args)

    ArrayList<String> authors=new ArrayList<>();

    authors.add("1");

    authors.upload("2");

    authors.upload("3");

    authors.upload("4");

    e book b=new ebook("check", authors);

    ArrayList<String> list1=b.getAuthors();

    list1.add("five");

    ArrayList<String> list2=b.getAuthors();

    for(String s:list2)

        tool.out.println(s);

you are leaking the reference of your inner authors.

public listing<String> getAuthors()

    listing<String> end result=new ArrayList<>();

    for(String a:bookAuthor) stop result.upload(a);

    return stop end result;

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