FPTPStrategy
TheFPTPStrategy class implements the First Past The Post (FPTP) voting system, also known as simple plurality voting. In this system, voters select a single candidate, and the candidate with the most votes wins.
Algorithm
FPTP uses a simple counting mechanism:- Count the number of votes each candidate receives
- Sort candidates by vote count in descending order
- The candidate with the most votes is declared the winner
- If multiple candidates are tied for the top position, the tie is flagged for manual resolution
Interface
calculateResults
Calculates election results using the FPTP mechanism.Array of all ballots cast in the election. Each ballot must contain a single candidate ID in its preferences array.
Array of all candidates participating in the election.
Ordered array of vote results, sorted by vote count (descending). Each result contains:
Unique identifier of the candidate
Display name of the candidate
Total number of votes received
Percentage of total votes (0-100)
True if this candidate won the election. False if tied or lost.
True if multiple candidates are tied for first place. When true, isWinner will be false.
Example
validateBallot
Validates that a ballot is properly formed for FPTP voting.The ballot to validate
Number of candidates in the election (not used in FPTP validation)
Returns
true if the ballot contains exactly one candidate preference, false otherwise.Validation Rules
- The ballot must have a preferences array
- The preferences array must contain exactly one candidate ID
- Multiple selections or empty ballots are invalid
Example
Tie Handling
When multiple candidates receive the same highest number of votes:- All tied candidates have
isTied: true - All tied candidates have
isWinner: false - The tie must be resolved manually by election administrators
Implementation Location
Source:~/workspace/source/src/services/strategies/FPTPStrategy.ts:6-53