public void AddContest(Contest contest) { Contests.Add(contest); }
static Ballot BuildBallotFromJson(string fileName) { string contestCode; string contestData; JsonDocument contestDoc; JsonElement contestRoot; string contestName; int contestMaxChoices; bool contestWriteIn; JsonElement contestCandidates; Contest contest; string candidateCode; string candidateName; string candidateParty; Candidate candidate; Dictionary <string, Candidate> candidatesTable; List <string> candidatesOrder; // access the ballot json file string ballotData = File.ReadAllText(fileName); JsonDocument ballotDoc = JsonDocument.Parse(ballotData); JsonElement ballotRoot = ballotDoc.RootElement; string ballotName = ballotRoot.GetProperty("BallotName").ToString(); JsonElement contests = ballotRoot.GetProperty("Contests"); Ballot ballot = new Ballot(ballotName); // for each contest in ballot for (int i = 0; i < contests.GetArrayLength(); i++) { // get the order of candidate codes JsonElement order = contests[i].GetProperty("CandidateCodes"); candidatesOrder = new List <string>(); for (int orderIndex = 0; orderIndex < order.GetArrayLength(); orderIndex++) { candidatesOrder.Add(order[orderIndex].GetString()); } // begin new candidatesTable candidatesTable = new Dictionary <string, Candidate>(); // access the contest json file to get all candidate information contestCode = contests[i].GetProperty("ContestCode").ToString(); contestData = File.ReadAllText("CONTEST_" + contestCode + ".json"); contestDoc = JsonDocument.Parse(contestData); contestRoot = contestDoc.RootElement; contestName = contestRoot.GetProperty("ContestName").GetString(); contestRoot.GetProperty("MaxChoices").TryGetInt32(out contestMaxChoices); contestWriteIn = contestRoot.GetProperty("WriteIn").GetBoolean(); contestCandidates = contestRoot.GetProperty("Candidates"); contest = new Contest(contestCode, contestName, contestMaxChoices); // for each candidate in contest for (int j = 0; j < contestCandidates.GetArrayLength(); j++) { candidateCode = contestCandidates[j].GetProperty("CandidateCode").GetString(); candidateName = contestCandidates[j].GetProperty("CandidateName").GetString(); candidateParty = contestCandidates[j].GetProperty("CandidateParty").GetString(); candidate = new Candidate(candidateCode, candidateName, candidateParty); //contest.AddCandidate(candidate); // add candidate and its candidateCode to candidatesTable candidatesTable.Add(candidateCode, candidate); } // add candidates in the correct order foreach (string candCode in candidatesOrder) { contest.AddCandidate(candidatesTable[candCode]); } // add writeins if appropriate if (contestWriteIn) { contest.AddBlankWriteIns(); } ballot.AddContest(contest); } return(ballot); }