In: Computer Science
Create JAVA PROGRAM, and write comment for codes also.
1) Let the user enter in candidate names for a ballot. Then imagine the program is moved to a voting booth. Each voter enters their unique name and who they vote for, and when there are no more voters display who won. (Imagine something outside your program is telling it there are no more voters.)
import java.util.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("please enter names of all candidates for
election (seperated by comma)");
String[] names = br.readLine().trim().split(",");
Map<String , Integer> hm = new HashMap<>(); //// hasmap
to store candidate name and vote count for them
int size = names.length;
/// storing candiadte name with initial vote count as 0
for(int i = 0; i<size;i++)
{
hm.put(names[i].toLowerCase().trim(), 0);
}
////////// Moving to polling booth //////////////
System.out.println("------POLLING BOOTH------");
boolean isThereAVoter = false; //// variable which indicates
presence of a voter
System.out.println("Is there any voter?"); /// intraction with
outsider for first time
String outsiders_reply = br.readLine().trim(); /// listening reply
of outsider
if(outsiders_reply.toLowerCase().equals("yes"))
{
isThereAVoter = true;
while(isThereAVoter)
{
////displaying ballot paper ////
System.out.println("--Ballot Paper--");
for(String s : names)
{
System.out.println(s);
}
System.out.println();
System.out.println();
///// taking voter name and candiadte name whom he/she wants to
vote for
System.out.print("Sir , Please Enter your name : ");
br.readLine().trim();
System.out.print("Sir, Please enter your choice of candidate :
");
String candidate_choice = br.readLine().trim().toLowerCase();
////// Checking the validating of entered candidate name
// if valid candidate name , then
if(hm.containsKey(candidate_choice))
{
///// adding voter's vote into the corresponding candiadte's vote
count
hm.put(candidate_choice, hm.get(candidate_choice) + 1);
System.out.println("Sucessfullly voted!");
System.out.println();
System.out.println();
///// telling outsider to send next voter
System.out.println("Hey outsider!, is there any voter
left?");
outsiders_reply = br.readLine().trim();
if(outsiders_reply.toLowerCase().equals("yes"))
{
System.out.println("Please send inside.");
}
else ///// if no voter is left... displaying the winner
{
isThereAVoter = false; ///s setting presence of voter to false as
there is no voter left
/////// getting the winner from the hasmap in which the candiadte
name and corresponing vote count is stored
Set<Map.Entry<String, Integer>> set =
hm.entrySet();
Iterator itr = set.iterator();
String winner = "";
int max_vote = 0;
while(itr.hasNext())
{
Map.Entry<String, Integer> e = (Map.Entry<String,
Integer>)itr.next();
if(e.getValue() > max_vote)
{
winner = e.getKey();
max_vote = e.getValue();
}
}
/////// announcement for the winner
System.out.println("Winnner is ------ " + winner);
}
}
////// if name of candidate enterd by voter is not present in
ballot paper, then
else
{
System.out.println();
System.out.println("Please enter a candidate name from the above
list");
System.out.println();
}
}
}
////// if no voter came to vote
else{
System.out.println("No over is intrested to vote, Hence they didn't
come");
}
}
}