Lesson 4
Managing Employee Records with Kotlin MutableMap and MutableList
Introducing Methods to Implement

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

  • addProject(employeeId: String, projectName: String): Boolean — 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: String, projectName: String, task: String): Boolean — 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: String, projectName: String): List<String>? — 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

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.

Kotlin
1class EmployeeRecords { 2 private val records: MutableMap<String, MutableMap<String, MutableList<String>>> = mutableMapOf() 3} 4 5fun main() { 6 val records = EmployeeRecords() 7}

In this initial setup, we define the EmployeeRecords class and create a property records that is a MutableMap. Each key is an employee ID with a corresponding value being another map that holds projects and tasks.

Step 2: Implementing `addProject` Method

Next, we'll implement the addProject method to add projects to an employee's record.

Kotlin
1class EmployeeRecords { 2 /* Other methods and properties omitted for brevity */ 3 4 fun addProject(employeeId: String, projectName: String): Boolean { 5 val employeeProjects = records.getOrPut(employeeId) { mutableMapOf() } 6 if (projectName in employeeProjects) { 7 return false 8 } else { 9 employeeProjects[projectName] = mutableListOf() 10 return true 11 } 12 } 13} 14 15fun main() { 16 val records = EmployeeRecords() 17 println(records.addProject("E123", "ProjectA")) // Returns true 18 println(records.addProject("E123", "ProjectA")) // Returns false 19}

Here, the addProject method uses getOrPut to check if the given employeeId exists in the records map. This method creates an empty map for an employee if none exists. It checks if the projectName already exists, returning false if it does. Otherwise, it creates a new empty list for tasks and returns true.

Step 3: Implementing `addTask` Method

Now, we will implement the addTask method, which depends on the existence of the project.

Kotlin
1class EmployeeRecords { 2 /* Other methods and properties omitted for brevity */ 3 4 fun addTask(employeeId: String, projectName: String, task: String): Boolean { 5 val employeeProjects = records[employeeId] 6 val tasks = employeeProjects?.get(projectName) 7 return if (tasks != null) { 8 tasks.add(task) 9 true 10 } else { 11 false 12 } 13 } 14} 15 16fun main() { 17 val records = EmployeeRecords() 18 records.addProject("E123", "ProjectA") 19 println(records.addTask("E123", "ProjectA", "Task1")) // Returns true 20 println(records.addTask("E123", "NonExistentProject", "Task2")) // Returns false 21}

The addTask method first tries to obtain the list of tasks for the specified project. If the employee or project doesn't exist, the method returns false. Otherwise, it appends the task to the list and returns true.

Step 4: Implementing `getTasks` Method

Lastly, let's implement the getTasks method to retrieve tasks from a specified employee's project.

Kotlin
1class EmployeeRecords { 2 /* Other methods and properties omitted for brevity */ 3 4 fun getTasks(employeeId: String, projectName: String): List<String>? { 5 return records[employeeId]?.get(projectName) 6 } 7} 8 9fun main() { 10 val records = EmployeeRecords() 11 records.addProject("E123", "ProjectA") 12 records.addTask("E123", "ProjectA", "Task1") 13 println(records.getTasks("E123", "ProjectA")) // Returns ["Task1"] 14 println(records.getTasks("E123", "NonExistentProject")) // Returns null 15}

The getTasks method attempts to retrieve the list of tasks for the specified project. If it doesn't exist, null is returned. Otherwise, the list of tasks is returned.

Lesson Summary

In this lesson, we successfully implemented the EmployeeRecords class for managing projects and tasks for employees using Kotlin's MutableMap and MutableList. We covered methods for adding projects, adding tasks to those projects, and retrieving tasks from those projects.

Understanding how to work with Kotlin's collections and effectively handle nullable types will enhance your ability to manage complex data hierarchies. Continue practicing with these concepts to further build your skills.

Keep coding and exploring new challenges!

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