w3resource

Java Simple Voting System Project

Two Solutions for Implementing a Voting System in Java:

Simple Voting System:

Input: Candidate name or number to vote for.
Output: Confirmation of the vote and updated vote count for each candidate.

Example:

  • Input: "Vote for Candidate 1"
  • Output: "Vote cast for Candidate 1. Current votes: Candidate 1: 10 votes, Candidate 2: 7 votes."

Solution 1: Simple Voting System using an Array

This solution uses an array to store votes for each candidate.

Code:

import java.util.Scanner;

public class VotingSystemArray {
    // Array to store votes for candidates
    private static int[] votes = new int[3];  // Assuming 3 candidates

    // Method to cast a vote
    public static void castVote(int candidate) {
        if (candidate >= 1 && candidate <= votes.length) {
            votes[candidate - 1]++;  // Increment vote count for the chosen candidate
            System.out.println("Vote cast for Candidate " + candidate);
        } else {
            System.out.println("Invalid candidate number. Try again.");
        }
    }

    // Method to display the vote count
    public static void displayResults() {
        for (int i = 0; i < votes.length; i++) {
            System.out.println("Candidate " + (i + 1) + ": " + votes[i] + " votes");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean voting = true;

        while (voting) {
            System.out.println("Enter the candidate number to vote for (1-3), or 0 to end voting:");
            int input = scanner.nextInt();

            if (input == 0) {
                voting = false;  // End voting if input is 0
            } else {
                castVote(input);  // Cast a vote
            }
        }

        System.out.println("Voting has ended. Final results:");
        displayResults();  // Display final results
        scanner.close();
    }
}

Output:

Enter the candidate number to vote for (1-3), or 0 to end voting:
 2
Vote cast for Candidate 2
Enter the candidate number to vote for (1-3), or 0 to end voting:
 1
Vote cast for Candidate 1
Enter the candidate number to vote for (1-3), or 0 to end voting:
 1
Vote cast for Candidate 1
Enter the candidate number to vote for (1-3), or 0 to end voting:
 2
Vote cast for Candidate 2
Enter the candidate number to vote for (1-3), or 0 to end voting:
 2
Vote cast for Candidate 2
Enter the candidate number to vote for (1-3), or 0 to end voting:
 0
Voting has ended. Final results:
Candidate 1: 2 votes
Candidate 2: 3 votes
Candidate 3: 0 votes

Explanation :

  • Votes Array: An array stores votes for each candidate.
  • Input: The user enters a candidate number or 0 to end voting.
  • castVote(): This method increments the vote count for the selected candidate.
  • displayResults(): Shows the total vote count for all candidates.
  • Voting Loop: Continues until the user enters 0.

Solution 2: Simple Voting System using a HashMap

This solution uses a HashMap to store candidate names and vote counts.

Code:

import java.util.HashMap;
import java.util.Scanner;

public class VotingSystemHashMap {
    // HashMap to store candidate names and vote counts
    private static HashMap votes = new HashMap<>();

    // Method to initialize candidates
    public static void initializeCandidates() {
        votes.put("Candidate 1", 0);
        votes.put("Candidate 2", 0);
        votes.put("Candidate 3", 0);
    }

    // Method to cast a vote
    public static void castVote(String candidate) {
        if (votes.containsKey(candidate)) {
            votes.put(candidate, votes.get(candidate) + 1);  // Increment vote count
            System.out.println("Vote cast for " + candidate);
        } else {
            System.out.println("Invalid candidate. Try again.");
        }
    }

    // Method to display the vote count
    public static void displayResults() {
        for (String candidate : votes.keySet()) {
            System.out.println(candidate + ": " + votes.get(candidate) + " votes");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean voting = true;
        initializeCandidates();  // Initialize candidates

        while (voting) {
            System.out.println("Enter the candidate name to vote for (Candidate 1-3), or 'exit' to end voting:");
            String input = scanner.nextLine();

            if (input.equalsIgnoreCase("exit")) {
                voting = false;  // End voting if input is 'exit'
            } else {
                castVote(input);  // Cast a vote
            }
        }

        System.out.println("Voting has ended. Final results:");
        displayResults();  // Display final results
        scanner.close();
    }
}  

Output:

Enter the candidate name to vote for (Candidate 1-3), or 'exit' to end voting:
 Candidate 1
Vote cast for Candidate 1
Enter the candidate name to vote for (Candidate 1-3), or 'exit' to end voting:
 Candidate 2
Vote cast for Candidate 2
Enter the candidate name to vote for (Candidate 1-3), or 'exit' to end voting:
 Candidate 1
Vote cast for Candidate 1
Enter the candidate name to vote for (Candidate 1-3), or 'exit' to end voting:
 Candidate 1
Vote cast for Candidate 1
Enter the candidate name to vote for (Candidate 1-3), or 'exit' to end voting:
 exit
Voting has ended. Final results:
Candidate 3: 0 votes
Candidate 2: 1 votes
Candidate 1: 3 votes

Explanation:

  • HashMap: Stores candidate names and vote counts.
  • initializeCandidates(): Initializes candidates with zero votes.
  • Input: The user enters a candidate name or exit to end voting.
  • castVote(): Increments the vote count for the selected candidate if valid.
  • displayResults(): Shows the total vote count for all candidates.
  • Voting Loop: Continues until the user enters exit.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/projects/java/java-project-simple-voting-system.php