/**
 * Test the Step 4 AddressBook class.
 * This is a controller class called from
 * Tester.
 *
 * Make sure you try out many other variations
 * of mixing the sequence of add, search, and 
 * delete operations.
 */

class TestAddressBook
{
    private static boolean SUCCESS = true;
    
    AddressBook     myBook;
    
    public void setupArray( int N )
    {
        Person      person;
    
        myBook = new AddressBook( N );

        //add N Person objects
        for (int i = 0; i < N; i++) {
            person = new Person( "Ms. X" + i, 10, 'F' );
            myBook.add( person );
        }
    }

    public void testOperations( )
    {
        boolean status;
        Person  person;
        
        
        person = myBook.search( "Ms. X7" );      //search for X7
        displaySearchResult( person, "Ms. X7" );
        
        
        status = myBook.delete( "Ms. X7" );      //delete X7
        displayDeleteResult( "Ms. X7", status );
        

        person = myBook.search( "Ms. X7" );       //search now non-existent X7
        displaySearchResult( person, "Ms. X7" );
        
        
        myBook.delete( "Ms. X9" );               //delete X9
        
        
        for (int i = 50; i < 55; i++) {                  //add five more people
            person = new Person( "Ms. X" + i, i, 'F' );
            myBook.add( person );
        }
        
        person = myBook.search( "Ms. X1" );      //search for the first person
        displaySearchResult( person, "Ms. X1" );
        
        
        person = myBook.search( "Ms. X54" );     //search for the last person
        displaySearchResult( person, "Ms. X54" );
        
        
        person = myBook.search( "Ms. X9" );     //search now non-existent X9
        displaySearchResult( person, "Ms. X9" );

               
    }
    
        
    private void displaySearchResult( Person p, String name )
    {
        if ( p == null ) {
            
              System.out.println( name + " is not found in the address book." );
        }
        else {
            
              System.out.println( p.getName() + " is found.");
        }
    }
    
    private void displayDeleteResult( String name, boolean status )
    {
        if ( status == SUCCESS ) {
            
            System.out.println( name + " removed from the address book." );
            
        }
        else {
            
            System.out.println( name + " was not removed." );
        }
    }
            
}