/**************************************************************************
 * All classes together
 *************************************************************************/
/**************************************************************************
 * The Employee class
 *************************************************************************/
class Employee 
{
    static String companyName  =  "XYZ inc.";               //static fields
    static int    employeeCount;                            //belong to the class
    
    String position;                                        //instance fields
    String lastname;                                        //belong to each object
    String firstname;
    String sex;
    int    age;

    Employee(String title , String last, String first, String x, int old)     // constructor
    {
        position  = title;
        lastname  = last;
        firstname = first;
        sex       = x;
        age       = old;
        
        employeeCount += 1; 
    }

    static int getEmployeeCount()                                   //static method
    {                                                               //belongs to the class
        return (employeeCount); 
    }
    static void companyInfo()                               
    {                                                       
        System.out.println("Company   "  + companyName);
        System.out.println("Empl count " + employeeCount);
    }

    String  getFullname (String salutation)                           //instance method
    {                                                                 //belongs to each object
        String fullname = salutation +" "+ firstname +" "+ lastname;    
        return (fullname);
    }
    
    //Every object should ("best practice") have a toString( ) method
    //The toString( ) method should return the content of the object in one long string 
    //If you use the object in any string context (e.g. printing), the toString( ) method will 
    //automatically be called.  We will cover this in more detail later

    public String toString()                                        //instance method
    {                                                               //belongs to each object
        String data = "Company     "  + companyName +               //must be public                
                      "\t Position: " + position  +                 
                      "\t Name: "     + firstname +" "+ lastname + 
                      "\t Sex: "      + sex + 
                      "\t Age: "      + age;
        return (data);
    }
}                   

/**************************************************************************
 * The tester class
 *************************************************************************/
public class EmployeeTest
{
    public static void main (String[ ] args)
    { 
        String co  = Employee.companyName;                          //access static field
        int count  = Employee.employeeCount;

        Employee.companyInfo( );                                    //call a static method

        Employee e1 = new Employee("Dir","Sultan","Sam","M",45);    //instantiate/create
        Employee e2 = new Employee("Mgr","Smith","Mary","F",35);    //objects
        Employee e3 = new Employee("Sec","Stevens","Bob","M",28);
        Employee x  = new Employee("???","Person","Mystery","",0);

        String fname = e1.firstname;                                //access instance field
        System.out.println("First Name: " + fname);                 //prints  First Name: Sam

        x.firstname = "Harry";                                      //changing instance fields
        x.lastname  = "Potter";

        System.out.println("After  change: " + x.firstname +" "+ x.lastname );  //prints Harry Potter


        count = Employee.getEmployeeCount( );                       //call static method
        System.out.println("Number of employees: " + count);        //prints  Number Employees:  4

        String name1 = e1.getFullname("Mr.");                       //call instance method 
        System.out.println(name1);                                  //prints:  Mr. Sam Sultan

        System.out.println( e2.getFullname("Ms.") );                //combining 2 previous. lines

        System.out.println( e3.toString( ) );                       //call the toString( ) method

        System.out.println(e3);                                     //when an obj. is used in string context
                                                                    //no need to say  .toString( )


        // I can change the value of employeeCount, thereby destroying the integrity of the field.
        // That may not be what you want 

        System.out.println("Adding to employeeCount");

        Employee.employeeCount += 10;                               //changing class field

        System.out.println("Number of employees: " + Employee.employeeCount);   //14 employees?
    }
}