Overview
TheVoter class is a domain entity that represents a registered voter in the Consensus e-voting platform. It encapsulates voter identity, authentication credentials, and registration status with controlled mutation through setters.
Constructor
Unique identifier for the voter
Full name of the voter (cannot be empty)
Email address of the voter (must contain @)
Hashed password for authentication (never store plain text)
Current registration status. Defaults to
PENDING if not specified.Date when the voter registered. Defaults to current date if not specified.
Example
Properties
Unique identifier for the voter (read-only)
Full name of the voter. Can be updated via setter with validation.
Email address of the voter. Can be updated via setter with validation.
Hashed password for authentication. Can be updated via setter with validation.
Current registration status. Can be updated via setter.Valid values:
RegistrationStatus.PENDING- Awaiting approvalRegistrationStatus.APPROVED- Approved to voteRegistrationStatus.REJECTED- Registration rejectedRegistrationStatus.SUSPENDED- Temporarily suspended
Date when the voter registered (read-only)
Setters
name
Updates the voter’s name with validation.- Throws
Errorif name is empty or contains only whitespace
- Throws
Errorif email is empty or doesn’t contain@
passwordHash
Updates the voter’s password hash with validation.- Throws
Errorif password hash is empty
registrationStatus
Updates the voter’s registration status.PENDING→APPROVEDPENDING→REJECTEDAPPROVED→SUSPENDED
Methods
isApproved()
Checks if the voter is approved.true if the registration status is APPROVED.
Example:
isSuspended()
Checks if the voter is suspended.true if the registration status is SUSPENDED.
canVote()
Determines if the voter is eligible to cast a vote.true if the voter is approved and not suspended.
Business Logic: A voter can vote only if they are both approved AND not suspended.
Example:
Usage Example
Security Notes
- Always store password hashes, never plain text passwords
- The
passwordHashproperty should only be set with properly hashed values - Email validation is basic; implement additional validation in your application layer