/**
 * A library has a collection of books stored in an array.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

import java.util.Scanner;
import java.io.*;

public class Library
{
    // instance variables
    private final int MAX_NUM_BOOKS = 500;
    private Book[] bookArray = new Book[MAX_NUM_BOOKS];
    private int numBooks; // actual number of books

    /**
     * Constructor for objects of class Library
     */
    public Library()
    {
        // The array (this.bookArray) is initialized in method readFromFile()
        this.numBooks = 0;
    }
    
    /**
     * readFromFile: read from specified file into an array of books, and keep track of the number of books.
     * 
     * @param  fileName  the name of the input file
     * @return none
     */
    public void readFromFile(String fileName) throws IOException
    {
        Scanner fileScan = new Scanner (new File(fileName));
        
        while (fileScan.hasNext())  // while there is more to read from the file
        {
            // Read the next line from the file.
            String oneLineOfBookInfo = fileScan.nextLine();
            
            // Create a Scanner object for the purpose of parsing that line.
            Scanner bookScan = new Scanner (oneLineOfBookInfo);
            
            // Set delimiter, i.e. what separates one token from the next.
            bookScan.useDelimiter (", ");
            
            // Extract the book information from the line that we just read from the file.
            String nextTitle = bookScan.next();
            String nextAuthor = bookScan.next();
            String nextPublisher = bookScan.next();
            int nextCopyrightDate = bookScan.nextInt();
            
            // Create a Book object with the info that we just extracted,
            // and store it in the next available position in the array of Book objects.
            this.bookArray[numBooks] = new Book (nextTitle, nextAuthor, nextPublisher, nextCopyrightDate);    
            this.numBooks ++;
        }
    }
   
    /**
     * toString: 
     * @param none
     * @return a String containing the info of each book stored in the instance variable bookArray.
     */ 
    public String toString()
    {
        String result = "";
        
        for (int i = 0; i < this.numBooks; i++)
        {
            result += this.bookArray[i].toString() + "\n";    
        }
        
        return result;
    }

    /**
     * Test this class by 
     * calling its methods to read from a file into an array of books,
     * and print the array of books.
     */
    public static void test() throws IOException
    {
        // Create a Library object named myLibrary.
        Library myLibrary = new Library ();
        
        // Read information about books from a file into myLibrary.
        myLibrary.readFromFile ("books.txt");
        
        // Print all the books in myLibrary.
        System.out.println (myLibrary.toString());
        
        // Print a message indicating that the program is done.
        System.out.println ("End of run.");
    }    
}


