Java Proect
Java Proect
: 10
DATE: 24-11-2024
Online Voting System
Aim:
To design and implement a secure online voting system that enables
authenticated users to participate in various polls, ensuring one vote per
user per poll, and provides real-time result updates as votes are cast.
Description:
This Online Voting System is a Java-based platform designed to
facilitate secure, transparent voting on various polls. Key features
include user authentication, controlled vote casting, and live result
updates. The main components of the system are:
1. User Authentication: Verifies user identity through a secure login
process. Passwords are hashed and validated to ensure security
and prevent unauthorized access. Only authenticated users are
permitted to access polls and cast votes.
2. Voting Process: Each authenticated user can vote once per poll.
The system checks for duplicate votes and validates each vote's
input, ensuring that users select only from available options within
each poll. If a user has already voted in a poll, the system prevents
additional voting attempts.
3. Real-time Results: As users cast their votes, the system instantly
updates the results, displaying a live tally of votes for each option.
This provides an engaging, transparent view of poll progress.
Constraints:
Input Constraints: Login credentials (username and password)
must be valid, secure, and follow set complexity standards. Poll
selections must match available options, and users cannot cast
multiple votes in the same poll.
Output: Successful authentication gives access to polls, while
failed attempts generate an error message. After voting, users
receive a confirmation of vote submission, and any duplicate
attempts are prevented with an error message. Results are
updated live, reflecting the current voting status.
Program:
import java.util.*;
while (true) {
System.out.println("Welcome to the Online Voting System");
System.out.println("1. Login");
System.out.println("2. Vote");
System.out.println("3. View Poll Results");
System.out.println("4. Logout");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1 -> login();
case 2 -> vote();
case 3 -> viewResults();
case 4 -> logout();
case 5 -> {
System.out.println("Exiting. Thank you for using the
system.");
System.exit(0);
}
default -> System.out.println("Invalid option. Try again.");
}
}
}
if (users.containsKey(username) &&
users.get(username).equals(password)) {
currentUser = username;
System.out.println("Login successful. Welcome, " + currentUser
+ "!");
} else {
System.out.println("Invalid credentials. Try again.");
}
}
if (!polls.containsKey(pollName)) {
System.out.println("Invalid poll name.");
return;
}
if (votedUsers.getOrDefault(pollName, new
HashSet<>()).contains(currentUser)) {
System.out.println("You have already voted in this poll.");
return;
}
System.out.println("Available Options:");
polls.get(pollName).keySet().forEach(System.out::println);
System.out.print("Enter your choice: ");
String choice = scanner.nextLine();
if (polls.get(pollName).containsKey(choice)) {
polls.get(pollName).put(choice, polls.get(pollName).get(choice) +
1);
votedUsers.computeIfAbsent(pollName, k -> new
HashSet<>()).add(currentUser);
System.out.println("Your vote has been recorded.");
} else {
System.out.println("Invalid choice.");
}
}
private static void viewResults() {
System.out.println("Available Polls:");
polls.keySet().forEach(System.out::println);
System.out.print("Enter poll name to view results: ");
String pollName = scanner.nextLine();
if (!polls.containsKey(pollName)) {
System.out.println("Invalid poll name.");
return;
}
System.out.println("Results for " + pollName + ":");
polls.get(pollName).forEach((option, votes) ->
System.out.println(option + ": " + votes + " votes"));
}
Output:
Result:
The program is successfully written, compiled, and executed. It performs
all intended functionalities, such as user login, vote casting, and viewing
poll results in real-time. The output demonstrates the system's capability
to handle user authentication, manage polls, and provide accurate vote
counts efficiently.