Source: commented-uda.js

/**
 * A class that handles user data operations in the localStorage database.
 */
class UserDataAccess {

  ///////////////////////////////////////////////
  // PRIVATE INSTANCE VARIABLES (start with #)
  ///////////////////////////////////////////////

  /**
   * Dummy data used to populate localStorage if no user data is found.
   * @private
   * @type {Array<Object>}
   */
  #dummyData = [
      {id:1, firstName:"Jane", lastName:"Doe", email:"jdoe@acme.com"},
      {id:2, firstName:"Tony", lastName:"Thompsom", email:"tony@acme.com"},
      {id:3, firstName:"Jesse", lastName:"Jones", email:"jesse@acme.com"}
  ];

  ////////////////////////////////////
  // CONSTRUCTOR
  ////////////////////////////////////

  /**
   * Initializes the UserDataAccess instance.
   * Checks if 'userData' is already in localStorage. If not, initializes it with the dummy data.
   */
  constructor(){
      if(!localStorage.getItem("userData")){
          localStorage.setItem("userData", JSON.stringify(this.#dummyData));
      }
  }

  ////////////////////////////////////
  // PUBLIC METHODS
  ////////////////////////////////////

  /**
   * Retrieves all users from the localStorage.
   * @returns {Array<Object>} List of all users in localStorage.
   */
  getAllUsers(){
      const str = localStorage.getItem("userData");
      const users = JSON.parse(str);
      return users;
  }

  /**
   * Retrieves a user by their unique ID.
   * @param {number} id - The ID of the user to retrieve.
   * @returns {Object|null} The user object if found, or null if not found.
   */
  getUserById(id){
      const str = localStorage.getItem("userData");
      const users = JSON.parse(str);
      const user = users.find((u) => u.id == id);
      return user || null;
  }

  /**
   * Inserts a new user into the localStorage database.
   * @param {Object} newUser - The user object to insert.
   * @param {number} newUser.id - The ID of the new user.
   * @param {string} newUser.firstName - The first name of the new user.
   * @param {string} newUser.lastName - The last name of the new user.
   * @param {string} newUser.email - The email address of the new user.
   */
  insertUser(newUser){
      // We really should validate newUser before inserting it!
      newUser.id = this.#getMaxId() + 1;
      const str = localStorage.getItem("userData");
      const users = JSON.parse(str);
      users.push(newUser);
      localStorage.setItem("userData", JSON.stringify(users));
  }

  /**
   * Updates an existing user's data in the localStorage database.
   * @param {Object} updatedUser - The user object with updated data.
   * @param {number} updatedUser.id - The ID of the user to update.
   * @param {string} updatedUser.firstName - The updated first name of the user.
   * @param {string} updatedUser.lastName - The updated last name of the user.
   * @param {string} updatedUser.email - The updated email address of the user.
   */
  updateUser(updatedUser){
      // We should validate updatedUser before putting it in the database
      const str = localStorage.getItem("userData");
      const users = JSON.parse(str);
      const indexOfUserToUpdate = users.findIndex(u => updatedUser.id == u.id);
      users[indexOfUserToUpdate] = updatedUser;
      localStorage.setItem("userData", JSON.stringify(users));
  }

  /**
   * Deletes a user from the localStorage database by their ID.
   * @param {number} id - The ID of the user to delete.
   */
  deleteUser(id){
      const str = localStorage.getItem("userData");
      const users = JSON.parse(str);
      const indexOfUserToRemove = users.findIndex(u => id == u.id);
      users.splice(indexOfUserToRemove,1);
      localStorage.setItem("userData", JSON.stringify(users));
  }

  ////////////////////////////////////
  // PRIVATE METHODS (start with #)
  ////////////////////////////////////

  /**
   * Retrieves the highest user ID from the localStorage database.
   * @private
   * @returns {number} The highest user ID in the localStorage.
   */
  #getMaxId(){
      const str = localStorage.getItem("userData");
      const users = JSON.parse(str);
      let maxId = 0;
      for(let x = 0; x < users.length; x++){
          if(users[x].id > maxId){
              maxId = users[x].id;
          }
      }
      return maxId;
  }
}