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 void shouldStartCountingVotes() { // Reset the votes to 0 resetCandidateVotes(); // For each ballot, find the highest remaining candidate and add the vote foreach (VCSBallot ballot in ballots) { int candidateIndex = 0; while (ballot.hasCandidateAtIndex(candidateIndex)) { // Get the candidate from the ballot VCSCandidate candidate = ballot.getCandidateChoiceAtIndex(candidateIndex); if (candidateVotes.ContainsKey(candidate)) { int votes = candidateVotes[candidate]; candidateVotes[candidate] = ++votes; break; } else { // That candidate doesn't appear in the structure, so has been removed // Take the next choice instead candidateIndex++; } } } checkForVoteWinner(); updateView(); }
/** * 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); }
public VCSCandidate getCandidateWithName(string name) { Debug.Assert(!name.Equals(""), "Name is empty"); Debug.Assert(isValidCandidateWithName(name), "Name is not valid"); VCSCandidate selectedCandidate = null; foreach (VCSCandidate candidate in candidates) { if (candidate.getName().Equals(name)) { selectedCandidate = candidate; break; } } return(selectedCandidate); }
public void redistributeVotesForCandidates() { int lowestVote = int.MaxValue; List <VCSCandidate> potentialLowestCandidates = new List <VCSCandidate>(); // remove the candidate with the least votes foreach (VCSCandidate candidate in getAllCandidates()) { int voteCount = candidateVotes[candidate]; if (voteCount < lowestVote) { // A new lowest vote, so reset the list of candidates with the same vote potentialLowestCandidates = new List <VCSCandidate>(); // Add candidate to list of lowest votes potentialLowestCandidates.Add(candidate); // Update the lowest vote + candidate lowestVote = voteCount; } else if (voteCount == lowestVote) { // Candidate has joint lowest vote, add to lowest vote list potentialLowestCandidates.Add(candidate); } } // Do more than one candidate have the lowest votes? if (potentialLowestCandidates.Count() > 1) { // Fetch a random candidate, remove them VCSCandidate lowestCandidate = getRandomCandidateToRemoveFromCandidatesWithLowestVotes(potentialLowestCandidates); candidateVotes.Remove(lowestCandidate); } else { //Only one candidate with lowest votes // remove from structure candidateVotes.Remove(potentialLowestCandidates[0]); } // Recount the votes shouldStartCountingVotes(); }
public int getVotesForCandidate(VCSCandidate candidate) { Debug.Assert(isValidCandidateWithName(candidate.getName()), "Candidate does not exist"); return(candidateVotes[candidate]); }