示例#1
0
        static void Main(string[] args)
        {
            var     ballot = new Ballot("Demo Ballot");
            Contest contest;

            contest = new Contest("BF", "Board of Finance", 4);
            contest.AddCandidate(new Candidate("RUTH", "Babe Ruth", "REP"));
            contest.AddCandidate(new Candidate("THORPE", "Jim Thorpe", "REP"));
            contest.AddCandidate(new Candidate("MARAVICH", "Pete Maravich", "REP"));
            contest.AddCandidate(new Candidate("ROBINSON", "Jackie Robinson", "REP"));
            contest.AddCandidate(new Candidate("COOLIDGE", "Calvin Coolidge", "DEM"));
            contest.AddCandidate(new Candidate("KENNEDY", "John F. Kennedy", "DEM"));
            contest.AddBlankWriteIns();
            ballot.AddContest(contest);

            contest = new Contest("BE", "Board of Education", 3);
            contest.AddCandidate(new Candidate("JONES", "Bobby Jones", "REP"));
            contest.AddCandidate(new Candidate("RUDOLPH", "Wilma Rudolph", "REP"));
            contest.AddCandidate(new Candidate("WALKER", "Jimmy Walker", "DEM"));
            contest.AddCandidate(new Candidate("DALEY", "Richard Daley", "DEM"));
            contest.AddCandidate(new Candidate("CHARLES", "Ray Charles", "PET"));
            contest.AddCandidate(new Candidate("DISNEY", "Walt Disney", "PET"));
            contest.AddCandidate(new Candidate("LINCOLN", "Abraham Lincoln", "PET"));
            contest.AddBlankWriteIns();
            ballot.AddContest(contest);

            contest = new Contest("BA", "Board of Assessment Appeals", 1);
            contest.AddCandidate(new Candidate("MARCIANO", "Rocky Marciano", "REP"));
            contest.AddBlankWriteIns();
            ballot.AddContest(contest);

            contest = new Contest("PZ", "Planning and Zoning Commission", 4);
            contest.AddCandidate(new Candidate("WASHINGTON", "George Washington", "REP"));
            contest.AddCandidate(new Candidate("BARTON", "Clara Barton", "REP"));
            contest.AddCandidate(new Candidate("ASH", "Arthur Ash", "REP"));
            contest.AddCandidate(new Candidate("PIERCE", "Arthur Pierce", "REP"));
            contest.AddCandidate(new Candidate("FRANKLIN-DEM", "Benjamin Franklin", "DEM"));
            contest.AddCandidate(new Candidate("FRANKLIN-SAN", "Benjamin Franklin", "SNA"));
            contest.AddBlankWriteIns();
            ballot.AddContest(contest);
            ballot.Output();
            Vote(ballot);
        }
示例#2
0
        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);
        }
示例#3
0
 public void AddContest(Contest contest)
 {
     Contests.Add(contest);
 }
示例#4
0
        static void Vote(Ballot ballot)
        {
            Contest   currentContest   = ballot.contests[ballot.currentContestIndex];
            Candidate currentCandidate = ballot.contests[ballot.currentContestIndex].candidates[ballot.currentCandidateIndex];
            bool      isDone           = false;

            Console.Write(currentContest.name + " --- " + currentCandidate.fullName);
            if (currentCandidate.party == "WRITE-IN")
            {
                Console.Write(" (WRITE-IN)");
            }
            if (currentCandidate.isSelected)
            {
                Console.WriteLine(" (Selected)");
            }
            else
            {
                Console.WriteLine();
            }

            Console.Write("Press a key --  ");
            List <int> options = new List <int>();

            options.Add(0);
            Console.Write("0: Display Ballot  ");

            if (ballot.currentContestIndex != 0)
            {
                options.Add(2);
                Console.Write("2: Prev Contest  ");
            }
            if (ballot.currentCandidateIndex != 0)
            {
                options.Add(4);
                Console.Write("4: Prev Candidate  ");
            }
            if (currentCandidate.isSelected)
            {
                options.Add(5);
                Console.Write("5: Deselect  ");
            }
            else
            {
                options.Add(5);
                Console.Write("5: Select  ");
            }

            if (ballot.currentCandidateIndex != currentContest.candidates.Count - 1)
            {
                options.Add(6);
                Console.Write("6: Next Candidate  ");
            }

            if (ballot.currentContestIndex != ballot.contests.Count - 1)
            {
                options.Add(8);
                Console.Write("8: Next Contest  ");
            }
            else
            {
                isDone = true;
                options.Add(8);
                Console.Write("8: Done  ");
            }
            Console.WriteLine();
            int input = ReadKey();

            Console.WriteLine();


            if (!options.Contains(input))
            {
                Console.WriteLine("Invalid input!");
                Console.WriteLine();
                Vote(ballot);
                return;
            }

            if (input == 0)
            {
                ballot.Output();
            }
            if (input == 2)
            {
                ballot.currentContestIndex--;
                ballot.currentCandidateIndex = 0;
            }
            if (input == 4)
            {
                ballot.currentCandidateIndex--;
            }
            if (input == 5)
            {
                if (currentCandidate.isSelected)
                {
                    if (currentCandidate.party == "WRITE-IN")
                    {
                        currentCandidate.fullName = "";
                    }
                    currentCandidate.isSelected = false;
                }
                else
                {
                    if (currentContest.GetHowManySelected() >= currentContest.numberToVote)
                    {
                        Console.WriteLine("Overvote!");
                    }
                    else
                    {
                        if (currentCandidate.party == "WRITE-IN")
                        {
                            Console.WriteLine("Enter the write-in name: ");
                            string nameInput = Console.ReadLine().Trim();
                            if (nameInput == "")
                            {
                                Console.WriteLine("Invalid name");
                            }
                            else
                            {
                                currentCandidate.fullName   = nameInput;
                                currentCandidate.isSelected = true;
                            }
                        }
                    }
                    currentCandidate.isSelected = true;
                }
            }
            if (input == 6)
            {
                ballot.currentCandidateIndex++;
            }
            if (input == 8)
            {
                if (isDone)
                {
                    return;
                }
                else
                {
                    ballot.currentContestIndex++;
                    ballot.currentCandidateIndex = 0;
                }
            }

            Console.WriteLine();
            Vote(ballot);
        }