import javabook.*;

/*
 * Introduction to OOP with Java 2nd Edition, McGraw-Hill
 *	
 *  A sample program from Section 9.2 to illustrate the
 *  basic processing of an array of objects.
 *
 *  
 * @author Dr. Caffeine
 *
 */
class ProcessPersonArray
{
    
    public static void main (String[] args)
    {
        MainWindow  mainWindow;
        OutputBox 	outputBox;
        InputBox    inputBox;  
        
        mainWindow = new MainWindow("Process Person Program");
        outputBox  = new OutputBox( mainWindow );
        inputBox   = new InputBox ( mainWindow );
                
        mainWindow.setVisible( true ); 
        outputBox.setVisible( true );
        
        
        Person[]    person;         //declare the person array
        person = new Person[5];    //and then create it


        //----------- Create person Array -----------------//
        
        String      name, inpStr;
        int         age;
        char        gender;

        for (int i = 0; i < person.length; i++) {
            
            //read in data values
            name    = inputBox.getString ( "Enter name:"   );
            age     = inputBox.getInteger( "Enter age:"    );
            inpStr  = inputBox.getString ( "Enter gender:" );
            gender  = inpStr.charAt(0);
        
            //create a new Person and assign values
            person[i] = new Person( );
        
            person[i].setName  ( name   );
            person[i].setAge   ( age    );
            person[i].setGender( gender );
        }
        
        //-------------- Compute Average Age --------------//
        
        float sum = 0, averageAge;

        for (int i = 0; i < person.length; i++) {
            
            sum += person[i].getAge();
        }
        
        averageAge = sum / (float) person.length;
        
        outputBox.printLine( "Average age: " + averageAge );
        outputBox.skipLine( 1 );
        
        
        //------ Find the youngest and oldest person ----------//
        //------ Approach No. 3: Using person reference -------//
        
        Person    youngest,       //points to the youngest person
                  oldest;         //points to the oldest person

        youngest = oldest = person[0];
        
        for (int i = 1; i < person.length; i++) {
        
            if ( person[i].getAge() < youngest.getAge() ) { 
                //found a younger person
                youngest   = person[i];
            }
            else if ( person[i].getAge() > oldest.getAge() ) {                            
                //found an older person
                oldest     = person[i];
            }
        }
        
        outputBox.printLine("Oldest  : " + oldest.getName() 
                    + " is " +   oldest.getAge() + " years old.");
        
        outputBox.printLine("Youngest: " + youngest.getName() 
                    + " is " + youngest.getAge() + " years old.");
                    
                    
        //----------- Search for a particular person ------------//
                    
        String searchName = inputBox.getString( "Name to search:" );
        
        int i = 0;

        while ( i < person.length &&     //still more persons to search
                !person[i].getName().equals( searchName ) ) {
            i++;
        }
        
        if (i == person.length) {
            //not found - unsuccessful search
            outputBox.printLine( searchName + " was not in the array" );
        }
        else {
            //found - successful search
            outputBox.printLine("Found " + searchName + " at position " + i);
        }

   }
    
}