w3resource

Java: Create Employee class with years of service calculation

Java OOP: Exercise-9 with Solution

Write a Java program to create a class called "Employee" with a name, salary, and hire date attributes, and a method to calculate years of service.

Sample Solution:

Java Code:

// Employee.java
import java.time.LocalDate;
import java.time.Period;

public class Employee {
  private String name;
  private double salary;
  private LocalDate hireDate;

  public Employee(String name, double salary, LocalDate hireDate) {
    this.name = name;
    this.salary = salary;
    this.hireDate = hireDate;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public double getSalary() {
    return salary;
  }

  public void setSalary(double salary) {
    this.salary = salary;
  }

  public LocalDate getHireDate() {
    return hireDate;
  }

  public void setHireDate(LocalDate hireDate) {
    this.hireDate = hireDate;
  }

  public int getYearsOfService() {
    return Period.between(hireDate, LocalDate.now()).getYears();
  }

  public void printEmployeeDetails() {
    System.out.println("\nName: " + name);
    System.out.println("Salary: " + salary);
    System.out.println("HireDate: " + hireDate);
  }

}

In the above Employee class, there are three private attributes: name, salary, and hireDate, a constructor that initializes these attributes with the values passed as arguments, and getter and setter methods to access and modify these attributes.

There is a method “getYearsOfService()” to calculate years of service between the hire date and the current date, and returns the number of years as an integer value.

//Main.java
import java.time.LocalDate;

public class Main {
  public static void main(String[] args) {
    Employee employee1 = new Employee("Roberta Petrus", 50000, LocalDate.parse("2021-04-01"));
    Employee employee2 = new Employee("Loyd Blair", 70000, LocalDate.parse("2015-04-01"));
    Employee employee3 = new Employee("Magdalena Artemon", 50000, LocalDate.parse("2011-01-15"));
    employee1.printEmployeeDetails();
    System.out.println("Years of Service: " + employee1.getYearsOfService());
    employee2.printEmployeeDetails();
    System.out.println("Years of Service: " + employee2.getYearsOfService());
    employee3.printEmployeeDetails();
    System.out.println("Years of Service: " + employee3.getYearsOfService());
  }
}

In the above main() function, we create three instances of the "Employee" class, and print their name, salary, hire date, and years of service using the appropriate methods.

Sample Output:

Name: Roberta Petrus
Salary: 50000.0
HireDate: 2021-04-01
Years of Service: 2

Name: Loyd Blair
Salary: 70000.0
HireDate: 2015-04-01
Years of Service: 8

Name: Magdalena Artemon
Salary: 50000.0
HireDate: 2011-01-15
Years of Service: 12

Flowchart:

Flowchart: Java  OOP Exercises: Employee class with years of service calculation.
Flowchart: Java  OOP Exercises: Employee class with years of service calculation.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Traffic Light Simulation.
Java OOP Next: Manage student courses using the Student class.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.