Micro Project
On
Voting System
By
Rana Darpan K.
Sadhu Siddharth Tarunbhai
Dhruv Prajapati
Bhagya Patel
Enrollment No: 216040316067
Enrollment No: 216040316019
Enrollment No: 216040316026
Enrollment No: 216040316018
A Micro Project in Data Structure with Python
(4331601) Submitted to
Information Technology Department
B & B Institute of Technology, Vallabh Vidyanagar
1
Introduction
The provided code is a Java program that implements a simple voting system. It allows users
to vote for candidates and keeps track of the total votes received by each candidate. Let's go
through the main components and functionalities of the code:
1. The `VotingSystem` class:
- This class represents the voting system and contains methods for adding candidates,
voting for candidates, and displaying the voting results.
- It maintains a `candidateVotes` map, where the candidate names are stored as keys, and
the corresponding vote counts are stored as values.
2. The `addCandidate()` method:
- This method allows the addition of candidates to the voting system by adding their names
to the `candidateVotes` map.
- The initial vote count for each candidate is set to 0.
3. The `voteForCandidate()` method:
- This method records a vote for a given candidate.
- It checks if the candidate exists in the `candidateVotes` map, retrieves the current vote
count for that candidate, increments it by 1, and updates the map with the new vote count.
- If the candidate does not exist in the map, it displays a message indicating that the
candidate is not valid.
4. The `displayResults()` method:
- This method iterates over the `candidateVotes` map and displays the candidate names
along with their corresponding vote counts.
2
- It provides an overview of the voting results.
5. The `main()` method:
- The main entry point of the program.
- It creates an instance of the `VotingSystem` class and sets up some initial candidates using
the `addCandidate()` method.
- It prompts the user to enter the name of the candidate they want to vote for.
- If the user enters "exit", the program exits the voting loop and displays the voting results
using the `displayResults()` method.
Overall, this Java program demonstrates a basic implementation of a voting system, allowing
users to vote for candidates and presenting the final vote counts for each candidate.
3
Program
import [Link];
import [Link];
import [Link];
public class VotingSystem {
private Map<String, Integer> candidateVotes;
public VotingSystem() {
candidateVotes = new HashMap<>();
public void addCandidate(String candidateName) {
[Link](candidateName, 0);
public void voteForCandidate(String
candidateName) { if
([Link](candidateName)) {
int currentVotes = [Link](candidateName);
[Link](candidateName, currentVotes + 1);
[Link]("Vote recorded for " + candidateName);
} else {
[Link](candidateName + " is not a valid candidate.");
4
public void displayResults() {
[Link]("Voting Results:");
for ([Link]<String, Integer> entry : [Link]()) {
String candidateName = [Link]();
int votes = [Link]();
[Link](candidateName + ": " + votes + " votes");
public static void main(String[] args) {
VotingSystem votingSystem = new VotingSystem();
[Link]("Candidate 1");
[Link]("Candidate 2");
[Link]("Candidate 3");
Scanner scanner = new Scanner([Link]);
boolean votingComplete = false;
while (!votingComplete) {
[Link]("Enter the name of the candidate you want to vote for (or 'exit' to quit):");
String candidateName = [Link]();
if ([Link]("exit")) {
votingComplete = true;
5
}
else {
[Link](candidateName);
[Link]();
Output :
Enter the name of the candidate you want to vote for (or 'exit' to quit):
Candidate 1
Vote recorded for Candidate 1
Enter the name of the candidate you want to vote for (or 'exit' to quit):
Candidate 2
Vote recorded for Candidate 2
Enter the name of the candidate you want to vote for (or 'exit' to quit):
Candidate 1
Vote recorded for Candidate 1
Enter the name of the candidate you want to vote for (or 'exit' to quit):
Candidate 3
Vote recorded for Candidate 3
Enter the name of the candidate you want to vote for (or 'exit' to quit):
exit
6
Voting Results:
Candidate 1: 2 votes
Candidate 2: 1 votes
Candidate 3: 1 votes