import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application that is intended to make use of the Election class ** for the purpose of testing its features. ** ** In summary, this program reads partial election results from one ** "vote-counts" file, processes the ballots described in another file, ** and then writes the updated (possibly still partial) election results ** to another "vote-counts" file. (The two "vote-counts" files could be ** the same file, in which case its original contents will be overwritten.) ** ** The names of the three files (minus their assumed ".txt" extensions) ** must be passed to this program via command-line arguments. ** ** The two vote-counts files begin with a record indicating the number ** of candidates. Each subsequent record contains the name of a ** candidate, optionally followed by a colon and a vote count. (If the ** colon and vote count are absent, the intent is that the vote count ** is zero.) ** ** Each record in the ballots file is the name of a candidate and ** represents a vote for that candidate. ** ** Author: R. McCloskey ** Date: November 2024 */ public class ElectionApp { public static void main(String[] args) throws FileNotFoundException { // Assign the three command line arguments, which name the two // input files and the output file, to variables with meaningful names. String voteCountsBeforeFileName = args[0] + ".txt"; String ballotsFileName = args[1] + ".txt"; String voteCountsAfterFileName = args[2] + ".txt"; // Create an Election object from the input vote-counts file, Election election = new Election(voteCountsBeforeFileName); // Process the ballots in the ballots file. Scanner ballotsInput = new Scanner(new File(ballotsFileName)); while (ballotsInput.hasNextLine()) { String s = ballotsInput.nextLine().trim(); System.out.print("Processing vote for " + s); if (election.recordVoteFor(s)) { System.out.println("; vote count: " + election.numVotesFor(s) + "; rank: " + election.rankOf(s)); } else { System.out.println(": not counted (no such candidate)"); }; } // Print the updated election standings. System.out.println("\nElection standings:"); election.displayStandings(); // Write the updated election standings to the output vote-counts // file, which could possibly be used as input during a later run of // this program when more ballots are available to be processed. election.writeToFile(voteCountsAfterFileName); } }