import java.util.Scanner;
import javax.swing.JOptionPane;


// This example shows an array of Person returned from a method

public class Example2B {
    public Example2B() {
        Person[] people = getPersonArray();
        String out = "The full names are:\n";
        for( Person person : people ){
            out += person + "\n";
        } // end for
        JOptionPane.showMessageDialog(null,out);
    } // end Example2B()
    
    public static void main(String[] args) {
        new Example2B();
    } // end main
    
    Person[] getPersonArray() {
        int size = 1;
        int total = 0;
        Person[] people = new Person[size];
        String name = JOptionPane.showInputDialog("Enter your full name.\nClick Cancel to quit.");
        while( name != null ){
            if( total == size ){
                Person[] save = people;
                people = new Person[2*size];
                for( int i = 0; i < size; i++ ){
                    people[i] = save[i];
                } // end for
                size = 2*size;
            } // end if
            Scanner s = new Scanner(name);
            String first = s.next();
            String last;
            if( s.hasNext() ){
                last = s.next();
            } else {
                last = "";
            } // end if
            Person aPerson = new Person(total+"",first,last);
            people[total] = aPerson;
            total++;
            JOptionPane.showMessageDialog(null,"Hi " + aPerson.getFirstName() +". How are you?");
            name = JOptionPane.showInputDialog("Enter your full name.\nClick Cancel to quit.");
        } // end while
        // the return array's capacity is set to the "right" size
        Person[] returnArray = new Person[total];
        for( int i = 0; i < total; i++ ) {
            returnArray[i] = people[i];
        } // end for
        return returnArray;
    } // end getArray
    
} // end Example2B


