public IList <VoteTally> ConvertToTally(int[] tallyResult, ElectionMap electionMap) { if (tallyResult.Length != electionMap.NumberOfSelections) { throw new Exception("Tally count does not match number of selections"); } var voteTallies = new List <VoteTally>(); foreach (var contestMap in electionMap.ContestMaps.Values) { var voteTally = new VoteTally { Candidates = new int[contestMap.SelectionMap.Count] }; foreach (var(key, value) in contestMap.SelectionMap) { switch (key.ToLower()) { case "yes": voteTally.Yes = tallyResult[value]; break; case "no": voteTally.No = tallyResult[value]; break; default: voteTally.Candidates[value - contestMap.StartIndex] = tallyResult[value]; break; } } if (contestMap.WriteInStartIndex != contestMap.EndIndex) // Write-Ins Exist { var writeInTally = 0; for (var i = contestMap.WriteInStartIndex; i < contestMap.NullVoteStartIndex; i++) { writeInTally += tallyResult[i]; } voteTally.WriteIns = new [] { new WriteInCandidateTally { Name = "Write In", Tally = writeInTally, } }; } voteTallies.Add(voteTally); } return(voteTallies); }
public bool[] ConvertToSelections(Ballot ballot, ElectionMap electionMap) { var numberOfSelections = electionMap.NumberOfSelections; var ballotStyleMap = electionMap.BallotStyleMaps[ballot.BallotStyle.Id]; var selections = new bool[numberOfSelections]; foreach (var contestMap in ballotStyleMap.ContestMaps.Values) { var voteExists = ballot.Votes.TryGetValue(contestMap.Contest.Id, out var vote); // No Selection becomes Null Votes if (!voteExists) { selections = AddNullProtectionVotes(selections, contestMap); continue; } // Handle Votes switch (contestMap.Contest.Type) { case ContestType.Candidate: if (vote.GetType() == typeof(JArray)) { vote = ((JArray)vote).Select(x => x.ToObject <Candidate>()).ToArray(); } selections = AddCandidateSelections(selections, (Candidate[])vote, contestMap); break; case ContestType.YesNo: selections = AddYesNoSelection(selections, (string)vote, contestMap); break; } selections = AddNullProtectionVotes(selections, contestMap); } return(selections); }