public class BookRecommender {

    public static Book bestByAuthor(Book[] bookList, String author) {
        Book bestBook = null;
        double bestRating = 0.0;
        for (int i = 0; i < bookList.length; i++) {
            Book currBook = bookList[i];
            if (currBook.author().equals(author)) {
                if (currBook.rating() > bestRating) {
                    bestBook = currBook;
                    bestRating = currBook.rating();
                }
            }
        }
        return bestBook;
    }

    // find all of the books with that author
    // put them in a new array
    // print out each book from that array
    public static void recommendByAuthor(Book[] bookList, String author) {
        int numBooksByAuthor = 0;
        for (int i = 0; i < bookList.length; i++) {
            if (bookList[i].author().equals(author)) {
                numBooksByAuthor++;
            }
        }
        // at this point, numBooksWithAuthor tells us how many of our books
        // have the specified genre.
        Book[] booksByAuthor = new Book[numBooksByAuthor];
        int booksAdded = 0;
        for (int i = 0; i < bookList.length; i++) {
            if (bookList[i].author().equals(author)) {
                booksByAuthor[booksAdded] = bookList[i];
                booksAdded++;
            }
        }
        // at this point, booksWithAuthor contains references to all of the books with
        // the target author.
        for (int i = 0; i < booksByAuthor.length; i++) {
            System.out.println(booksByAuthor[i]);
        }

    }

    public static void main(String[] args) {
        String filename = args[0];
        In reader = new In(filename);

        int numBooks = reader.readInt();
        System.out.println(numBooks);

        Book[] books = new Book[numBooks];
        for (int i = 0; i < numBooks; i++) {
            reader.readLine(); // proceed to next line...
            String title = reader.readLine().trim();
            String author = reader.readLine().trim();
            int year = reader.readInt();
            int pages = reader.readInt();
            double rating = reader.readDouble();

            books[i] = new Book(title, author, year, pages, rating);
        }

        System.out.println(bestByAuthor(books, "Rachel Cusk"));
        recommendByAuthor(books, "Rachel Cusk");
    }
}