/**************************************************************************
 * All classes together
 *************************************************************************/
/**************************************************************************
 * The Employee class
 * Added 3rd constructor (a copy constructor) 
 * Added a clone() method 
 *************************************************************************/
class Employee
{
    private static String companyName  = "XYZ inc.";     // static fields
    private static int    employeeCount;
    
    private String position  = "";                       // instance fields
    private String lastname  = "";
    private String firstname = "";
    private String sex       = "";
    private int    age;

//-- The constructors ---------------------------------------------------------------

    Employee(String title)                                                 //1st constructor   
    {                                                 
        this.position = title;
        employeeCount++;
    }

    Employee(String title, String last, String first, String x, int old)   //2nd constructor   
    {                                                 
        this(title);                            					//call the 1st constructor

        this.lastname  = last;
        this.firstname = first;  
        this.sex       = x;
        this.age       = old;
    }

    Employee(Employee e)                                					//3rd constructor (copy constructor)    
    {
        this(e.position, e.lastname, e.firstname, e.sex, e.age);    //Call the 2nd constructor                                                        

//or    this.position  = e.position;
//      this.lastname  = e.lastname;
//      this.firstname = e.firstname;
//      this.sex       = e.sex;
//      this.age       = e.age;
    }


//-- A clone method ------------------------------------------------------------------

    public Employee clone()                     //Another way to clone an Employee
    {
        String pos   = this.position;           //copy the fields of this object  
        String last  = this.lastname  ;         //to local variables
        String first = this.firstname;           
        String sex   = this.sex;                 
        int age      = this.age;
        
        Employee e2 = new Employee(pos, last, first, sex, age);

//or    Employee e2 = new Employee(this.position, this.lastname, this.firstname, this.sex, this.age);

//or    Employee e2 = new Employee(this);       //will call the copy constructor 

        return(e2);
    }

    public void setPosition(String position)
    {
        this.position = position;
    }

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



/**************************************************************************
 * The Employee Tester class
 * Adding an array of employees
 * Using the copy constructor to copy employee 
 * Using the clone() method to copy employee  
 *************************************************************************/
public class EmployeeTest
{
    public static void main (String[ ] args)
    {        
        Employee[] emp = new Employee[5];

        emp[0] = new Employee("Prof","Sultan","Sam","M",44);  	//instantiate
        emp[1] = new Employee("Dir","Burns","Barbara","F",48);  	//objects
        emp[2] = new Employee("Mgr","Smith","Mary","F",35);  	

        System.out.println("The original employees: \n");

        System.out.println("Emp-0: " + emp[0]);
        System.out.println("Emp-1: " + emp[1]);
        System.out.println("Emp-2: " + emp[2]);
        System.out.println();
       

//----- Using a copy constructor -----------------------------------------------------------

        System.out.println("CREATING one object from another, using a COPY CONSTRUCTOR: \n");

        emp[3] = new Employee(emp[1]);             			//creating an object from another
        
        System.out.println("Emp-1: " + emp[1]);				//emp1 and emp3 should be identical
        System.out.println("Emp-3: " + emp[3]);                   
        System.out.println();

        System.out.println("Changing the title of emp3: \n");

        emp[3].setPosition("Pres");

        System.out.println("Emp-1: " + emp[1]);				
        System.out.println("Emp-3: " + emp[3]);                   
        System.out.println();
       

//----- Using the clone method ------------------------------------------------------------

        System.out.println();
        System.out.println("CLONNING one employee from another, using a CLONE METHOD: \n");

        emp[4] = emp[2].clone();                   			//cloning one employee from another

        System.out.println("Emp-2: " + emp[2]);        		//emp2 and emp4 should be identical
        System.out.println("Emp-4: " + emp[4]);                   
        System.out.println();

        System.out.println("Changing the title of emp4: \n");

        emp[4].setPosition("VP");

        System.out.println("Emp-2: " + emp[2]);
        System.out.println("Emp-4: " + emp[4]);                   
        System.out.println();

        System.out.println("Reprinting all employees: \n");

        int i = 0;
        for (Employee e : emp)
            System.out.println("Emp-" + i++ + " " + e);                   
   }
}