Solution to
Try It Yourself: using a book collection

  1. Report the title of every book in the library that is checked out.
    // Given that library is a Library object
    BookCollection bookColl = library.getBookCollection();
    System.out.println("The following books are checked out:");
    while (bookColl.hasMoreBooks()) {
    	Book book = bookColl.nextBook();
    	if (book.isCheckedOut())
    		System.out.println(book.getTitle());
    }
    
  2. Find out the number of books with the title given in the variable title.
    // Given that library is a Library object
    BookCollection bookColl = library.getBookCollection();
    int numBooks = 0;
    while (bookColl.hasMoreBooks()) {
    	Book book = bookColl.nextBook();
    	if (book.getTitle().equals(title))
    		numBooks++;
    }
    System.out.println("Number of books with the title " + title + 
                       ": " + numBooks);
    
  3. Find all the books written by the author given in the variable author.
    // Given that library is a Library object
    BookCollection bookColl = library.getBookCollection();
    while (bookColl.hasMoreBooks()) {
    	Book book = bookColl.nextBook();
    	if (book.getAuthor().equals(author))
    		System.out.println(book.getTitle() + " was written by " + author);
    }
    
  4. Check out the book titled Intro to CS to Pat Smith.
    // Given that library is a Library object
    BookCollection bookColl = library.getBookCollection();
    boolean found = false;
    while (bookColl.hasMoreBooks() && !found) {
    	Book book = bookColl.nextBook();
    	if (book.getTitle().equals("Intro to CS")) {
    		book.checkOut("Pat Smith");
    		found = true;
    	}
    }