0% found this document useful (0 votes)
24 views8 pages

Java Proect

Java program for future use

Uploaded by

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

Java Proect

Java program for future use

Uploaded by

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

EX NO.

: 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.*;

public class OnlineVotingSystem {


private static Map<String, String> users = new HashMap<>();
private static Map<String, Map<String, Integer>> polls = new
HashMap<>();
private static Map<String, Set<String>> votedUsers = new
HashMap<>();
private static Scanner scanner = new Scanner(System.in);
private static String currentUser = null;

public static void main(String[] args) {


setupUsers();
setupPolls();

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.");
}
}
}

private static void setupUsers() {


users.put("user1", "password1");
users.put("user2", "password2");
}

private static void setupPolls() {


Map<String, Integer> poll1 = new HashMap<>();
poll1.put("Option A", 0);
poll1.put("Option B", 0);
polls.put("Poll1", poll1);
}
private static void login() {
if (currentUser != null) {
System.out.println("You are already logged in as " +
currentUser);
return;
}
System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();

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.");
}
}

private static void vote() {


if (currentUser == null) {
System.out.println("Please log in to vote.");
return;
}
System.out.println("Available Polls:");
polls.keySet().forEach(System.out::println);
System.out.print("Enter poll name to vote: ");
String pollName = scanner.nextLine();

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"));
}

private static void logout() {


if (currentUser == null) {
System.out.println("No user is currently logged in.");
} else {
System.out.println("Logging out " + currentUser + ".");
currentUser = null;
}
}
}

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.

You might also like