class UserManager {
constructor() {
this.users = {}; // Initialize an empty object to store user data
}
// Method to add a new user
addUser(userId, age, country, subscribed) {
if (this.users[userId]) {
return false; // Return false if userId already exists
}
this.users[userId] = { age, country, subscribed }; // Add user to the users object
return true; // Return true to indicate successful addition
}
// Method to retrieve a user's profile
getUser(userId) {
return this.users[userId] || null; // Return the user profile or null if the user does not exist
}
// Method to update a user's profile
updateUser(userId, age, country, subscribed) {
if (!this.users[userId]) {
return false; // Return false if the user does not exist
}
if (age !== null) {
this.users[userId].age = age; // Update age if provided
}
if (country !== null) {
this.users[userId].country = country; // Update country if provided
}
if (subscribed !== null) {
this.users[userId].subscribed = subscribed; // Update subscription status if provided
}
return true; // Return true to indicate successful update
}
// Method to filter users based on criteria
filterUsers(minAge, maxAge, country, subscribed) {
const filteredUsers = [];
for (const [userId, profile] of Object.entries(this.users)) {
// Check minimum age criterion
if (minAge !== null && profile.age < minAge) {
continue;
}
// Check maximum age criterion
if (maxAge !== null && profile.age > maxAge) {
continue;
}
// Check country criterion
if (country !== null && profile.country !== country) {
continue;
}
// Check subscription status criterion
if (subscribed !== null && profile.subscribed !== subscribed) {
continue;
}
// Add userId to filteredUsers if all criteria are met
filteredUsers.push(userId);
}
return filteredUsers; // Return the list of filtered user IDs
}
// Method to aggregate statistics from user profiles
aggregateStats() {
const totalUsers = Object.keys(this.users).length; // Get the total number of users
if (totalUsers === 0) { // If no users, return zeroed statistics
return { totalUsers: 0, averageAge: 0, subscribedRatio: 0.00 };
}
// Calculate total age by summing ages of all users
const totalAge = Object.values(this.users).reduce((sum, profile) => sum + profile.age, 0);
// Count number of subscribed users
const subscribedUsers = Object.values(this.users).filter(profile => profile.subscribed).length;
// Calculate average age (rounded down)
const averageAge = Math.floor(totalAge / totalUsers);
// Calculate subscribed ratio (to two decimals)
const subscribedRatio = (subscribedUsers / totalUsers).toFixed(2);
return { totalUsers, averageAge, subscribedRatio: parseFloat(subscribedRatio) }; // Return statistics object
}
}
// Example usage
const um = new UserManager();
um.addUser("u1", 25, "USA", true);
um.addUser("u2", 30, "Canada", false);
um.addUser("u3", 22, "USA", true);
console.log(um.filterUsers(20, 30, "USA", true)); // ["u1", "u3"]
console.log(um.filterUsers(null, 28, null, null)); // ["u1", "u3"]
console.log(um.filterUsers(null, null, "Canada", false)); // ["u2"]
console.log(um.aggregateStats()); // { totalUsers: 3, averageAge: 25, subscribedRatio: 0.67 }