How to Build a Simple Voting System Using Collections in Java?

How to Build a Simple Voting System Using Collections in Java?

Introduction

In this tutorial, we’ll walk through building a simple voting system in Java using the Collections Framework. Java’s built-in collections, such as HashMap and ArrayList, are perfect for creating efficient systems to handle user votes. A voting system typically collects votes from users, processes them, and then provides results based on the votes cast. We’ll explore how to achieve this using various collection classes in Java.

Prerequisites

Before proceeding with the implementation, you should have a basic understanding of the following:

  • Java programming language
  • Collections Framework in Java
  • Basic understanding of HashMap, ArrayList, and other collection types

Step 1: Set Up the Project

Start by creating a new Java project in your favorite IDE, or you can use the command line to compile and run your Java program. In this tutorial, we will focus on the logic of the voting system and its implementation.

Step 2: Define the Data Structures

We need a data structure to store the candidate names and their vote counts. A HashMap will be ideal for this purpose, as it allows efficient storage and retrieval of key-value pairs. The key will be the candidate’s name (a String) and the value will be the number of votes they have received (an Integer).

import java.util.HashMap; import java.util.Map; public class VotingSystem { private Map candidates; public VotingSystem() { candidates = new HashMap<>(); } }

Step 3: Add Candidates to the System

We will allow the addition of candidates to the voting system. A method called addCandidate can be used to insert candidates into our HashMap with an initial vote count of zero.

public void addCandidate(String candidateName) { candidates.put(candidateName, 0); }

Step 4: Allow Users to Vote

Next, we’ll implement the logic that allows users to vote for a candidate. The vote method will take a candidate’s name as input and increment the vote count for that candidate.

public void vote(String candidateName) { if (candidates.containsKey(candidateName)) { candidates.put(candidateName, candidates.get(candidateName) + 1); } else { System.out.println("Candidate not found!"); } }

Step 5: Display the Results

After voting, we need to display the vote count for each candidate. We will loop through the HashMap and print out each candidate’s name along with their respective vote count.

public void displayResults() { System.out.println("Voting Results:"); for (Map.Entry entry : candidates.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue() + " votes"); } }

Step 6: Putting It All Together

Now, let’s put everything together in a complete Java program. We’ll create a simple main method where users can add candidates, vote for them, and display the results.

import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class VotingSystem { private Map candidates; public VotingSystem() { candidates = new HashMap<>(); } public void addCandidate(String candidateName) { candidates.put(candidateName, 0); } public void vote(String candidateName) { if (candidates.containsKey(candidateName)) { candidates.put(candidateName, candidates.get(candidateName) + 1); } else { System.out.println("Candidate not found!"); } } public void displayResults() { System.out.println("Voting Results:"); for (Map.Entry entry : candidates.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue() + " votes"); } } public static void main(String[] args) { VotingSystem votingSystem = new VotingSystem(); Scanner scanner = new Scanner(System.in); // Add candidates System.out.println("Enter the number of candidates:"); int numCandidates = scanner.nextInt(); scanner.nextLine(); // Consume the newline for (int i = 0; i < numCandidates; i++) { System.out.println("Enter the name of candidate " + (i + 1) + ":"); String candidateName = scanner.nextLine(); votingSystem.addCandidate(candidateName); } // Voting process System.out.println("Enter the number of votes:"); int numVotes = scanner.nextInt(); scanner.nextLine(); // Consume the newline for (int i = 0; i < numVotes; i++) { System.out.println("Enter the name of the candidate you want to vote for:"); String vote = scanner.nextLine(); votingSystem.vote(vote); } // Display results votingSystem.displayResults(); } }

Conclusion

This simple voting system demonstrates the power and simplicity of Java's Collections Framework. By using a HashMap to store candidates and their votes, we can efficiently add candidates, vote, and display results. Java's Collections API allows us to build powerful applications with ease, leveraging its various data structures for different needs.

© 2024 Tech Interview Guide. All Rights Reserved.

Please follow and like us:

Leave a Comment