/***********************************************************************************************
 * All the classes together
 ***********************************
 * For polymorhism to work:
 *   Define a method in the superclass.  Example. getWeekPay( )  
 *   Override that method in both subclasses.  
 *   Create objects from those 2 subclasses, and assign them to variables of the superclass
 *   Call the method on those objects, Java will automatically call the correct getWeekPay( ) 
***********************************************************************************************/
/********************************************************************************
* Superclass Employee
*********************************************************************************/
class Employee
{
    private static String companyName  = "XYZ inc.";     //class fields
    private static int    employeeCount;
    
    private String position;                             //instance fields
    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
                                                        //and pass it title
        lastname  = last;
        firstname = first;  
        sex       = x;
        age       = old;
    }

    String getFirstname( )        
    {
        return (firstname);
    }
    String getLastname( )        
    {
        return (lastname);
    }
    String getGender( )        
    {
        return (sex);
    }

    String getFullname ()                               //instance method
    {
        String salutation = (sex.equals("M")) ? "Mr." : "Ms.";

        String fullname   = salutation +" "+ firstname +" "+ lastname;
        return (fullname);
    }

    int getWeekPay( )                                   //empty method
    {
        return(0);                                      //unkown weekly pay  
    }

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

/********************************************************************************
* Subclass FullTimer
*********************************************************************************/
class FullTimer extends Employee                        //extends Employee superclass
{
    private static boolean healthPlan = true;           //add class variables
    
    private int salary;                                 //add instance variables
    private int vacationDays;

    FullTimer(String title, String last, String first, String x, int old,
              int sal, int vac)   
    {
        super(title, last, first, x, old);              //call superclass constructor
                                                        //pass it title,last,first,sex,age
        salary       = sal;
        vacationDays = vac;  
    }

    int getWeekPay( )                                       //override getWeekPay() 
    {
        return(salary / 52);                                //compute weekly pay
    } 

    public String toString ( )                              //override the toString()
    {
        String data = super.toString()  + "\n"   +          //call the superclass toString()
                      "Salary: "        + salary + "\t " +
                      "Vacation Days: " + vacationDays + "\n";
        return (data);
    }
}

/********************************************************************************
* Subclass PartTimer
*********************************************************************************/
class PartTimer extends Employee                        //extends Employee superclass
{
    private static String  companyName = "Helper Co.";  //override the companyName
                                                        //from the Employee class

    private static boolean healthPlan = false;          //add class variables      
    
    private int numHours;                               //add instance variables
    private int hourlyRate;

    PartTimer(String title, String last, String first, String x, int old,
              int hours, int rate)   
    {
        super(title, last, first, x, old);              //call superclass constructor
                                                        //pass it title,last,first,sex,age
        numHours   = hours;
        hourlyRate = rate;  
    }

    String getFullname ( )                              //override the getFullname()
    {
        String sex = getGender();
        String salutation = (sex.equals("M")) ? "Mr." : "Ms.";

        String fullname  = salutation +" "+ getFirstname() +" "+ getLastname();
               fullname += "\t Part timer-Employee of "    + companyName;

        return (fullname);
    }

    int getWeekPay( )                                       //override getWeekPay()                
    {
        return (numHours * hourlyRate);                     //compute weekly pay
    }

    public String toString ( )                              //override the toString()
    {
        String data = super.toString()  + "\n" +            //call the superclass toString()
                      "Number Hours: " + numHours     + "\t" +
                      "Hourly Rate: "  + hourlyRate   + "\t" +
                      " Income: "      + getWeekPay() + "\n";
        return (data);
    }
}

/********************************************************************************
* The tester class
*********************************************************************************/
public class xEmployeeTest
{
    /**
     * main method to test the classes
     */

    public static void main (String[ ] args)
    {        
        /**
         * All employees will be created using variables type Employee,
         * but the objects created will be either FullTimer or PartTimer
         */ 

        Employee[ ] emp = new Employee[10];     //Create an array of Employees

        emp[0] = new FullTimer("Dir","Sultan","Sam","M",48,78000,12);   //assign a FullTimer   
        emp[1] = new FullTimer("Mgr","Smith","Mary","F",35,62400,12);   //to an Employee reference 
        emp[2] = new FullTimer("Sec","Stevens","Bob","M",28,44200,10);

        emp[3] = new PartTimer("NE","Williams","Bill","M",45,30,25);  
        emp[4] = new PartTimer("NE","Johnson","Joan","F",32,25,22);  
 
        System.out.println(emp[0].getFullname());               //Even though all employees  
        System.out.println(emp[0].getWeekPay());                //created are members of
                                                                //the Employee class,
        System.out.println(emp[1].getFullname());               //   
        System.out.println(emp[1].getWeekPay());                //when calling 
                                                                //getFullName() or getWeekPay() 
        System.out.println(emp[2].getFullname());               //Java will NOT call 
        System.out.println(emp[2].getWeekPay());                //the Employee class                         
                                                                //getFullName() or getWeekPay()
        System.out.println(emp[3].getFullname());               //   
        System.out.println(emp[3].getWeekPay());                //but rather will choose the    
                                                                //correct subclass method 
        System.out.println(emp[4].getFullname());               //based on the subclass 
        System.out.println(emp[4].getWeekPay());                //object that's being referenced         

        System.out.println("\n ---Better yet, Using a Loop ---");

        for (int i = 0; i < emp.length; i++)
        {
            if (emp[i] != null)         //check for null slots
            {
                System.out.println(emp[i].getFullname());
                System.out.println(emp[i].getWeekPay());
            }
        }
    }
}