public ElectionOutcome IsThereAWinner(ElectionType electionType, string ElectoralCollegeVotes = @"ElectoralCollegeVotes\") { foreach (KeyValuePair <int, CandidateElectoralCollegeVotes> c in IDCandidateElectoralCollegeVotes) { c.Value.clearElectoralCollegeVotes(); } if (electionType == ElectionType.InstantRunoff) { Round++; } FederalJsonReader reader = new FederalJsonReader(); foreach (FederalState state in states) { KeyValuePair <int, List <KeyValuePair <int, int> > > CandidateAndVotes = reader.ReadStatesResults(state.Name); if (CandidateAndVotes.Key <= state.Electors) { foreach (KeyValuePair <int, int> candidateVote in CandidateAndVotes.Value) { IDCandidateElectoralCollegeVotes[candidateVote.Key].SetElcetoralCollegeVotes(candidateVote.Value); } } else { throw new System.InvalidOperationException(state.Name + " is submitting " + CandidateAndVotes.Key.ToString() + " votes instead of " + state.Electors.ToString()); } } int mostElectoralVotes = 0; List <CandidateElectoralCollegeVotes> outcomeList = new List <CandidateElectoralCollegeVotes>(); foreach (KeyValuePair <int, CandidateElectoralCollegeVotes> c in IDCandidateElectoralCollegeVotes) { outcomeList.Add(c.Value); if (c.Value.GetElcetoralCollegeVotes() > mostElectoralVotes) { mostElectoralVotes = c.Value.GetElcetoralCollegeVotes(); } } if (mostElectoralVotes > totalElectoralCollegeVotes / 2 || IDCandidateElectoralCollegeVotes.Count == 1) { int max = IDCandidateElectoralCollegeVotes.Aggregate((l, r) => l.Value.GetElcetoralCollegeVotes() > r.Value.GetElcetoralCollegeVotes() ? l : r).Key; ElectionOutcome WinningOutcome = new ElectionOutcome(); WinningOutcome.ElectionType = electionType; WinningOutcome.Winner = IDCandidateElectoralCollegeVotes[max]; WinningOutcome.outcome = Outcome.ClearWinner; if (electionType == ElectionType.InstantRunoff) { WinningOutcome.Round = Round; } WinningOutcome.Results = outcomeList; WinningOutcome.totalElectoralCollegeVotes = totalElectoralCollegeVotes; return(WinningOutcome); } else { return(checkForTie(outcomeList)); } }
private void setStates(string statePath) { FederalJsonReader reader = new FederalJsonReader(); states = reader.collectStates(statePath); if (states.Count == 0) { throw new System.InvalidOperationException("There are no states present in this election"); } }
private void setCandidates(string candidatePath) { FederalJsonReader reader = new FederalJsonReader(); candidates = reader.collectCandidates(candidatePath); if (candidates.Count == 0) { throw new System.InvalidOperationException("There are no candidates present in this election"); } foreach (Candidate c in candidates) { CandidateElectoralCollegeVotes tempCandidatesVotes = new CandidateElectoralCollegeVotes(); tempCandidatesVotes.candidate = c; IDCandidateElectoralCollegeVotes.Add(c.ID, tempCandidatesVotes); } }