Exercise 2

A fictive company has 10 employees and manages the personnel files using java classes. An employee record includes the following information:
  • Id (9 digits stored in a string)
  • Name (string)
  • Date of birth (date)
  • Years of service (integer)
  • Days of vacation earned (integer)
  • Salary (double)
  • Days absent for illness (integer)
    1. Write the class Employee with the needed instance variables and two constructors: the first receives a values for each instance variable in the order given above, and the second receives a value for only the ID, the name and date of birth of the employee.
    2. Add in the class Employee the following methods:
      • display(): displays all information of an employee in the following format exemple:
        =======================
        ID : 123456789
        Name : Fred Flintstone
        Birth : 60/03/12
        Age : 40
        Salary : $120000
        Service: 8 years
        Abcence: 2 days
        Vacation: 13 days
        =======================
      • displayID(): displays the ID and the name of an employee on the same line (ex: 123456789: Fred Flintstone);
      • getSalary(): returns the salary of an employee;
      • getYearService(): returns the number of years in service;
      • getDaysVacation(): returns the number of days of vacation earned;
      • getDaysAbsence(): returns the number of days of absence for illness;
      • setSalary(): changes the salary with a given new value;
      • setYearService(): changes the number of years of service with a given number;
      • setDaysVacation(): changes the number of days of vacation earned with a given number;
      • setDaysAbsence(): changes the number of days of absence for illness with a given number;
      • getAge(): returns the age of the employee;
    3. Write a black box test for class Employee and incapsulate it in the class Employee as the main method.

    Solution

    import java.util.*;
    public class Employee {
         
         
         /* instance variables*/
         
         String id; /*(9 digits stored in a string)*/ 
         String name; 
         Date birthdate; 
         int serviceYears; 
         int vacationDays; 
         double salary;
         int absenceDays;     
    /*
    	Program description.
    */
    	public Employee(String id, String name, Date bDate){
    	  this.id=id; 
    	  this.name=name; 
    	  this.birthdate=bDate; 
    	  this.serviceYears=0; 
    	  this.vacationDays=0; 
    	  this.salary=0.0f;
    	  this.absenceDays=0;
    	}
    	
    	public Employee(String id, String name, Date bDate, 
    	                int servY, int vacD, double salary, 
    	                int absD){
    	  this.id=id; 
    	  this.name=name; 
    	  this.birthdate=bDate; 
    	  this.serviceYears=servY; 
    	  this.vacationDays=vacD; 
    	  this.salary=salary;
    	  this.absenceDays=absD;
    	}
    
    	public void display() {
    	// displays all information of an employee; 
    		System.out.println("=================================");
    		System.out.println("ID      :"+this.id);
    		System.out.println("Name    :"+this.name);
    		System.out.println("Birth   :"+this.birthdate.getYear()+"/"+
    					      (this.birthdate.getMonth()+1)+"/"+
    					       this.birthdate.getDate());
    		System.out.println("Age     :"+this.getAge());
    		System.out.println("Salary  : $"+this.salary);
    		System.out.println("Service :"+this.serviceYears+" years");
    		System.out.println("Absence :"+this.absenceDays+" days");
    		System.out.println("Vacation:"+this.vacationDays+" days");							
    		System.out.println("=================================");
    	}
    
    	public void displayID() {
    	// displays the ID and the name of an employee on the same line; 
    		System.out.println(this.id+" "+this.name);
    	}
    
    	public double getSalary(){
    	// returns the salary of an employee; 
    		return this.salary;
    	}
    
    	public int getYearService(){
    	// returns the number of years in service; 
    		return this.serviceYears;
    	}
    
    	public int getDaysVacation(){
    	// returns the number of days of vacation earned; 
    		return this.vacationDays;
    	}
    
    	public int getDaysAbsence(){
    	// returns the number of days of absence for illness; 
    		return this.absenceDays;
    	}
    
    	public void setSalary(double salary){
    	// changes the salary with a given new value; 
    	       this.salary=salary;
    	}
    
    	public void setYearService(int years){
    	// changes the number of years of service with a given number; 
    		this.serviceYears=years;
    	}
    
    	public void setDaysVacation(int days){
    	// changes the number of days of vacation earned with a given number; 
    		this.vacationDays=days;
    	}
    
    	public void setDaysAbsence(int days){
    	//changes the number of days of absence for illness with a given number; 
    		this.absenceDays=days;
    	}
    
    	public int getAge(){
    	// returns the age of the employee;
    		Date		today;
    		int		todayYear;
    		int		birthYear;
    		int		age;
    		
    		today = new Date();
    		todayYear = today.getYear();
    		birthYear = this.birthdate.getYear();
    		age = todayYear - birthYear;
    		if (! this.hadBirthdayThisYear())
    			age = age - 1;
    
    		return age;
    	}
    
    	private boolean hadBirthdayThisYear() {
    	/*   Return true if I have already had my
    		birthday this year. */
    
    		Date	today;		int	todayMonth;
    		int	myMonth;	int	myDayOfMonth;
    		int	todayDayOfMonth;
    				
    		today = new Date();
    		todayMonth = today.getMonth();
    		myMonth = this.birthdate.getMonth();
    		todayDayOfMonth = today.getDate();
    		myDayOfMonth = this.birthdate.getDate();
    		if (myMonth < todayMonth)
    			return true;
    		else if (myMonth > todayMonth)
    			return false;
    		else if (myDayOfMonth <= todayDayOfMonth)
    			return true;
    		else
    			return false;
    	}
    
    	public static void main(String args[]) {
    	/* Program statements go here. */
    	
    		Employee myEmployee1;
    		Employee myEmployee2;
    	
    		myEmployee1 = new Employee("123456789", 
    					   "Fred Flintstone", 
    					   new Date(60,2,12));
    		myEmployee2 = new Employee("987654321", 
    					   "John Smith", 
    					   new Date(70,4,30),
    					   10,3,40000,5);
    		System.out.println("Testing the Employee class");		
    	        myEmployee1.display();
    	        myEmployee1.displayID();
    	        System.out.println("Should be 123456789 Fred Flintstone");
    	        myEmployee2.display();
    	        myEmployee2.displayID();
    	        System.out.println("Should be 987654321 John Smith");
    	        double mysalary = myEmployee2.getSalary();
    	        System.out.println(mysalary + "<< Should be 40,000");
    		myEmployee1.setSalary(120000);
    	        mysalary = myEmployee1.getSalary();
    	        System.out.println(mysalary + "<< Should be 120,000");
    
    		int number = myEmployee2.getYearService();
    		System.out.println(number + "<< Should be 10");
    	        number = myEmployee2.getDaysVacation();
    		System.out.println(number + "<< Should be 3");
    		number = myEmployee2.getDaysAbsence();
    		System.out.println(number + "<< Should be 5");
    
    
    		myEmployee1.setYearService(8);
    		number = myEmployee1.getYearService();
    		System.out.println(number + "<< Should be 8");
    		myEmployee1.setDaysVacation(13);
    	        number = myEmployee1.getDaysVacation();
    		System.out.println(number + "<< Should be 13");
    		myEmployee1.setDaysAbsence(2);
    		number = myEmployee1.getDaysAbsence();
    		System.out.println(number + "<< Should be 2");
    
    	}
    }