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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | 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 = 50000 name =ramu@dayina address = MADRAS department = CSE email = d_ramu2002@yahoo.com} Salary is incremented Employee details {emp_id = 67 salary = 50600 name = ramu@dayina address = madras department = cse email = d_ramu2002@yahoo.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. |
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి