import java.io.*;

/**
 * This class is for testing the read operation of AddressBookStorage.
 *
 * @author Dr. Caffeine
 */

class TestAddressBookFinal
{
  //--------------------------------
//    Data Members
//--------------------------------
    
    /**
     * An AddressBook object to be stored in a file
     */
    AddressBook         myBook;
   
    /**
     * A storage object that handles file i/o for AddressBook
     */
    AddressBookStorage  fileManager;


//--------------------------------
//    Constructors
//--------------------------------
    
    /**
     * Default constructor. 
     */
    public TestAddressBookFinal( int N )
    {
        myBook = new AddressBook(N);
  
        for (int i = 0; i < N; i++) {
           Person person = new Person("Ms. X" + i, 10, 'F');
           myBook.add(person);
        }
      
    }
    
//-------------------------------------------------
//      Public Methods:
//
//          void    printout   (   String     )
//          void    testRead   (   String     )
//          void    testWrite  (   String     )
//
//-------------------------------------------------
  
    /**
     * Prints out the data in the AddressBook loaded
     * from the file.
     *
     */ 
    public void printout(String name)
    {
        Person person;
  
        person = myBook.search( name );
  
        if (person != null) {
           System.out.println(person.getName());
           System.out.println(person.getAge());
           System.out.println(person.getGender());
        }
        else {
           System.out.println("Error: object not found");
        }
    }
    
    
    /**
     * Read the address book to the specified file.
     *
     * @param filename the name of a file to load AddressBook
     *
     */
    public void testRead( String filename )
    {
        fileManager = new AddressBookStorage( filename );
       
        try {
           myBook = fileManager.read();
        }
        catch (IOException e) {
           System.out.println("Error: IOException is thrown.");
        }
    }
    
    /**
     * Writes the address book to the specified file.
     *
     * @param filename the name of a file to store AddressBook
     *
     */
    public void testWrite( String filename )
    {
         fileManager = new AddressBookStorage( filename );
  
         try {
            fileManager.write(myBook);
         }
         catch (IOException e) {
            System.out.println("Error: IOException is thrown.");
         }
    }




}