// User Data Access Component
class UserDataAccess {
constructor() {
this.users = [];
}
/**
* Adds a new user to the data store.
* @param {Object} user - The user object to be added.
* @param {string} user.id - The unique identifier for the user.
* @param {string} user.name - The name of the user.
* @param {string} user.email - The email of the user.
*/
addUser(user) {
this.users.push(user);
}
/**
* Retrieves a user by their unique identifier.
* @param {string} userId - The unique identifier for the user.
* @returns {Object|null} The user object if found, or null if not found.
*/
getUserById(userId) {
return this.users.find((user) => user.id === userId) || null;
}
/**
* Deletes a user by their unique identifier.
* @param {string} userId - The unique identifier for the user.
* @returns {boolean} True if the user was deleted, or false if not found.
*/
deleteUserById(userId) {
const index = this.users.findIndex((user) => user.id === userId);
if (index !== -1) {
this.users.splice(index, 1);
return true;
}
return false;
}
}
// Example usage
const userDataAccess = new UserDataAccess();
userDataAccess.addUser({ id: '1', name: 'John Doe', email: 'john.doe@example.com' });
console.log(userDataAccess.getUserById('1'));
userDataAccess.deleteUserById('1');
console.log(userDataAccess.getUserById('1'));