/**
 * This class is designed to manage an address book that contains
 * Person objects. The user can specify the size of the address book
 * when it is created. If no size is specified, then the default size
 * is set to 25 Person objects.
 *
 * @author Dr. Caffeine
 *
 */
class AddressBook        //Step 2: Implement the add method
{

//--------------------------------
//    Data Members
//--------------------------------
    
    /**
     * Default size of the array
     */
    private static final int  DEFAULT_SIZE = 25;
    
    /**
     * The array of Person objects
     */
    private Person[]   entry;
    
    /**
     * The number of elements in the <code>entry</code> array,
     * which is also the position to add the next Person object
     */
    private int        count;   
    
//--------------------------------
//    Constructors
//--------------------------------
    
    /**
     * Default constructor. 
     * Creates an address book of size 25.
     */
    public AddressBook( )
    {
        this( DEFAULT_SIZE ); 
    }
    
    
    /**
     * Creates an address book with the designated size.
     *
     * @param size the size of this address book.
     */
    public AddressBook( int size )
    {
        count = 0;
        
        if (size <= 0 ) { //invalid data value, use default
            size = DEFAULT_SIZE;
        }
        
        entry = new Person[size];
        
        System.out.println("array of "+ size + " is created."); //TEMP
    }
    
    
//-------------------------------------------------
//      Public Methods:
// 
//          void  add   (   Person        )
//
//------------------------------------------------
    
    /**
     * Adds a new Person to this address book.
     * If the overflow occurs, the array size
     * is increased by 50 percent.
     *
     * @param newPerson a new Person object to add
     */
    public void add( Person newPerson )
    {
        if (count == entry.length) {   //no more space left, 
            enlarge( );                //create a new larger array
        }
    
        //at this point, entry refers to a new larger array
        entry[count] = newPerson;
        count++;
    }
 
    
//-------------------------------------------------
//      Private Methods:
// 
//          void  enlarge   (           )
//
//------------------------------------------------

    /**
     * Enlarges the size of <code>entry</code> array to
     * eliminate the overflow condition. The new array
     * is 50 percent larger than the current array.
     */
    private void enlarge( )
    {
        //create a new array whose size is 150% of
        //the current array
        int newLength = (int) (1.5 * entry.length);
        Person[] temp = new Person[newLength];
    
        //now copy the data to the new array
        for (int i = 0; i < entry.length; i++) {
            temp[i] = entry[i];
        }
    
        //finally set the variable entry to point to the new array
        entry = temp;
    
        System.out.println("Inside the method enlarge");            //TEMP
        System.out.println("Size of a new array: " + entry.length); //TEMP
    }
}