Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- int max = 0, counter = 0, winner = 0;
- String name;
- List<String> candidate_Name = new ArrayList<>();
- List<Integer> votes = new ArrayList<>();
- Scanner input = new Scanner(System.in);
- System.out.println("###################################################");
- System.out.println("# Enter the votes, one vote per line. #");
- System.out.println("# End with either -1 or an empty line. #");
- System.out.println("###################################################");
- while (true) {
- name = input.nextLine();
- //break point of the program
- if (name.equals("-1") || name.equals("")) {
- break;
- }
- //Check if there is exist a candidate name if not add
- if (!candidate_Name.contains(name)) {
- candidate_Name.add(name);
- votes.add(1);
- continue;
- }
- //Handle the vote Count
- int index = candidate_Name.indexOf(name);
- votes.set(index, votes.get(index) + 1);
- }
- //display all candidate names with votes
- System.out.println("\n");
- for (int i = 0; i < candidate_Name.size(); i++) {
- if (votes.get(i) > max) {
- max = votes.get(i);
- winner = i;
- }
- System.out.println(candidate_Name.get(i) + " recieved " + votes.get(i) + " votes");
- }
- System.out.println("----------------------------");
- for (int i = 0; i < candidate_Name.size(); i++) {
- if (max == votes.get((i))) {
- counter += 1;
- }
- }
- if (counter == 1) {
- System.out.println("The Winner is " + candidate_Name.get(winner) + " with " + votes.get(winner) + " votes");
- } else {
- System.out.println("Its draw");
- System.out.println("Press Enter for re-election or any key to terminate the program.....");
- String command = input.nextLine();
- if (command.equals("")){
- }
- else{
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement