Java Program for Employee Details using Class and Object

In this article, we will talk about the Java program for employee details using class and object.

An Employee is a person or also be referred to as an entity that consists of various attributes such as – emp_id, emp_name, emp_salary, emp_department, emp_email, emp_address, and many more. We use the getter (to receive Employee details) and setter (to set Employee details) method in this program.

In the main class, we will create an object of the EmployeeDetails class and with the help of an object, we will access the attributes of the EmployeeDetails class.

Program:

class EmployeeDetails 
{

   //Creating properties of Employee class

   int emp_id, emp_salary;

   String emp_name, emp_address, emp_department, emp_email;

   //Getter and setters for getting and setting properties

   public int getEmp_id() 
   {

      return emp_id;
   }

   public void setEmp_id(int emp_id)
   {

      this.emp_id = emp_id;
   }

   public int getSalary() 
   {

     return emp_salary;

    }

    public void setSalary(int emp_salary)
    {

       this.emp_salary = emp_salary;

    }

    public String getName() 
    {

       return emp_name;

    }

    public void setName(String emp_name)
    {

       this.emp_name = emp_name;
    }

   public String getAddress() 
   {

      return emp_address;

   }

   
   public void setAddress(String emp_address)
   {

      this.emp_address = emp_address;

   }

    public String getDepartment() 
    {

      return emp_department;

    }

    public void setDepartment(String emp_department)
    {

       this.emp_department = emp_department;
    }

    public String getEmail() 
    {

       return emp_email;
     }

     public void setEmail(String emp_email) 
     {

       this.emp_email = emp_email;

     }

   //Overriding toString() method

    @Override

     public String toString() 
     {

        return " Employee details{ Id = "+ emp_id + "\n Salary = " + emp_salary + "\n Name = " + emp_name + "\n Address = " + emp_address + "\n Department = " + emp_department + "\n Email = " + emp_email + "}";
     }

}

//Creating the main class

public class Employee
{

  // main method

   public static void main(String args[]) 
   {

     //Creating object of EmployeeDetails class

      EmployeeDetails emp = new EmployeeDetails();

      //Setting values to the properties

      emp.setEmp_id(67);

      emp.setName("Shruti Agarwal");

      emp.setDepartment("IT");

      emp.setSalary(30000);

      emp.setAddress("New Delhi");

      emp.setEmail("agarwalshruti234@gmail.com");

      //Showing Employee details

      System.out.println(emp);


     //Getting salary using getter

     int sal = emp.getSalary();

     int increment = 0;

     //Incrementing salary based on condition

     if ((sal >=10000) && (sal <=50000))

     {

        //incrementing salary 2%

        increment += (sal * 2)/100;

        sal = sal+increment;

        emp.setSalary(sal);

        System.out.println("\n Salary is incremented \n");

        System.out.println(emp);

    }
    else if ((sal >=50000) && (sal <=70000))
    {

       //incrementing salary 5%

         increment += (sal * 5)/100;

         sal = sal+increment;

         emp.setSalary(sal);

         System.out.println("\n Salary is incremented \n");

         System.out.println(emp);

      }
      else 
      {

          System.out.println("\n Salary is not incremented \n");

          System.out.println(emp);

       }

   }

}

Output:

Employee details {emp_id = 67

salary = 30000

name = Shruti Agarwal

address = Delhi

department = Sales

email = agarwalshruti234@gmail.com}

Salary is incremented

Employee details {emp_id = 67

salary = 30600

name = Shruti Agarwal

address = Delhi

department = Sales

email = agarwalshruti234@gmail.com}

Explanation:

In this program, we defined two classes named ‘EmployeeDetails’ and ‘Employee’. In the class ‘EmployeeDetails’, we define some variables which represent the employee’s properties. These variables are as follows:

emp_id, emp_salary are of int type whereas

emp_address, emp_department, emp_email, emp_name are of String type..

In this EmployeeDetails class, we created getter and setter functions for each property of the employees to fetch and assign the values. In the ‘Employee’ class, we created an object of the EmployeeDetails class. Now, we assign the values using the setter method. And call these setter methods using objects. We also created one function namely: ‘toString()’. This function invokes when we print the class’s object. In the next line, we print the object of EmployeeDetails class so it prints the string which is return by the ‘toString()’ method.

We declare some more variables named ‘sal’ and ‘increment’. We get the emp_salary value using its getter method and initialize it to the sal variable. And, initialize increment as 0. Now, we start incrementing the salary of the employees based on some conditions. And, after incrementing salary, we again print the employee’s details using the toString() method.

So, that’s all about the Java program for employee details using class and object with detailed explanation.

Leave a Comment

Your email address will not be published. Required fields are marked *