import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; /* An instance of this class is for the purpose of processing, tabulating, ** and recording the votes in an election, and for updating a file that ** contains the (partial) results of that election. ** Author: R. McCloskey */ public class Election { // instance variable // ----------------- // Each element of the candidates[] array stores a candidate's ID/name // and vote count (in the form of an instance of the Candidate class, // which is a static inner class within this class). The array // elements are maintained to be in descending order by vote count. private Candidate[] candidates; // constructor // ----------- /* The argument is assumed to be the name of a "candidates" file in ** which is stored the partial results of an election. The first ** line of the file is assumed to contain an integer indicating the ** number of candidates on the ballot; each of the remaining lines ** contains the ID/name of a candidate, optionally followed by a colon ** and the number of votes already received by that candidate. */ public Election(String fileName) throws FileNotFoundException { // Establish a Scanner object to read from the named file: // << MISSING CODE >> // Read the # of candidates and then skip to the next line: // << MISSING CODE >> // Establish the candidates[] array to be of adequate size for holding // the Candidate objects representing the candidates competing in // this election: // << MISSING CODE >> // Read the records describing the candidates and use them to populate // the candidates[] array. Note that the constructor of the Candidate // class handles records of both allowed formats described above. // << MISSING CODE >> } // observers // --------- /* Returns the number of votes that have been recorded for the candidate ** with the given name, or zero if there is no such candidate. */ public int numVotesFor(String name) { return -1; // STUB } /* Returns the rank of the candidate with the given name, which is ** one more than the number of candidates having higher vote counts. ** If there is no candidate of the given name, -1 is returned. */ public int rankOf(String name) { return -2; // STUB } // mutators // -------- /* If a candidate with the given name exists, a vote for that candidate ** is recorded and true is returned. Otherwise, no vote is recorded ** and false is returned. */ public boolean recordVoteFor(String name) { return false; // STUB } /* Prints the current standings of the election; each line of output ** is of the form ". :" showing the rank, name, ** and vote count of a candidate. (E.g., "1. Lincoln:24567"). Ranks ** are numbered starting at 1. The candidates are listed in descending ** order of vote counts. */ public void displayStandings() { System.out.println("Election Standings belong here!"); // STUB } /* Writes the current standings of the election to the file with ** the given name. */ public void writeToFile(String fileName) throws FileNotFoundException { PrintWriter pw = new PrintWriter(new File(fileName)); // On the first line of the file, write the # of candidates. pw.println(candidates.length); // On each subsequent line of the file, write the toString() // image of a candidate. for (Candidate cand : candidates) { pw.println(cand); } pw.close(); } // helper methods // -------------- /* If there exists a candidate with the given name, the index of ** that candidate within candidates[] is returned. Otherwise, -1 ** is returned. */ private int indexOf(String name) { return -2; // STUB } /* Adjusts the candidates[] array to remain in decsending order of ** votes received, under the assumption that the candidate at the ** given location may need to move up. */ private void adjustCandidates(int k) { // STUB } /* Swaps the elements at locations k-1 and k of candidates[]. */ private void swapWithPredecessor(int k) { Candidate temp = candidates[k]; candidates[k] = candidates[k-1]; candidates[k-1] = temp; } // static inner class // ------------------ /* An instance of this "static inner" class is an ordered pair ** whose components are a String (e.g., representing the name of a ** candidate) and a count (representing # of votes received by that ** candidate). */ private static class Candidate { // instance variables // ------------------ public String name; public int voteCount; // constructor // ----------- /* Establishes this Candidate object to have the name and vote count ** indicated by the given string, which is assumed to be either of ** the form : or just . In the former case, ** the vote count is explicitly indicated; in the former, it is ** implicitly zero. ** E.g., "Lincoln:23456" or "Lincoln" */ public Candidate(String descriptor) { int posOfColon = descriptor.indexOf(':'); if (posOfColon == -1) { // no colon, so only name is given this.name = descriptor; this.voteCount = 0; } else { // colon is present, so both name and vote count are given this.name = descriptor.substring(0,posOfColon).trim(); String voteCountStr = descriptor.substring(posOfColon+1).trim(); this.voteCount = Integer.parseInt(voteCountStr); } } // observer // -------- /* Returns a string displaying this Candidate's name and vote ** count, separated by a colon. */ public String toString() { return name + ':' + voteCount; } } // end of static inner Candidate class }