Employee.java
public class Employee {
private String firstName, lastName;
private String designation;
private String employeeCode;
public Employee(String employeeCode, String firstName, String lastName, String designation){
this.employeeCode=employeeCode;
this.firstName=firstName;
this.lastName=lastName;
this.designation=designation;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getEmployeeCode() {
return employeeCode;
}
public void setEmployeeCode(String employeeCode) {
this.employeeCode = employeeCode;
}
public String toString(){
return "[EmployeeCode : "+employeeCode+", LastName : "+lastName+", FirstName : "+firstName+", Designation : "+designation+"]";
}
}
EmployeeDAO.java
import java.util.HashMap;
import java.util.Map;
public class EmployeeDAO {
protected Map employeeMap;
public EmployeeDAO(){
employeeMap = new HashMap();
}
public Employee getEmployee(String employeeCode){
return employeeMap.get(employeeCode);
}
public void addEmployee(String employeeCode, String firstName, String lastName, String designation){
employeeMap.put(employeeCode, new Employee(employeeCode,firstName,lastName,designation));
}
public void updateEmployee(Employee employee){
employeeMap.put(employee.getEmployeeCode(), employee);
}
}
DAOPatternDemo.java
public class DAOPatternDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//update database
EmployeeDAO dao = new EmployeeDAO();
dao.addEmployee("1", "Tina", "Roz", "PM");
dao.addEmployee("2", "Mina", "Koze", "Lead");
dao.addEmployee("3", "Ina", "Arase", "Developer");
dao.addEmployee("4", "Hina", "Mase", "Tester");
//get employee with code 4
System.out.println("EmployeeCode :4 "+dao.getEmployee("4"));
//update Designation of Employee3
Employee employee3 = dao.getEmployee("3");
employee3.setDesignation("Sr. Developer");
dao.updateEmployee(employee3);
System.out.println("After update Employee : 3 "+ dao.getEmployee("3"));
}
}
Output
