Managing User Data: Filtering and Aggregation in C#

Managing User Data: Filtering and Aggregation in C#

Welcome to today's lesson on applying data filtering and aggregation in a real-world scenario using a user management system. We'll start by building a foundational structure that can handle basic user operations. Then, we'll expand it by introducing more advanced functionalities that allow filtering and aggregating user data.

Starter Task Methods

In our starter task, we will implement a class that manages basic operations on a collection of user data, specifically handling adding new users, retrieving user profiles, and updating user profiles.

Here are the starter task methods:

  • addUser(string userId, int age, string country, bool subscribed) - adds a new user with the specified attributes. Returns true if the user was added successfully and false if a user with the same userId already exists.
  • getUser(string userId) - returns the user's profile as an object if the user exists; otherwise, returns null.
  • updateUser(string userId, int? age, string country, bool? subscribed) - updates the user's profile based on non-null parameters. Returns true if the user exists and was updated, false otherwise.

Starter Task Implementation

The C# implementation of our starter task is shown below:

using System;
using System.Collections.Generic;

public class UserManager
{
    private Dictionary<string, UserProfile> users;

    public UserManager()
    {
        users = new Dictionary<string, UserProfile>();
    }

    public bool addUser(string userId, int age, string country, bool subscribed)
    {
        if (users.ContainsKey(userId))
        {
            return false;
        }
        users[userId] = new UserProfile { Age = age, Country = country, Subscribed = subscribed };
        return true;
    }

    public UserProfile getUser(string userId)
    {
        if (users.TryGetValue(userId, out var profile))
        {
            return profile;
        }
        return null;
    }

    public bool updateUser(string userId, int? age, string country, bool? subscribed)
    {
        if (!users.ContainsKey(userId))
        {
            return false;
        }
        if (age.HasValue)
        {
            users[userId].Age = age.Value;
        }
        if (country != null)
        {
            users[userId].Country = country;
        }
        if (subscribed.HasValue)
        {
            users[userId].Subscribed = subscribed.Value;
        }
        return true;
    }
}

public class UserProfile
{
    public int Age { get; set; }
    public string Country { get; set; }
    public bool Subscribed { get; set; }
}

class Program
{
    static void Main()
    {
        var um = new UserManager();
        Console.WriteLine(um.addUser("u1", 25, "USA", true));  // true
        Console.WriteLine(um.addUser("u2", 30, "Canada", false));  // true
        Console.WriteLine(um.addUser("u1", 22, "Mexico", true));  // false
        Console.WriteLine(um.getUser("u1"));  // UserProfile with specified attributes
        Console.WriteLine(um.updateUser("u1", 26, null, null));  // true
        Console.WriteLine(um.updateUser("u3", 19, "UK", false));  // false
    }
}

This implementation covers all our starter methods. Let's move forward and introduce more complex functionalities.

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