Managing Records with Nested Dictionaries and Lists in C#

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

Introducing Methods to Implement

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

  • AddProject(string employeeId, string 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(string employeeId, string projectName, string 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(string employeeId, string 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.
  • _Traverse(string employeeId, string projectName) - This private method helps to locate the nested structure for a given employee and project. If the path is valid and exists, it returns the target object. If it is invalid or does not exist, it returns null.
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.

C#
using System;
using System.Collections.Generic;

public class EmployeeRecords
{
    private Dictionary<string, Dictionary<string, List<string>>> records;

    public EmployeeRecords()
    {
        records = new Dictionary<string, Dictionary<string, List<string>>>();
    }
}

public class Program
{
    public static void Main()
    {
        var records = new EmployeeRecords();
        Console.WriteLine(records);
    }
}

In this initial setup, we define the EmployeeRecords class and create an instance variable records that is a nested dictionary. This structure will be used to store employee records, where each key is an employee ID and each value is another dictionary holding projects and their respective tasks.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal