Enhancing a Voting System with C# for Robust Functionality
Introduction
Welcome back to another exciting session where we learn about enhancing existing functionality without causing regressions. Today, our scenario involves designing a voting system. We'll start with the basic implementation of the voting system and gradually introduce additional elements of complexity.
Starter Task Review
In our initial task, we created a simple voting system in C# with a set of basic functionalities:
bool RegisterCandidate(string candidateId): This method is used to add new candidates to our system.bool Vote(long timestamp, string voterId, string candidateId): This method facilitates users casting their votes. Each vote is given a timestamp.int? GetVotes(string candidateId): This method retrieves the total number of votes for a given candidate.List<string> TopNCandidates(int n): We also want to add a leaderboard functionality to our system. This method returns the topncandidates sorted by the number of votes.
Initial Solution Development
Let's jump into the C# code and begin the implementation of our starter task. Here, we use C#'s built-in Dictionary and List as the core of our design. These collections allow us to have dynamic lists keyed based on candidate IDs and voter IDs, which will greatly simplify our design.
