Enhancing a Voting System in Kotlin: Implementing New Functionalities and Ensuring Compatibility
Introduction
Welcome back to another exciting session where we learn about enhancing existing functionality without causing regressions. Our scenario today 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 Kotlin with a set of basic functionalities:
fun registerCandidate(candidateId: String): Boolean: This function is used for adding new candidates to our system.fun vote(timestamp: Long, voterId: String, candidateId: String): Boolean: This function facilitates users casting their votes. Each vote is given a timestamp.fun getVotes(candidateId: String): Int?: This function retrieves the total number of votes for a given candidate.fun topNCandidates(n: Int): List<String>: We also want to add a leaderboard functionality to our system. This function returns the topncandidates sorted by the number of votes.
Initial Solution Development
Let's jump into the Kotlin code and begin the implementation of our starter task. Here, we use Kotlin's mutableMapOf() and mutableListOf() 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.
