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.
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.
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.setMailId("[email protected]");
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