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.
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 returnsfalse. Otherwise, it adds the project and returnstrue.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 returnsfalse. If the task is added successfully, it returnstrue.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 returnsnull. Otherwise, it returns the list of tasks.
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.
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.
Now, we will implement the addProject, addTask, and getTasks methods to manage the nested data structure directly within these methods.
Here is the behavior for each method:
addProjectinitializes a new project for an employee if it does not exist.addTaskadds a task to an existing project, returningfalseif the project does not exist.getTasksretrieves tasks for a specified project, returningnullif the project does not exist.
