// Election Management System with Multithreading & Exception Handling
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class User implements Serializable {
protected String name;
protected String id;
public User(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
}
class Candidate extends User {
private String party;
private String manifesto;
private int votes;
public Candidate(String name, String id, String party, String manifesto) {
super(name, id);
this.party = party;
this.manifesto = manifesto;
this.votes = 0;
}
public String getParty() {
return party;
}
public String getManifesto() {
return manifesto;
}
public int getVotes() {
return votes;
}
public void incrementVote() {
this.votes++;
}
@Override
public String toString() {
return name + "," + id + "," + party + "," + manifesto + "," + votes;
}
}
class Voter extends User {
private boolean hasVoted;
public Voter(String name, String id) {
super(name, id);
this.hasVoted = false;
}
public boolean hasVoted() {
return hasVoted;
}
public void setVoted() {
this.hasVoted = true;
}
@Override
public String toString() {
return name + "," + id + "," + hasVoted;
}
}
class FileHandler {
private static final String CANDIDATE_FILE = "Candidate.txt";
private static final String VOTER_FILE = "Voter.txt";
public static List<Candidate> readCandidates() {
List<Candidate> candidates = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(CANDIDATE_FILE))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
candidates.add(new Candidate(parts[0], parts[1], parts[2], parts[3]));
}
} catch (IOException e) {
System.out.println("Error reading candidates file.");
}
return candidates;
}
public static List<Voter> readVoters() {
List<Voter> voters = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(VOTER_FILE))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
Voter voter = new Voter(parts[0], parts[1]);
if (parts[2].equals("true")) voter.setVoted();
voters.add(voter);
}
} catch (IOException e) {
System.out.println("Error reading voter file.");
}
return voters;
}
public static void writeCandidates(List<Candidate> candidates) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(CANDIDATE_FILE))) {
for (Candidate c : candidates) {
bw.write(c.toString());
bw.newLine();
}
} catch (IOException e) {
System.out.println("Error writing candidates file.");
}
}
public static void writeVoters(List<Voter> voters) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(VOTER_FILE))) {
for (Voter v : voters) {
bw.write(v.toString());
bw.newLine();
}
} catch (IOException e) {
System.out.println("Error writing voters file.");
}
}
}
class VoteCounter extends Thread {
private List<Candidate> candidates;
public VoteCounter(List<Candidate> candidates) {
this.candidates = candidates;
}
public void run() {
while (true) {
System.out.println("Updating Vote Count...");
for (Candidate c : candidates) {
System.out.println(c.getName() + " - " + c.getVotes() + " votes");
}
try {
Thread.sleep(5000); // Update every 5 seconds
} catch (InterruptedException e) {
System.out.println("Vote Counter Interrupted!");
break;
}
}
}
}
class ElectionTimer extends Thread {
private int duration;
private Admin admin;
public ElectionTimer(int duration, Admin admin) {
this.duration = duration;
this.admin = admin;
}
public void run() {
try {
Thread.sleep(duration * 1000); // Convert seconds to milliseconds
admin.endElection();
} catch (InterruptedException e) {
System.out.println("Election Timer Interrupted!");
}
}
}
class Admin {
private List<Candidate> candidates;
private List<Voter> voters;
private boolean electionStarted = false;
private VoteCounter voteCounter;
public Admin() {
this.candidates = FileHandler.readCandidates();
this.voters = FileHandler.readVoters();
}
public void addCandidate(String name, String id, String party, String manifesto) {
candidates.add(new Candidate(name, id, party, manifesto));
FileHandler.writeCandidates(candidates);
}
public void addVoter(String name, String id) {
voters.add(new Voter(name, id));
FileHandler.writeVoters(voters);
}
public void startElection(int duration) {
electionStarted = true;
voteCounter = new VoteCounter(candidates);
voteCounter.start();
ElectionTimer timer = new ElectionTimer(duration, this);
timer.start();
System.out.println("Election Started!");
}
public void endElection() {
electionStarted = false;
if (voteCounter != null) voteCounter.interrupt();
Candidate winner = candidates.stream().max((c1, c2) -> c1.getVotes() - c2.getVotes()).orElse(null);
System.out.println("Election Ended. Winner: " + (winner != null ? winner.getName() : "No winner"));
}
}
public class ElectionSystem {
public static void main(String[] args) {
JFrame frame = new JFrame("Election Management System");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("Select Role:");
userLabel.setBounds(10, 20, 150, 25);
panel.add(userLabel);
String[] roles = {"Admin", "Candidate", "Voter"};
JComboBox<String> roleBox = new JComboBox<>(roles);
roleBox.setBounds(150, 20, 200, 25);
panel.add(roleBox);
JButton loginButton = new JButton("Login");
loginButton.setBounds(10, 80, 150, 25);
panel.add(loginButton);
loginButton.addActionListener(e -> {
String selectedRole = (String) roleBox.getSelectedItem();
JOptionPane.showMessageDialog(panel, "Redirecting to " + selectedRole + " Panel...");
});
}
}