Example #1
0
        static void Vote(Ballot ballot)
        {
            ballot.PrepForVoting();
            char userInput;
            bool done = false;

            do
            {
                ballot.DisplayCurrentCandidate();
                List <char> options = ballot.DisplayOptions();
                userInput = Console.ReadKey(true).KeyChar;

                // if user does not enter a valid option, set userInput to trigger default switch case
                if (!options.Contains(userInput))
                {
                    userInput = '!';
                }
                switch (userInput)
                {
                case '0':
                    ballot.Output();
                    break;

                case '2':
                    ballot.GoToPrevContest();
                    break;

                case '4':
                    ballot.GoToPrevCandidate();
                    break;

                case '5':
                    ballot.SelectCandidate();
                    break;

                case '6':
                    ballot.GoToNextCandidate();
                    break;

                case '8':
                    if (ballot.CurrentContest.IsLastContest)
                    {
                        done = true;
                        break;
                    }
                    ballot.GoToNextContest();
                    break;

                default:
                    Console.WriteLine("Invalid key!");
                    break;
                }
            } while (!done);
            Console.WriteLine("Here is your final ballot:");
            ballot.Output();
            Console.WriteLine("\n...Program ended!");
        }
Example #2
0
        static void Main(string[] args)
        {
            Ballot ballot = BuildBallotFromJson("BALLOT_0002.json");

            ballot.Output();
            Vote(ballot);
        }
Example #3
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);
        }
Example #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);
        }