package class10_1_FunctionalExample_Library_start;// Dr. Yoder. MSOE. 02 December 2016 import java.util.ArrayList; import java.util.InputMismatchException; import java.util.List; /** * This class represents a home (paper hardcopy) library collection */ public class Library { private ArrayList books = new ArrayList<>(); /** * Pull the book off of the shelf by the specific author. * @param author the full name of the person who wrote the book. * @return the requested book (or null if not found! * ... wish there was a better way to handle this....) * @throws InputMismatchException if book not found */ public Book findByAuthor(String author) throws InputMismatchException { int i = 0; Book found; while(i < books.size() && !books.get(i).getAuthor().equals(author)) { i++; } if(i == books.size()) { throw new InputMismatchException("author did not match any book"); // TODO: better exception } else { found = books.get(i); } return found; } public Book findByTitle(String title) throws InputMismatchException { int i = 0; Book found; while(i < books.size() && !books.get(i).getTitle().equals(title)) { i++; } if(i == books.size()) { throw new InputMismatchException("author did not match any book"); // TODO: better exception } else { found = books.get(i); } return found; } public Book find(Matcher condition, String searchTerm) throws InputMismatchException { int i = 0; Book found; while(i < books.size() && !condition.match(books.get(i),searchTerm)) { i++; } if(i == books.size()) { throw new InputMismatchException("author did not match any book"); // TODO: better exception } else { found = books.get(i); } return found; } /** * Add a set of books to the library. * Null references are skipped and not added to the library. * (There must be a better way to handle nulls!) * @param books the books to be added */ public void addBooks(List books) { if(books != null) { for (Book book : books) { if (book != null) { this.books.add(book); } } } } }