-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnlineVotingSystemApp.java
213 lines (163 loc) · 9.39 KB
/
OnlineVotingSystemApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import com.onlinevotingsystem.candidate.Candidate;
import com.onlinevotingsystem.poll.Poll;
import com.onlinevotingsystem.registration.Voter;
import com.onlinevotingsystem.result.Result;
import com.onlinevotingsystem.db.DBConnection;
import com.onlinevotingsystem.db.DBInitializer;
import com.onlinevotingsystem.util.InputValidator;
import com.onlinevotingsystem.util.Logger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class OnlineVotingSystemApp {
public static void main(String[] args) {
Connection connection = null;
try {
// Initialize the database (create tables)
DBInitializer.initializeDatabase();
connection = DBConnection.getConnection();
Logger.log("Online Voting System Application Started");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Welcome to the Online Voting System");
System.out.println("1. Voter Registration");
System.out.println("2. Candidate Registration");
System.out.println("3. Create a Poll");
System.out.println("4. Record Election Results");
System.out.println("5. Exit");
System.out.print("Select an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (choice) {
case 1:
// Voter Registration
System.out.print("Enter voter's name: ");
String name = scanner.nextLine();
System.out.print("Enter voter's email: ");
String email = scanner.nextLine();
System.out.print("Enter voter's ID: ");
String voterID = scanner.nextLine();
System.out.print("Enter voter's address: ");
String address = scanner.nextLine();
System.out.print("Enter voter's date of birth: ");
String dateOfBirth = scanner.nextLine();
System.out.print("Enter security question: ");
String securityQuestion = scanner.nextLine();
System.out.print("Enter security answer: ");
String securityAnswer = scanner.nextLine();
if (InputValidator.isEmailValid(email)) {
Voter voter = new Voter(name, email, voterID, address, dateOfBirth,
securityQuestion, securityAnswer);
String insertQuery = "INSERT INTO voters (name, email, voter_id, address, " +
"date_of_birth, security_question, security_answer) VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setString(1, name);
preparedStatement.setString(2, email);
preparedStatement.setString(3, voterID);
preparedStatement.setString(4, address);
preparedStatement.setString(5, dateOfBirth);
preparedStatement.setString(6, securityQuestion);
preparedStatement.setString(7, securityAnswer);
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Data inserted successfully.");
} else {
System.out.println("Data insertion failed.");
}
Logger.log("Registered voter: " + voter.getName());
Logger.log("Voter mail: " + email);
Logger.log("Voter ID: " + voterID);
Logger.log("Date of birth: " + dateOfBirth);
} else {
System.out.println("Invalid email address. Registration failed.");
}
break;
case 2:
// Candidate Registration
System.out.print("Enter candidate's name: ");
String candidateName = scanner.nextLine();
System.out.print("Enter candidate's party: ");
String party = scanner.nextLine();
System.out.print("Enter candidate's platform: ");
String platform = scanner.nextLine();
Candidate candidate = new Candidate(candidateName, party, platform);
String insertCandidateQuery = "INSERT INTO candidates (name, party, platform) VALUES (?, ?, ?)";
PreparedStatement candidateStatement = connection.prepareStatement(insertCandidateQuery);
candidateStatement.setString(1, candidateName);
candidateStatement.setString(2, party);
candidateStatement.setString(3, platform);
int candidateRowsAffected = candidateStatement.executeUpdate();
if (candidateRowsAffected > 0) {
System.out.println("Candidate data inserted successfully.");
} else {
System.out.println("Candidate data insertion failed.");
}
Logger.log("Registered candidate: " + candidate.getName());
break;
case 3:
// Create a Poll
System.out.print("Enter poll question: ");
String question = scanner.nextLine();
System.out.print("Enter option 1: ");
String option1 = scanner.nextLine();
System.out.print("Enter option 2: ");
String option2 = scanner.nextLine();
Poll poll = new Poll(question, option1, option2);
String insertPollQuery = "INSERT INTO polls (question, option1, option2) VALUES (?, ?, ?)";
PreparedStatement pollStatement = connection.prepareStatement(insertPollQuery);
pollStatement.setString(1, question);
pollStatement.setString(2, option1);
pollStatement.setString(3, option2);
int pollRowsAffected = pollStatement.executeUpdate();
if (pollRowsAffected > 0) {
System.out.println("Poll data inserted successfully.");
} else {
System.out.println("Poll data insertion failed.");
}
Logger.log("Created a poll: " + poll.getQuestion());
break;
case 4:
// Record Election Results
System.out.print("Enter election name: ");
String electionName = scanner.nextLine();
System.out.print("Enter the name of the winning candidate: ");
String winnerName = scanner.nextLine();
System.out.print("Enter the number of votes for the winner: ");
int votesWinner = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
Result result = new Result(electionName, winnerName, votesWinner);
String insertResultQuery = "INSERT INTO results (election_name, winner_name, votes_winner) VALUES (?, ?, ?)";
PreparedStatement resultStatement = connection.prepareStatement(insertResultQuery);
resultStatement.setString(1, electionName);
resultStatement.setString(2, winnerName);
resultStatement.setInt(3, votesWinner);
int resultRowsAffected = resultStatement.executeUpdate();
if (resultRowsAffected > 0) {
System.out.println("Election results recorded successfully.");
} else {
System.out.println("Election results recording failed.");
}
Logger.log("Recorded election results for: " + result.getElectionName());
break;
case 5:
// Exit the application
Logger.log("Online Voting System Application Closed");
return;
default:
System.out.println("Invalid choice. Please select a valid option.");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}