// Arup Guha
// 2/18/2026
// Second class for HAS-A relationship example.

import java.util.*;

public class Author {

	private String first;
	private String last;
	private int numBooks;
	private Book[] books;
	
	// Constructor for an Author with no books.
	public Author(String firstName, String lastName) {
		first = firstName;
		last = lastName;
		numBooks = 0;
		books = new Book[10];
		Arrays.fill(books, null);
	}
	
	// Returns the number of books by this Author.
	public int getNumBooks() {
		return numBooks;
	}
	
	// We'll worry about exceptions later.
	// Returns a reference to the ith book (0 based) by this Author.
	public Book getBook(int i) {
		return books[i];
	}
	
	// Returns a String representation of this Author.
	public String toString() {
		String build = first+" "+last+" collection of books\n";
		for (int i=0; i<numBooks; i++)
			build = build + books[i] + "\n";
		return build;
	}
	
	// Adds a book to the ones written by this Author.
	public void addBook(Book newBook) {
	
		// Need more room.
		if (numBooks == books.length) {
			Book[] newlist = Arrays.copyOf(books, 2*books.length);
			books = newlist;
		}
		
		// Add this book.
		books[numBooks++] = newBook;
	}
	
	public static void main(String[] args) {
		
		// Create one Author object.
		Scanner stdin = new Scanner(System.in);
		System.out.println("What is the author's name?");
		String first = stdin.next();
		String last = stdin.next();
		Author shakespeare = new Author(first, last);
		
		// For basic testing.
		while (true) {
			
			// Get info and add a book.
			System.out.println("What book title?");
			String title = stdin.next();
			System.out.println("How many pages?");
			int mypages = stdin.nextInt();
			System.out.println("Year published?");
			int year = stdin.nextInt();
			Book tmp = new Book(title, mypages, year);
			shakespeare.addBook(tmp);
			
			// Print out 
			System.out.println(shakespeare);
			System.out.println("----------------------------");
			
			// For fun, read some pages in each book.
			for (int i=0; i<shakespeare.getNumBooks(); i++)
				shakespeare.getBook(i).read(5);
			
			// Allow us to stop!
			System.out.println("get out?");
			String ans = stdin.next();
			if (ans.equals("yes")) break;
		}
	}
	
}