Book program with arrayList I have this Book program that contains 2 classes: Book and Library.  I have the Book class done, but need some help on the Library class. Please help me check my code. I can provide the Book class if you need it.

Instruction here:


  Fields: A single private ArrayList of Book field is all that is
  necessary. This will hold all the Book objects in the library.
  
  Methods: public Library(ArrayList other)
  
  Throws a NullPointerException if other is null. Otherwise, the
  Library’s Book ArrayList should take on all the books in other.
  
  public Library( ) Creates an empty ArrayList of books.
  
  public boolean add(Book book)
  
  First checks for null or empty Strings and calls the appropriate
  exception. Otherwise it adds the Book argument to the end of the
  library ArrayList. Notice it returns a boolean (whether or not the add
  operation succeeded) See the add method of ArrayList:
  
  public ArrayList findTitles(String title)
  
  Generates an ArrayList of all books which have titles that match
  (exactly) with the passed argument and returns this list to the
  calling program. The String compareTo method is useful here.
  
  public void sort( )
  
  Sorts the library’s book ArrayList in ascending order according to the
  title field (sort these titles just as they are, i.e. don’t worry
  about articles such as The or A, etc.). As illustrated in the
  textbook, p. 666 (Don’t let this number concern you :) ), you will use
  the Collections sort.
  
  public String toString( )
  
  returns a properly formatted String representation of all the books in
  the library (Title followed by authors).


    import java.util.ArrayList;


import java.util.Collections;

public class Library {
    private ArrayList&lt;Book&gt; allBook = new ArrayList&lt;Book&gt;();

    public Library(ArrayList&lt;Book&gt; other) {
        if (other == null) {
            throw new NullPointerException("null pointer");
        } else
            this.allBook = other;
    }

    public Library() {
        this.allBook = new ArrayList&lt;Book&gt;();
    }

    public boolean add(Book book) {
        if (book != null &amp;&amp; !book.equals("")) {
            throw new IllegalArgumentException("Can't be empty");
        }
        allBook.add(book);
        return true;
    }

    public ArrayList&lt;Book&gt; findTitles(String title) {
        for(Book b: allBook) {
            if(title.compareTo(b.getTitle())== 0) {
                return allBook;
            }
        }
        return null;
    }

    public void sort() {
        Collections.sort(allBook);
    }

    public String toString() {
        return Library.this.toString();
    }
}

    public class Book implements Comparable&lt;Book&gt; {
    private String bookTitle;
    private ArrayList&lt;String&gt; bookAuthor;

    public Book(String title, ArrayList&lt;String&gt; authors) {
        if(title == null &amp;&amp; authors == null) {
            throw new IllegalArgumentException("Can't be null");
        }
        if(title.isEmpty() &amp;&amp; authors.isEmpty()) {
            throw new IllegalArgumentException("Can't be empty");
        }
        bookTitle = title;
        bookAuthor = authors;
    }

    public String getTitle() {
        return bookTitle;
    }
    public ArrayList&lt;String&gt; getAuthors( ) {
        return bookAuthor;
    }

    public String toString( ) {
        return bookTitle + bookAuthor;
    }
    public int compareTo(Book other){
        return bookTitle.compareTo(other.bookTitle);
    }
    public boolean equals(Object o) {
         if(!(o instanceof Book)) {
             return false;
        }
         Book b = (Book) o;
         return b.bookTitle.equals(bookTitle)
                 &amp;&amp; b.bookAuthor.equals(bookAuthor);
    }
}
Your default constructor Library() initializes the allBook member variable twice, with the same value in each case.  Probably not what you had in mind.  The immediate fix is to remove the (re) assignment in the default constructor.

Now you have two different constructors that do the right thing two different ways.  To save yourself maintenance headaches, you'd normally prefer to have one code path that does "everything", so it would be good to combine the two.  There are two different ways of doing that, depending on the requirements....

Consider this test.

ArrayList source = new ArrayList();
source.add(book1);
source.add(book2);
source.add(book3);

Library library = new Library(source);
library.add(book4);


At this point, the library should contain four books.  But how many books should the source array contain?

If the answer is four, the the library is supposed to be modifying the array it was passed.  In that case, the easy answer to the constructor problem is to have the default constructor call the other, like so:

Library () {
    this(new ArrayList&lt;Book&gt;);
}


On the other hand, if the source array should still contain three books, then the Library should contain a copy of the ArrayList, rather than holding onto the original.  If that's your requirement, then the good answer is to go the other way around - initialize the allBook member where you declare it, but use copy semantics when you are passed the ArrayList

ArrayList&lt;Book&gt; allBook = new ArrayList&lt;Book&gt; ();

Library () {}

Library (ArrayList&lt;Book&gt; books) {
    allBook.addAll(books);
}


Your code in this version of the puzzle is very confused about Books, titles, and Strings.  Your requirements said 

public ArrayList findTitles(String title)


but the signature in your class is

public ArrayList&lt;Book&gt; findTitles(Book title)


The code in a number of places suggests that, once upon a time, you thought all books were just Strings, and then you changed your mind.  Your compiler should be telling you that you aren't being consistent.

The requirements for findTitles says that you should be returning an ArrayList of books with matching titles.  That means you probably need to be creating a new ArrayList, and then add()ing the matching Books to it.  The code you've written returns all of the books, but takes extra time to compare at the books first.  You probably don't want to call Book.compareTo (although you can implement the solution correctly that way), but instead Book.getTitle().equals(otherBook.getTitle())

There are better answers than for loops for visiting all of the elements in an ArrayList.  See Iterable.  Since you've written Book.toString() already, you can start Library.toString() by building a big String out of all of the Book.toString()s.