Managing User Data: Filtering and Aggregation in Java

Managing User Data: Filtering and Aggregation in Java

Welcome to today's lesson on applying data filtering and aggregation in a real-world scenario using a user management system in Java. We'll start by building a foundational structure to handle basic user operations. Then, we'll expand it by introducing more advanced functionalities that allow for 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, boolean 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, Integer age, String country, Boolean 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 Java implementation of our starter task is shown below:

Java
import java.util.HashMap;
import java.util.Map;

public class UserManager {
    private Map<String, UserProfile> users;

    public UserManager() {
        users = new HashMap<>();
    }

    public boolean addUser(String userId, int age, String country, boolean subscribed) {
        if (users.containsKey(userId)) {
            return false;
        }
        users.put(userId, new UserProfile(age, country, subscribed));
        return true;
    }

    public UserProfile getUser(String userId) {
        return users.get(userId);
    }

    public boolean updateUser(String userId, Integer age, String country, Boolean subscribed) {
        UserProfile profile = users.get(userId);
        if (profile == null) {
            return false;
        }
        if (age != null) {
            profile.setAge(age);
        }
        if (country != null) {
            profile.setCountry(country);
        }
        if (subscribed != null) {
            profile.setSubscribed(subscribed);
        }
        return true;
    }
}

class UserProfile {
    private int age;
    private String country;
    private boolean subscribed;

    public UserProfile(int age, String country, boolean subscribed) {
        this.age = age;
        this.country = country;
        this.subscribed = subscribed;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public boolean isSubscribed() {
        return subscribed;
    }

    public void setSubscribed(boolean subscribed) {
        this.subscribed = subscribed;
    }

    @Override
    public String toString() {
        return "UserProfile{" + "age=" + age + ", country='" + country + '\'' + ", subscribed=" + subscribed + '}';
    }
}

class Program {
    public static void main(String[] args) {
        UserManager um = new UserManager();
        System.out.println(um.addUser("u1", 25, "USA", true));  // true, Adds new user
        System.out.println(um.addUser("u2", 30, "Canada", false));  // true, Adds another user
        System.out.println(um.addUser("u1", 22, "Mexico", true));  // false, User exists
        System.out.println(um.getUser("u1"));  // UserProfile with specified attributes
        System.out.println(um.updateUser("u1", 26, null, null));  // true, Only updates age
        System.out.println(um.updateUser("u3", 19, "UK", false));  // false, User does not exist
        System.out.println(um.updateUser("u1", null, "UK", true)); // true, Updates country and subscription status
    }
}

The updateUser method allows for partial updates of a user's profile. Nullable parameters (age, country, and subscribed) enable selective updates, meaning only the provided non-null values are used to modify the existing user data. This design avoids unnecessary data updates and improves efficiency, especially in scenarios where only a subset of the user's data changes frequently.

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