/* ElectionClient.java ** ** Java application that accepts the name of an "election file" as its first ** run argument and uses its contents as input in simulating the process of ** establishing a ballot of candidates and then counting votes cast for ** them. An integer imposing a limit on the number of candidates for whom ** votes can be cast may be provided (optionally) as the second run argument. ** (Otherwise, a default limit applies.) ** ** The first line in an election file contains the title of the election ** (typically identifying the office for which the candidates are vying), ** and the second line contains the names of the candidates as a CSV string. ** Each subsequent line contains a name and represents a vote cast for the ** candidate with that name. An exception to this is that a blank line ** indicates that the election has closed, meaning that any subsequent ** votes are considered to have been cast afterwards (and thus should not ** be counted). ** ** CMPS 134 Fall 2019 Prog. Assg. #7 ** Authors: P.M.J & R.W.M. & ... ** ** Collaboration: ** ** Known defects: ... */ import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class ElectionClient { private static Scanner keyboard; // to read input entered by user private static Scanner input; // to read data from input file public static void main(String[] args) throws FileNotFoundException { System.out.print("Election Client ..."); // Establish a Scanner to read from the file with the specified name. String fileName = getFirstArg(args); input = new Scanner(new File(fileName)); // Read first line to get election title String title = input.nextLine().trim(); // Read second line to get election candidate list String candidateList = input.nextLine().trim(); // Declare variable Election election; // Construct object based on optional run argument if(args.length > 1) { int candLimit = Integer.parseInt(args[1]); System.out.printf(" (limited to %d candidates)", candLimit); election = new Election(candLimit); } else { election = new Election(); } System.out.println(); // Add candidates to the election addCandidates(election, candidateList); printElectionStatus(title, election); // Open voting election.openVoting(); System.out.println("Voting Opened .................."); printElectionStatus(title, election); // Now process the votes. while (input.hasNextLine()) { try { String vote = input.nextLine().trim(); if(vote.length() == 0) { election.closeVoting(); // Close voting System.out.println("Voting Closed .................."); } else { election.castVote(vote); } printElectionStatus(title, election); } catch (Exception e) { System.out.println(e + "\nContinuing ...\n"); } } // Print final results System.out.println("Final Results:"); printElectionStatus(title, election); System.out.println("\nDone!"); } private static final String COLON = ":"; private static final String BLANK = " "; /* Prints the given title followed by the current status of the given ** election. */ private static void printElectionStatus(String title, Election election) { System.out.println(title + COLON + BLANK + election.toString()); } /* If the given array has at least one element, the first such element ** (i.e., the one at location zero) is returned. Otherwise, the user ** is prompted to enter a string (intended to be the name of a file ** to be used as input) and the response is returned. */ private static String getFirstArg(String args[]) { String result = ""; if(args.length > 0) { result = args[0]; } else { result = getStringFromUser("Enter name of input file: ", new Scanner(System.in)); } return result; } /* Prints the given prompt and reads/returns a line of data read ** using the given Scanner. */ private static String getStringFromUser(String prompt, Scanner keyboard) { System.out.print(prompt); return keyboard.nextLine(); } /* Adds each of the candidates appearing in the given list to the ** given election. The given list is assumed to be in CSV form ** (i.e., the names of the candidates are separated by commas). */ private static void addCandidates(Election election, String list) { Scanner scanner = new Scanner(list); scanner.useDelimiter(","); while(scanner.hasNext()) { String candidate = scanner.next().trim(); try { election.addCandidate(candidate); } catch (Exception e) { System.out.println(e + "\nContinuing ...\n"); } } } }