Lesson 3
Managing Employee Project Records with Nested Data Structures
Introduction

Welcome! Today, we are going to explore an engaging task that involves managing employee records within a company. Specifically, we will work with nested objects and arrays to add projects and tasks for employees and retrieve those tasks as needed. This exercise will help you understand how to manipulate nested data structures efficiently.

Introducing Methods to Implement

Let's start by discussing the methods we will implement in our EmployeeRecords class.

  • addProject(employeeId, projectName) - This method adds a new project to an employee's list of projects. If the project already exists for that employee, the method returns false. Otherwise, it adds the project and returns true.
  • addTask(employeeId, projectName, task) - This method adds a new task to a specified project for an employee. If the project does not exist for that employee, the method returns false. If the task is added successfully, it returns true.
  • getTasks(employeeId, projectName) - This method retrieves all tasks for a specified project of an employee. If the project does not exist for that employee, the method returns null. Otherwise, it returns the list of tasks.
Step 1: Basic Class Structure

Now, let's build our EmployeeRecords class step by step, ensuring we understand each component clearly.

We'll start with the basic structure of the class and initialize our data storage.

JavaScript
1class EmployeeRecords { 2 constructor() { 3 this.records = {}; 4 } 5} 6 7// Instantiate the class to ensure it works. 8const records = new EmployeeRecords(); 9console.log(records);

In this initial setup, we define the EmployeeRecords class and create an instance variable records that is an empty object. This object will be used to store employee records, where each key is an employee ID, and each value is another object holding projects.

Step 2: Implementing `addProject`, `addTask`, and `getTasks` Methods

Now, we will implement the addProject, addTask, and getTasks methods to manage the nested data structure directly within these methods.

JavaScript
1class EmployeeRecords { 2 constructor() { 3 this.records = {}; 4 } 5 6 addProject(employeeId, projectName) { 7 if (!this.records[employeeId]) { 8 this.records[employeeId] = {}; 9 } 10 if (this.records[employeeId][projectName]) { 11 return false; 12 } else { 13 this.records[employeeId][projectName] = []; 14 return true; 15 } 16 } 17 18 addTask(employeeId, projectName, task) { 19 if (!this.records[employeeId] || !this.records[employeeId][projectName]) { 20 return false; 21 } 22 this.records[employeeId][projectName].push(task); 23 return true; 24 } 25 26 getTasks(employeeId, projectName) { 27 return this.records[employeeId] ? this.records[employeeId][projectName] || null : null; 28 } 29} 30 31// Example usage and testing 32const records = new EmployeeRecords(); 33records.addProject("E123", "ProjectA"); 34console.log(records.addTask("E123", "ProjectA", "Task1")); // Returns true 35console.log(records.addTask("E123", "NonExistentProject", "Task3")); // Returns false 36console.log(records.getTasks("E123", "ProjectA")); // Returns ["Task1"] 37console.log(records.getTasks("E123", "NonExistentProject")); // Returns null

Here is the behavior for each method:

  • addProject initializes a new project for an employee if it does not exist.
  • addTask adds a task to an existing project, returning false if the project does not exist.
  • getTasks retrieves tasks for a specified project, returning null if the project does not exist.
Lesson Summary

In this lesson, we successfully implemented the EmployeeRecords class for managing projects and tasks for employees using nested objects and arrays. We covered methods for adding projects, adding tasks to those projects, and retrieving tasks from those projects.

Understanding how to work with nested data structures allows you to efficiently manage complex data hierarchies, which improves your programming skills and problem-solving abilities.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.