0% found this document useful (0 votes)
6 views3 pages

Voting System

Uploaded by

Safreena Rafeek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Voting System

Uploaded by

Safreena Rafeek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Online Voting System

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

public class VotingSystem extends JFrame implements ActionListener {

// Create a map to hold vote counts for each candidate


private Map votes;
private JRadioButton candidateA;
private JRadioButton candidateB;
private JRadioButton candidateC;
private JRadioButton candidateD;
private ButtonGroup candidateGroup;
private JButton submitButton;
private JButton resultsButton;

public VotingSystem() {
// Initialize the votes map
votes = new HashMap<>();
votes.put("Candidate A", 0);
votes.put("Candidate B", 0);
votes.put("Candidate C", 0);
votes.put("Candidate D", 0);

// Set up the GUI


setTitle("Online Voting System");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

// Create and add components


JLabel titleLabel = new JLabel("Vote for Your Candidate");
titleLabel.setFont(new Font("Arial", Font.BOLD, 18));
add(titleLabel);

// Create radio buttons for each candidate


candidateA = new JRadioButton("Candidate A");
candidateB = new JRadioButton("Candidate B");
candidateC = new JRadioButton("Candidate C");
candidateD = new JRadioButton("Candidate D");
candidateGroup.add(candidateA);
candidateGroup.add(candidateB);
candidateGroup.add(candidateC);
candidateGroup.add(candidateD);

// Add radio buttons to the frame


add(candidateA);
add(candidateB);
add(candidateC);
add(candidateD);

// Submit button to cast a vote


submitButton = new JButton("Submit Vote");
submitButton.addActionListener(this);
add(submitButton);

// Results button to show vote counts


resultsButton = new JButton("Show Results");
resultsButton.addActionListener(this);
add(resultsButton);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submitButton) {
String selectedCandidate = null;

// Check which candidate is selected


if (candidateA.isSelected()) {
selectedCandidate = "Candidate A";
} else if (candidateB.isSelected()) {
selectedCandidate = "Candidate B";
} else if (candidateC.isSelected()) {
selectedCandidate = "Candidate C";
} else if (candidateD.isSelected()) {
selectedCandidate = "Candidate D";
}

// If a candidate is selected, record the vote


if (selectedCandidate != null) {
votes.put(selectedCandidate, votes.get(selectedCandidate) + 1);
JOptionPane.showMessageDialog(this, "Your vote for " + selectedCandidate + " has been recorded!");
candidateGroup.clearSelection(); // Clear selection for next vote
} else {
JOptionPane.showMessageDialog(this, "Please select a candidate to vote for.");
}

} else if (e.getSource() == resultsButton) {


// Display voting results
}
JOptionPane.showMessageDialog(this, resultMessage.toString());
}
}

public static void main(String[] args) {


// Create and display the voting system GUI
VotingSystem votingSystem = new VotingSystem();
votingSystem.setVisible(true);
}
}

You might also like