/***************************************************************************
 * Test the Employee and Hierarchy classes all together
 **************************************************************************/
/**************************************************************************
 * The Employee class
 * From previous example
 *************************************************************************/
class Employee
{
    private static String companyName  = "XYZ inc.";     // class variables
    private static int    employeeCount;
    
    private String position;                             // instance variables
    private String lastname;
    private String firstname;
    private String sex;
    private int    age;

    Employee(String title)                            // first constructor
    {
        position       = title;
        employeeCount += 1;         
    }

    Employee(String title, String last, String first, String x, int old)   
    {
        this(title);                                 // call first constructor
                                                     // add pass it title
        lastname  = last;
        firstname = first;  
        sex       = x;
        age       = old;
    }

    Employee(Employee obj)                           // copy constructor    
    {
        this(obj.position, obj.lastname, obj.firstname, obj.sex, obj.age);
    }


    void  setLastname(String lastname)         
    {
        this.lastname = lastname;
    }

    public String toString ( )                      
    {
        String data = "Position: " + position + 
                      "\t Name: "  + firstname +  " " + lastname + 
                      "\t Sex: "   + sex + "\t Age: " + age;
        return (data);
    }
}


/*****************************************************************************
 * The Hierarchy class
 * A class that take 2 Employee objects and creates a supervisor/subordinate
 * relationship with them
 ****************************************************************************/
class Hierarchy
{
    //Notice that some of the fields are not primitive by are object types.

    private static String companyName  = "XYZ inc.";    //class field
    
    private String   date;                              //instance field primitive type
    private Employee boss;                              //instance field object type
    private Employee sub;

    /**
     * Constructor 1.  Reference the passed objects
     */

    Hierarchy(Employee boss, Employee sub, String date)
    {
            this.date  = date;
            this.boss  = boss;               //reference the passed object
            this.sub   = sub;               
    }

    /**
     * Constructor 2.  Make a copy of the passed objects
     */

    Hierarchy(Employee boss, Employee sub, String date, String type)
    {
            this.date  = date;
            this.boss  = new Employee(boss);    //create a new object, and
            this.sub   = new Employee(sub);     //assign it value of passed obj
    }


    public String toString()                        
    {
        String data = "Effective date:  "   + date +
                      "\n  Supervisor  -  " + boss +    
                      "\n  Subordinate -  " + sub;  
        return (data);
    }
}


/**************************************************************************
 * Test the Hierarchy class
 *************************************************************************/
public class HierarchyTest
{
    public static void main (String[ ] args)
    {        
        // Instantiate employee objects

        Employee pres = new Employee("Pres","Bush","George","M",54);  //create
        Employee vp   = new Employee("VP","Flanagan","Liz","F",52);   //objects
        Employee dir  = new Employee("Dir","Sultan","Sam","M",49);  
        Employee mgr  = new Employee("Mgr","Smith","Mary","F",35);  
        Employee sec  = new Employee("Sec","Stevens","Bob","M",28);

        System.out.println(pres);                         //call toString( )
        System.out.println(vp);                           //of Employee object
        System.out.println(dir);
        System.out.println(mgr);
        System.out.println(sec);
        System.out.println();

    // Build the org chart hierarchy
    // I forced postion number 2 & 3 to create a copy of the employee passed
    // rather than to simply reference the employee passed. 
    // (see difference between 1st & 2nd constructor in the Hierarchy class)
        
        Hierarchy level1 = new Hierarchy(pres, vp,  "Jan 2005");
        Hierarchy level2 = new Hierarchy(vp,   dir, "Feb 2005", "copy");
        Hierarchy level3 = new Hierarchy(dir,  mgr, "Mar 2005", "copy");
        Hierarchy level4 = new Hierarchy(mgr,  sec, "Apr 2005");
        Hierarchy level5 = new Hierarchy(sec,  null, "");
        
        System.out.println(level1);                          //call toString( )
        System.out.println(level2);                          //of Hierarchy object
        System.out.println(level3);
        System.out.println(level4);
        System.out.println(level5);
        System.out.println();
        
        pres.setLastname("Carlin");                         //change pres lastname
        dir.setLastname("Elliot");                          //change dir lastname

        System.out.println(pres);                           //call toString( )
        System.out.println(dir);                            //of Employee object
        System.out.println();
        
    // You will notice that in the reprint of the Hierarchy 
    // the name of the president was changed from George Bush to George Carlin, 
    // but the name of the director did not change from Sam Sultan.  Why?

        System.out.println(level1);                         //reprint Hierarchy
        System.out.println(level2);
        System.out.println(level3);
        System.out.println(level4);
        System.out.println(level5);
    }
}