Class AccountController
java.lang.Object
controller.AccountController
Controller responsible for managing user accounts and authentication.
Handles registration, login, logout, password changes, password hashing,
user lookup across different user types (Applicant, Officer, Manager),
and stores the ID of the currently logged-in user.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidchangePassword(String userID, String oldPassword, String newPassword) Changes the password for a given user ID after verifying the old password.static booleancheckPassword(String userID, String password) Checks if the provided plain text password matches the stored hashed password for the given user ID.static UserFinds a user by their user ID across all user type lists.static StringGets the user ID of the currently authenticated user.static UserAttempts to log in a user with the provided credentials.static voidlogout()Logs out the currently authenticated user.static voidregister(UserType userType, String userID, String password, String name, int age, MaritalStatus maritalStatus) Registers a new user in the system.static voidSets the user ID for the currently authenticated user.
-
Constructor Details
-
AccountController
public AccountController()
-
-
Method Details
-
getUserID
Gets the user ID of the currently authenticated user.- Returns:
- The user ID string, or null if no user is logged in.
-
setUserID
Sets the user ID for the currently authenticated user. Typically called upon successful login and set to null upon logout.- Parameters:
ID- The user ID to set.
-
register
public static void register(UserType userType, String userID, String password, String name, int age, MaritalStatus maritalStatus) throws InvalidUserFormatException, AlreadyRegisteredException Registers a new user in the system. Checks the user ID format and if the ID is already taken. Hashes the password before storing. Adds the user to the appropriate lists based on their UserType: - All users are added to ApplicantList. - Officers and Managers are also added to OfficerList. - Managers are additionally added to ManagerList.- Parameters:
userType- The type of user to register (APPLICANT, OFFICER, MANAGER).userID- The user ID for the new account. Must match specific format.password- The plain text password for the new account.name- The name of the user.age- The age of the user.maritalStatus- The marital status of the user.- Throws:
InvalidUserFormatException- If the userID does not match the required format (e.g., "^[ST]\\d{7}[A-Z]$").AlreadyRegisteredException- If a user with the given userID already exists.
-
findUser
Finds a user by their user ID across all user type lists. Searches in the order: ManagerList, OfficerList, ApplicantList. Returns the first user found with the matching ID.- Parameters:
userID- The user ID to search for.- Returns:
- The User object (Manager, Officer, or Applicant) if found.
- Throws:
UserNotFoundException- If no user with the given ID is found in any list.
-
checkPassword
public static boolean checkPassword(String userID, String password) throws UserNotFoundException, PasswordIncorrectException Checks if the provided plain text password matches the stored hashed password for the given user ID.- Parameters:
userID- The user ID whose password needs checking.password- The plain text password to check.- Returns:
- true if the password matches.
- Throws:
UserNotFoundException- If the user ID does not exist.PasswordIncorrectException- If the provided password does not match the stored hash.
-
login
public static User login(String userID, String password) throws UserNotFoundException, PasswordIncorrectException Attempts to log in a user with the provided credentials. Finds the user by ID and checks if the provided password is correct. If successful, sets the currently logged-in user ID and returns the User object.- Parameters:
userID- The user ID attempting to log in.password- The plain text password provided for login.- Returns:
- The User object (Manager, Officer, or Applicant) upon successful login.
- Throws:
UserNotFoundException- If the user ID does not exist.PasswordIncorrectException- If the provided password does not match.
-
logout
public static void logout()Logs out the currently authenticated user. Clears the stored user ID and navigates back to the login page. -
changePassword
public static void changePassword(String userID, String oldPassword, String newPassword) throws UserNotFoundException, PasswordIncorrectException Changes the password for a given user ID after verifying the old password. Finds the user, checks the old password, hashes the new password, and updates the user object in all relevant user lists (Applicant, Officer, Manager). Note: Assumes user objects in different lists (e.g., ApplicantList, OfficerList) might need separate updates even if they represent the same underlying user.- Parameters:
userID- The user ID whose password needs changing.oldPassword- The current plain text password for verification.newPassword- The new plain text password to set.- Throws:
UserNotFoundException- If the user ID does not exist.PasswordIncorrectException- If the provided oldPassword does not match.
-