/** * Adds a new candidate to the ballot * * @param candidate - The candidate to add * * @pre. candidate != null * * @post. candidates != null * @post. candidates contains candidate */ public void addCandidateToBallot(VCSCandidate candidate) { Debug.Assert(candidate != null, "Candidate is null"); if (candidates == null) { candidates = new List<VCSCandidate>(); } candidates.Add(candidate); }
private bool checkForVoteWinner() { // If there is one candidate left, vote is over if (getAllCandidates().Count() == 1) { winningCandidate = getAllCandidates()[0]; return true; } // If one candidate has more than 50% of vote, vote is over int ballotCount = getBallots().Count(); VCSCandidate provisionallyWinningCandidate = null; int provisionallyWinningScore = 51; // For each candidate, check if they have more than 50% of the vote foreach (VCSCandidate candidate in getAllCandidates()) { int candidateVotes = getVotesForCandidate(candidate); int percentageOfVote = (candidateVotes * 100) / ballotCount; if (percentageOfVote >= provisionallyWinningScore) { provisionallyWinningCandidate = candidate; provisionallyWinningScore = percentageOfVote; } } winningCandidate = provisionallyWinningCandidate; return provisionallyWinningCandidate != null; }
public int getVotesForCandidate(VCSCandidate candidate) { Debug.Assert(isValidCandidateWithName(candidate.getName()), "Candidate does not exist"); return candidateVotes[candidate]; }
/** * Used to initialise a ballot with a completed list of candidates * @param candidatesSelected - The candidates selected * * @post. The ballot will have the candidates in order and be ready to use */ public VCSBallot(VCSCandidate[] candidatesSelected) { this.candidateOrder = candidatesSelected; }