Example #1
0
        public void BallotPrompt(ElectionType electionType)
        {
            if (electionType == ElectionType.FirstPastThePost)
            {
                var i = 1;

                Console.WriteLine("Welcome to the election! This is a 'First Past The Post' election model.\nThese are the following candidates to select from:\n" +
                                  string.Join("\n", this.candidateArr.Select(x => $"{i++}: {x}"))
                                  );
                int counter = 1;
                int choice  = 0;
                while (counter < 4)
                {
                    Console.WriteLine("Please select your choice of candidate e.g. 1.");
                    choice = Convert.ToInt32(Console.ReadLine());
                    if (choice < 1 || choice > candidateArr.Length)
                    {
                        Console.WriteLine("Please enter a valid choice.");
                        counter += 1;
                    }
                    else
                    {
                        Console.WriteLine("Thank you for voting!");
                        break;
                    }
                }

                if (counter == 4)
                {
                    Console.WriteLine("You've exhausted your tries. Bye Bye");
                    return;
                }

                FirstPastThePostVote irVote = new FirstPastThePostVote(this.candidateArr[choice - 1]);
                var  jsonVote  = this.voteSerializer.Serialize(irVote);
                var  signature = SignatureProvider.Sign(this.password, this.keyPair, jsonVote.GetBytes());
                Vote vote      = new Vote(this.keyPair.PublicKey.GetBase64String(), jsonVote, signature.GetBase64String());
                this.voteMemoryPool.AddVote(vote);
            }
            else if (electionType == ElectionType.InstantRunoff)
            {
                var i = 1;
                Console.WriteLine("Welcome to the election! This is a 'Instant Runoff' election model.\nThese are the following candidates to select from:\n" +
                                  string.Join("\n", this.candidateArr.Select(x => $"{i++}: {x}"))
                                  );
                // We need to consider when a candidate is not wishing NONE
                int           counter        = 1;
                var           prefs          = "";
                List <string> tokens         = null;
                char[]        charSeparators = new char[] { ',' };
                while (counter < 4)
                {
                    Console.WriteLine("Please type a list in order of candidate preference e.g. 2,1,3.");
                    prefs  = Console.ReadLine();
                    tokens = prefs.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();

                    if (tokens.Count > this.candidateArr.Length)
                    {
                        Console.WriteLine("Please enter valid choices in the correct format.");
                        counter += 1;
                    }

                    List <string> rankedOrderedCandidates = new List <string>();

                    for (int j = 0; j < tokens.Count; j++)
                    {
                        rankedOrderedCandidates.Add(this.candidateArr[Convert.ToInt32(tokens[j]) - 1]);
                    }


                    InstantRunoffVote iroVote = new InstantRunoffVote(rankedOrderedCandidates); // make the IR vote
                    var  jsonVote             = this.voteSerializer.Serialize(iroVote);
                    var  signature            = SignatureProvider.Sign(this.password, this.keyPair, jsonVote.GetBytes());
                    Vote vote = new Vote(this.keyPair.PublicKey.GetBase64String(), jsonVote, signature.GetBase64String());
                    this.voteMemoryPool.AddVote(vote);
                    return;
                }

                Console.WriteLine("You've exhausted your tries. Bye Bye");
                return;
            }
        }
Example #2
0
        void ballotPrompt(ElectionType electionType)
        {
            if (electionType == ElectionType.FirstPastThePost)
            {
                var i = 1;
                Console.WriteLine("Welcome to the election! These are the following candidates to select from:\n" +
                                  string.Join("\n", this.candidateArr.Select(x => $"{i++}: {x}"))
                                  );
                int counter = 1;
                int choice  = 0;
                while (counter < 4)
                {
                    Console.WriteLine("Please select your choice of candidate e.g. 1.");
                    choice = Convert.ToInt32(Console.ReadLine());
                    if (choice < 1 || choice > candidateArr.Length)
                    {
                        Console.WriteLine("Please enter a valid choice.");
                        counter += 1;
                    }
                    else
                    {
                        break;
                    }
                }

                if (counter == 4)
                {
                    Console.WriteLine("You've exhausted your tries. Bye Bye");
                    return;
                }
                FirstPastThePostVote irVote = new FirstPastThePostVote(this.candidateArr[choice - 1]);
                var  jsonVote  = this.voteSerializer.Serialize(irVote);
                var  signature = SignatureProvider.Sign(this.password, this.keyPair, jsonVote.GetBytes());
                Vote vote      = new Vote(this.keyPair.PublicKey.GetBase64String(), jsonVote, signature.GetBase64String());
            }
            else if (electionType == ElectionType.InstantRunoff)
            {
                var i = 1;
                Console.WriteLine("Welcome to the election! These are the following candidates to select from:\n" +
                                  string.Join("\n", this.candidateArr.Select(x => $"{i++}: {x}"))
                                  );
                // We need to consider when a candidate is not wishing NONE
                int    counter        = 1;
                var    prefs          = "";
                char[] charSeparators = new char[] { ',' };
                while (counter < 4)
                {
                    Console.WriteLine("Please type a list in order of candidate preference e.g. 2, 1, 3.");
                    prefs = Console.ReadLine();
                    string[] tokens = prefs.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
                    if () //TODO validation check of voter input check
                    {
                        Console.WriteLine("Please enter a valid choice.");
                        counter += 1;
                    }
                    else
                    {
                        break;
                    }
                }

                if (counter == 4)
                {
                    Console.WriteLine("You've exhausted your tries. Bye Bye");
                    return;
                }
                InstantRunoffVote iroVote = new InstantRunoffVote(); // make the IR vote
                var  jsonVote             = this.voteSerializer.Serialize(iroVote);
                var  signature            = SignatureProvider.Sign(this.password, this.keyPair, jsonVote.GetBytes());
                Vote vote = new Vote(this.keyPair.PublicKey.GetBase64String(), jsonVote, signature.GetBase64String());
            }
        }