Employee details program in java as below.
Here two classes are created
- Employee class
- EmployeeMain class
Details of these two classes are as below
java program for employee details using class and object
employee class in java : Employee.java
This class contains various fields of employees like id
, fname
, lname
, address
etc.
Its corresponding getter and setter method are created in the program.
Employee.java is a java bean and its good example of java encapsulation.
fields of Employee object can not be directly accessible outside the class to access this we need getter and setter methods.
Default and parameterized constructors are created to initialize employee objects.
toString() of the Object class is overridden to convert the object to String.
Some fields like hra
, providentFund
and grossSalary
are calculated from basicSalary
.
So user provides only basicSalary
value and other derived values are calculated based on it.
Two different methods are used to initialize values(Parameterised constructor and setter methods).
So the calculation of derived values is done inside the constructor and in the setter method.
Providing addDate
value is optional for a user it the user does not provide this value constructor or setter method does this.
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | import java.math.BigDecimal; import java.sql.Date; import java.sql.Timestamp; import java.io.Serializable; public class Employee implements Serializable{ private long id; private String fname; private String lname; private String address; private String mobileNo; private String mailId; private String city; private String designation; private Date dob; private Date doj; private BigDecimal basicSalary; private BigDecimal hra; //40 % of basic private BigDecimal providentFund;// 12% of basic private BigDecimal grossSalary;//sum of basic+allowance(hra+providentFund) private Timestamp addDate; public Employee() { } public Employee(String fname, String lname, String address, String mobileNo, String mailId, String city, String designation, Date dob, Date doj, BigDecimal basicSalary) { this.fname = fname; this.lname = lname; this.address = address; this.mobileNo = mobileNo; this.mailId = mailId; this.city = city; this.designation = designation; this.dob = dob; this.doj = doj; this.basicSalary = basicSalary; hra = basicSalary.multiply(new BigDecimal(0.12).setScale(2, BigDecimal.ROUND_UP)); providentFund = basicSalary.multiply(new BigDecimal(0.40).setScale(2, BigDecimal.ROUND_UP)); grossSalary = basicSalary.add(hra).add(providentFund).setScale(2, BigDecimal.ROUND_UP); if (addDate == null) { this.addDate = new Timestamp(System.currentTimeMillis()); } else { this.addDate = addDate; } } public Employee(long id, String fname, String lname, String address, String mobileNo, String mailId, String city, String designation, Date dob, Date doj, BigDecimal basicSalary, Timestamp addDate) { this.id = id; this.fname = fname; this.lname = lname; this.address = address; this.mobileNo = mobileNo; this.mailId = mailId; this.city = city; this.designation = designation; this.dob = dob; this.doj = doj; this.basicSalary = basicSalary; hra = basicSalary.multiply(new BigDecimal(0.12).setScale(2, BigDecimal.ROUND_UP)); providentFund = basicSalary.multiply(new BigDecimal(0.40).setScale(2, BigDecimal.ROUND_UP)); grossSalary = basicSalary.add(hra).add(providentFund).setScale(2, BigDecimal.ROUND_UP); if (addDate == null) { this.addDate = new Timestamp(System.currentTimeMillis()); } else { this.addDate = addDate; } } @Override public String toString() { return "Employee{" + "id=" + id + ", fname=" + fname + ", lname=" + lname + ", address=" + address + ", mobileNo=" + mobileNo + ", mailId=" + mailId + ", city=" + city + ", designation=" + designation + ", dob=" + dob + ", doj=" + doj + ", basicSalary=" + basicSalary + ", hra=" + hra + ", providentFund=" + providentFund + ", grossSalary=" + grossSalary + ", addDate=" + addDate + '}'; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getMobileNo() { return mobileNo; } public void setMobileNo(String mobileNo) { this.mobileNo = mobileNo; } public String getMailId() { return mailId; } public void setMailId(String mailId) { this.mailId = mailId; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Timestamp getAddDate() { return addDate; } public void setAddDate(Timestamp addDate) { } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public Date getDob() { return dob; } public void setDob(Date dob) { this.dob = dob; } public Date getDoj() { return doj; } public void setDoj(Date doj) { this.doj = doj; } public BigDecimal getBasicSalary() { return basicSalary; } public void setBasicSalary(BigDecimal basicSalary) { this.basicSalary = basicSalary; //calculating hra and PF setHra(basicSalary.multiply(new BigDecimal("0.40"))); setProvidentFund(basicSalary.multiply(new BigDecimal("0.12"))); setGrossSalary(basicSalary.add(getHra().add(getProvidentFund()))); if (addDate == null) { this.addDate = new Timestamp(System.currentTimeMillis()); } else { this.addDate = addDate; } } public BigDecimal getHra() { return hra; } public void setHra(BigDecimal hra) { this.hra = hra; } public BigDecimal getProvidentFund() { return providentFund; } public void setProvidentFund(BigDecimal providentFund) { this.providentFund = providentFund; } public BigDecimal getGrossSalary() { return grossSalary; } public void setGrossSalary(BigDecimal grossSalary) { this.grossSalary = grossSalary; } } |
EmployeeMain.java
This class contain public static void main()
(java main method) inside main tho objects of Employee, class is created.
The first object is created using parameterized constructor and the object’s value is a printer by overriding the toString() Object class.
The second object is created using the default constructor of employee and values are assigned using setter methods of the class.
To print the value of the object getter method is used. Instead of using the getter method toString()
can also be used.
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 | import java.math.BigDecimal; import java.sql.Date; import java.sql.Timestamp; public class EmployeeMain { public static void main(String[] args) { /*initializing using constructor*/ Employee employee = new Employee(1, "Ram", "Kumar", "Sector 5 bhilai", "345345345", "[email protected]", "Bhilai", "Developer", Date.valueOf("1992-05-07"), Date.valueOf("2018-03-04"), new BigDecimal(450000), new Timestamp(System.currentTimeMillis())); System.out.println("Detail of employee"); //printing employee using to string method of employee System.out.println(employee); //putting value using to getter of employee class Employee employee1 = new Employee(); employee1.setId(2); employee1.setFname("Mohan"); employee1.setLname("Singh"); employee1.setAddress("Bhopal"); employee1.setMobileNo("6523985623"); employee1.setCity("Bhopal"); employee1.setDesignation("DBA"); employee1.setDob(Date.valueOf("1992-03-01")); employee1.setDoj(Date.valueOf("2018-02-01")); employee1.setBasicSalary(new BigDecimal(40000)); //shoing employee1 value using getter method System.out.println("Detail of employee1"); System.out.println("id " + employee1.getId() + " fname " + employee1.getFname() + " lname " + employee1.getLname() + " address " + employee1.getAddress() + " mobileNo " + employee1.getMobileNo() + " mailId " + employee1.getMailId() + " city " + employee1.getCity() + " designation " + employee1.getDesignation() + " dob " + employee1.getDob() + " doj " + employee1.getDoj() + " salary " + employee1.getBasicSalary() + " hra " + employee1.getHra() + " PF " + employee1.getProvidentFund() + " Gross Salary " + employee1.getGrossSalary() + " addDate " + employee1.getAddDate()); } } |
It’s up to use which method he wants to use to set values in the object.
The result of the above program is
If anyone wants to change the value of any field then the appropriate setter method needs to call
like want to update lname of employee object then use
employee.setLname("Verma");
this will assign a new value to lname
.
Hope you understand how to show the Employee details program in java
Read More