Intermediate
30 min read
Model an Employee Directory (Maps)
By the end of this lesson, you'll be able to:
- Write the Employee wrapper class from the design sketch
- Write an EmployeeDirectory class holding a Map<String, Employee>
- Add and look up employees by employee ID
Prerequisites: "Design the Data Structures"
The Employee wrapper class
public class Employee {
private String employeeId;
private String name;
private String department;
public Employee(String employeeId, String name, String department) {
this.employeeId = employeeId;
this.name = name;
this.department = department;
}
public String getEmployeeId() { return employeeId; }
public String getName() { return name; }
public String getDepartment() { return department; }
}
public class Employee {— this is Module 16's wrapper class pattern combined with Module 10's encapsulation.private String employeeId;—privatefields, set once through the constructor.private String name;— another protected field.private String department;— another.- (blank line)
public Employee(String employeeId, String name, String department) {— the constructor.this.employeeId = employeeId;— sets the id.this.name = name;— sets the name.this.department = department;— sets the department.}— closes the constructor.- (blank line)
public String getEmployeeId() { return employeeId; }— a controlled getter.public String getName() { return name; }— another.public String getDepartment() { return department; }— another — all three fields exposed through controlled getters.}— closes the class.
EmployeeDirectory backed by a Map
public class EmployeeDirectory {
private Map<String, Employee> employeesById = new Map<String, Employee>();
public void addEmployee(Employee emp) {
employeesById.put(emp.getEmployeeId(), emp);
}
public Employee getEmployee(String employeeId) {
return employeesById.get(employeeId);
}
public Integer getEmployeeCount() {
return employeesById.size();
}
}
public class EmployeeDirectory {— the class.private Map<String, Employee> employeesById = new Map<String, Employee>();— this is exactly Module 16'sMap<Id, sObject>pattern, applied to a customEmployeetype instead of a real Salesforce record.- (blank line)
public void addEmployee(Employee emp) {— adds an employee.employeesById.put(emp.getEmployeeId(), emp);— oneput()per employee, keyed by their id.}— closes the method.- (blank line)
public Employee getEmployee(String employeeId) {— looks one up.return employeesById.get(employeeId);— instant, regardless of how many employees the directory holds.}— closes the method.- (blank line)
public Integer getEmployeeCount() {— reports the total.return employeesById.size();— returns it.}— closes the method.}— closes the class.
Putting it together
EmployeeDirectory directory = new EmployeeDirectory();
directory.addEmployee(new Employee('E001', 'Amara Nkosi', 'Engineering'));
directory.addEmployee(new Employee('E002', 'Ben Dlamini', 'Sales'));
Employee found = directory.getEmployee('E001');
System.debug(found.getName()); // "Amara Nkosi"
System.debug(directory.getEmployeeCount()); // 2
EmployeeDirectory directory = new EmployeeDirectory();— creates the directory.- (blank line)
directory.addEmployee(new Employee('E001', 'Amara Nkosi', 'Engineering'));— adds the first employee.directory.addEmployee(new Employee('E002', 'Ben Dlamini', 'Sales'));— adds a second.- (blank line)
Employee found = directory.getEmployee('E001');— no looping required to findE001— theMaphandles the lookup directly.System.debug(found.getName());— logs"Amara Nkosi".System.debug(directory.getEmployeeCount());— logs2, exactly the performance benefit highlighted back in Module 16's "Maps" lesson.
Exercise
Add a removeEmployee(String employeeId) method to EmployeeDirectory that removes an employee from the map.
Show hint
employeesById.remove(employeeId);
Model an Employee Directory (Maps) Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
This lesson turns the sketch from Lesson 1 into real classes — an Employee wrapper class, and an EmployeeDirectory backed by a Map for instant lookups by ID.