From 3a510fe9c4293f9d3b5184083f39f60ed88e5f1b Mon Sep 17 00:00:00 2001 From: Dan Nichols Date: Sat, 3 May 2025 23:24:04 -0400 Subject: [PATCH 1/2] refactor but i gave up --- src/App.java | 226 ++++++++++++++++++++++++++++-------------------- src/Ballot.java | 81 +++++++---------- 2 files changed, 160 insertions(+), 147 deletions(-) diff --git a/src/App.java b/src/App.java index 26efdbd..1209878 100644 --- a/src/App.java +++ b/src/App.java @@ -5,7 +5,6 @@ import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.Map; import java.util.TreeMap; import java.util.AbstractMap.SimpleEntry; @@ -14,13 +13,17 @@ public final class App { private App() { } - private static TreeMap ballots= new TreeMap<>(); - public static TreeMap>> allVotes = new TreeMap<>();//first string is election, second is candidate + + private static TreeMap ballots = new TreeMap<>(); + public static TreeMap>> allVotes = new TreeMap<>();// first string is + // election, second is + // candidate public static ArrayList importantElections = new ArrayList<>(); - public static TreeMap winners = new TreeMap<>(); + public static TreeMap winners = new TreeMap<>(); public static String[] columnHeadings; public static PrintWriter writer; - public static void main(String[] args) throws IOException { + + public static void main(String[] args) throws Exception { System.out.println("Looking in " + System.getProperty("user.dir")+" for voteTallies.csv"); BufferedReader reader; @@ -43,15 +46,15 @@ public static void main(String[] args) throws IOException { if(heading.contains("Timestamp")||heading.contains("Email Address")||heading.contains("Full Name")){ continue; } - String position = getElectionNameFromHeading(heading);//remove first/second/third + String election = getElectionNameFromHeading(heading);//remove first/second/third writer.println("Found column "+heading); - if(!allVotes.containsKey(position)){ - allVotes.put(position, new TreeMap<>()); - writer.println("Created election "+position); - if(position.contains("Captain")){ - importantElections.add(position); - writer.println("Important position "+position+" noted"); + if(!allVotes.containsKey(election)){ + allVotes.put(election, new TreeMap<>()); + writer.println("Created election "+election); + if(election.contains("Captain")){ + importantElections.add(election); + writer.println("Important election "+election+" noted"); } } } @@ -69,6 +72,10 @@ public static void main(String[] args) throws IOException { for(String importantName:importantElections){ runElection(importantName); System.out.println("Running election "+importantName); + if (winners.containsKey(importantName)) { // a winner has been found so they need to be removed from the list of eligible candidates + allVotes.keySet().forEach((electionName) -> {deregisterCandidate(electionName, winners.get(importantName));}); + + } } for(Map.Entry>> elections:allVotes.entrySet()){ @@ -86,83 +93,87 @@ public static void main(String[] args) throws IOException { writer.flush(); System.out.println("Done"); - } - public static String getElectionNameFromHeading(String heading){ - return(heading.replaceAll(" \\[.*", "")); } - public static void runElection(String election) throws IOException{ - writer.println("Running election "+election); + public static String getElectionNameFromHeading(String heading) { + return (heading.replaceAll(" \\[.*", "")); + } - ballots.forEach((idx,ballot)->{try { - ballot.cast(election); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - }}); + public static void runElection(String election) throws IOException { + writer.println("Running election " + election); + + ballots.forEach((idx, ballot) -> { + try { + ballot.cast(election); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + }); writer.println("Initial casting complete"); printElectionStandings(election); - for(int round=0;round<=10;round++){ - int numVotes=getNumberOfVotesInElection(election); - writer.println(numVotes+" votes in election "+election); - SimpleEntry currentLeader=getCurrentLeader(election); - if(currentLeader.getValue()>numVotes/2 && currentLeader.getKey()!=null){ - writer.println("Winner found. Winner is "+currentLeader.getKey()+" with "+currentLeader.getValue()+" votes."); + for (int round = 0; round <= 10; round++) { + int numVotes = getNumberOfVotesInElection(election); + writer.println(numVotes + " votes in election " + election); + SimpleEntry currentLeader = getCurrentLeader(election); + if (currentLeader.getValue() > numVotes / 2 && currentLeader.getKey() != null) { + writer.println("Winner found. Winner is " + currentLeader.getKey() + " with " + currentLeader.getValue() + + " votes."); winners.put(election, currentLeader.getKey()); break; } - int numberOfCandidatesBeforeRemoval=allVotes.get(election).size(); - SimpleEntry currentLoser=getCurrentLoser(election); - if(currentLoser.getKey()==null){ + int numberOfCandidatesBeforeRemoval = allVotes.get(election).size(); + SimpleEntry currentLoser = getCurrentLoser(election); + if (currentLoser.getKey() == null) { writer.println("Unbroken tie for the lose. See standings and resolve manually."); break; } ArrayList votesForLoser = allVotes.get(election).get(currentLoser.getKey()); allVotes.get(election).remove(currentLoser.getKey()); - writer.println("Candidate "+currentLoser.getKey()+" removed."); + writer.println("Candidate " + currentLoser.getKey() + " removed."); - for(Ballot ballotForLoser:votesForLoser){ //now they are eliminated remove those that voted for them + for (Ballot ballotForLoser : votesForLoser) { // now they are eliminated remove those that voted for them ballotForLoser.cast(election); } - if(allVotes.get(election).size()>=numberOfCandidatesBeforeRemoval){ - System.err.println("Recast all votes for "+currentLoser+" but now there are more candidates"); + if (allVotes.get(election).size() >= numberOfCandidatesBeforeRemoval) { + System.err.println("Recast all votes for " + currentLoser + " but now there are more candidates"); System.exit(-5); } - writer.println("Starting round "+(round+2)); + writer.println("Starting round " + (round + 2)); printElectionStandings(election); } - writer.println("Finished election "+election); + writer.println("Finished election " + election); } - - public static ArrayList>> getSortedStandings(String election){ - ArrayList>> currentStandings=new ArrayList<>(allVotes.get(election).entrySet()); - currentStandings.sort(Entry.comparingByValue((ArrayList o1, ArrayList o2)-> { - return (int)Math.signum(o2.size()-o1.size()); + public static ArrayList>> getSortedStandings(String election) { + ArrayList>> currentStandings = new ArrayList<>( + allVotes.get(election).entrySet()); + currentStandings.sort(Entry.comparingByValue((ArrayList o1, ArrayList o2) -> { + return (int) Math.signum(o2.size() - o1.size()); })); return currentStandings; } public static void printElectionStandings(String election) { - writer.println("Standings of election "+election); - for(Map.Entry> candidate:getSortedStandings(election)){ - String line=" "+candidate.getValue().size()+": "+candidate.getKey(); + writer.println("Standings of election " + election); + for (Map.Entry> candidate : getSortedStandings(election)) { + String line = " " + candidate.getValue().size() + ": " + candidate.getKey(); writer.print(line); - for(int i=0;i<40-line.length();i++){ + for (int i = 0; i < 40 - line.length(); i++) { writer.print(" "); } writer.print("|"); - int chunkVotes=candidate.getValue().size()*20/getNumberOfVotesInElection(election); - for(int i=0;i=10) + int chunkVotes = candidate.getValue().size() * 20 / getNumberOfVotesInElection(election); + for (int i = 0; i < chunkVotes; i++) { + if (i >= 10) writer.print("*"); else writer.print("#"); } - for(int i=0;i<20-chunkVotes;i++){ + for (int i = 0; i < 20 - chunkVotes; i++) { writer.print(" "); } writer.println("|"); @@ -170,70 +181,95 @@ public static void printElectionStandings(String election) { } public static int getNumberOfVotesInElection(String election) { - TreeMap> votes = allVotes.get(election); - int numTotalVotes=0; - for (Map.Entry> candidates : votes.entrySet()) { - numTotalVotes+=candidates.getValue().size(); + TreeMap> votes = allVotes.get(election); + int numTotalVotes = 0; + for (Map.Entry> candidates : votes.entrySet()) { + numTotalVotes += candidates.getValue().size(); } return numTotalVotes; } - public static SimpleEntry getCurrentLeader(String election){ - TreeMap> votes = allVotes.get(election); - String potentialCurrentLeader=null; - Integer potentialLeaderVotes=0; - for (Map.Entry> candidates : votes.entrySet()) { - Integer votesForThisCandidate=candidates.getValue().size(); - if(votesForThisCandidate.equals(potentialLeaderVotes)){ - potentialCurrentLeader=null;//tied in the lead - }else if(votesForThisCandidate>potentialLeaderVotes){ - potentialCurrentLeader=candidates.getKey(); - potentialLeaderVotes=votesForThisCandidate; + public static SimpleEntry getCurrentLeader(String election) { + TreeMap> votes = allVotes.get(election); + String potentialCurrentLeader = null; + Integer potentialLeaderVotes = 0; + for (Map.Entry> candidates : votes.entrySet()) { + Integer votesForThisCandidate = candidates.getValue().size(); + if (votesForThisCandidate.equals(potentialLeaderVotes)) { + potentialCurrentLeader = null;// tied in the lead + } else if (votesForThisCandidate > potentialLeaderVotes) { + potentialCurrentLeader = candidates.getKey(); + potentialLeaderVotes = votesForThisCandidate; } } - return new SimpleEntry<>(potentialCurrentLeader,potentialLeaderVotes); + return new SimpleEntry<>(potentialCurrentLeader, potentialLeaderVotes); } - public static int getNextWorstNumberOfVotes(String election){ - ArrayList>> currentStandings=getSortedStandings(election); - int minVotes=currentStandings.get(currentStandings.size()-1).getValue().size(); - if(currentStandings.size()<2){ + + public static int getNextWorstNumberOfVotes(String election) { + ArrayList>> currentStandings = getSortedStandings(election); + int minVotes = currentStandings.get(currentStandings.size() - 1).getValue().size(); + if (currentStandings.size() < 2) { return 1; } - for(int i=currentStandings.size()-2;i>=0;i--){ - int thisNumVotes=currentStandings.get(i).getValue().size(); - if(thisNumVotes>minVotes){ + for (int i = currentStandings.size() - 2; i >= 0; i--) { + int thisNumVotes = currentStandings.get(i).getValue().size(); + if (thisNumVotes > minVotes) { return thisNumVotes; } } return 1; } - public static SimpleEntry getCurrentLoser(String election) { - TreeMap> votes = allVotes.get(election); - String potentialCurrentLoser=null; - Integer potentialLoserVotes=9999; - boolean tie=false; - for (Map.Entry> candidates : votes.entrySet()) { - Integer votesForThisCandidate=candidates.getValue().size(); - if(votesForThisCandidate.equals(potentialLoserVotes)){ - tie=true; - //tied in the loss, the first randomly chosen person is still the loser - }else if(votesForThisCandidate getCurrentLoser(String election) { + TreeMap> votes = allVotes.get(election); + String potentialCurrentLoser = null; + Integer potentialLoserVotes = 9999; + boolean tie = false; + for (Map.Entry> candidates : votes.entrySet()) { + Integer votesForThisCandidate = candidates.getValue().size(); + if (votesForThisCandidate.equals(potentialLoserVotes)) { + tie = true; + // tied in the loss, the first randomly chosen person is still the loser + } else if (votesForThisCandidate < potentialLoserVotes) { + potentialCurrentLoser = candidates.getKey(); + potentialLoserVotes = votesForThisCandidate; + tie = false; } } - if(tie){ - if(potentialLoserVotes*2>getNextWorstNumberOfVotes(election)){ - //just eliminate first tied candidate potentialCurrentLoser=null;//no one can be eliminated simply - }else{ - writer.println("Two candidates tied in the loss of election "+election+" but neither are important. Candidate "+potentialCurrentLoser+" will be eliminated."); + if (tie) { + if (potentialLoserVotes * 2 > getNextWorstNumberOfVotes(election)) { + // just eliminate first tied candidate potentialCurrentLoser=null;//no one can + // be eliminated simply + } else { + writer.println("Two candidates tied in the loss of election " + election + + " but neither are important. Candidate " + potentialCurrentLoser + " will be eliminated."); } } - - return new SimpleEntry<>(potentialCurrentLoser,potentialLoserVotes); + return new SimpleEntry<>(potentialCurrentLoser, potentialLoserVotes); + } + + public static void registerCandidate(String electionName, String candidate) { + TreeMap> candidatesForThisElection = allVotes.get(electionName); + if (!candidatesForThisElection.containsKey(candidate)) {// candidate not in list for this election + candidatesForThisElection.put(candidate, new ArrayList<>()); + } + } + + public static void deregisterCandidate(String electionName, String candidate) throws Exception { + TreeMap> candidatesVotesForThisElection = allVotes.get(electionName); + if (candidatesVotesForThisElection.get(candidate).size() > 0) { + throw new Exception("Trying to deregister candidate " + candidate + " in election " + electionName + + " but they have votes!"); + } } + public static boolean isCandidateAvailable(String electionName, String candidate) { + if (!App.allVotes.get(electionName).containsKey(candidate)) { + return false; + } + + return true; + } } \ No newline at end of file diff --git a/src/Ballot.java b/src/Ballot.java index 6071f89..95b10e2 100644 --- a/src/Ballot.java +++ b/src/Ballot.java @@ -1,38 +1,35 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; import java.util.Map; +import java.util.Queue; import java.util.TreeMap; -import java.util.AbstractMap.SimpleEntry; public final class Ballot { - private TreeMap> votes = new TreeMap<>();//ballotData - private int ballotIndex; - private TreeMap currentChoiceIdxs = new TreeMap<>(); + private TreeMap> mVotes = new TreeMap<>();//ballotData + private int mBallotIndex; public Ballot(String ballotLine, int idx) { - ballotIndex = idx; - String[] voteNames = ballotLine.split(",");//chosenCandidates - if (voteNames.length != App.columnHeadings.length) { + mBallotIndex = idx; + String[] ballotEntries = ballotLine.split(",");//chosenCandidates + if (ballotEntries.length != App.columnHeadings.length) { System.err.println("Length of ballot " + ballotLine + " doesn't match number of elections!"); System.exit(-1); } - for (int i = 3; i < voteNames.length; i++) {// start at 3 to skip timestamp, email, name - String thisColumString = App.columnHeadings[i];//thisColumnString - String thisElection = App.getElectionNameFromHeading(thisColumString); - String thisVote = voteNames[i];//chosenCandidate + for (int columnIdx = 3; columnIdx < ballotEntries.length; columnIdx++) {// start at 3 to skip timestamp, email, name + String thisColumnHeading = App.columnHeadings[columnIdx];//thisColumnString + String thisElectionName = App.getElectionNameFromHeading(thisColumnHeading); + String thisChoice = ballotEntries[columnIdx];//chosenCandidate - if (!votes.containsKey(thisElection)) {// no votes yet by this ballot for this election - votes.put(thisElection, new ArrayList<>()); + if (!mVotes.containsKey(thisElectionName)) {// no votes yet by this ballot for this election + mVotes.put(thisElectionName, new LinkedList<>()); } - if(thisVote.length()>0){ - TreeMap> candidatesForThisElection = App.allVotes.get(thisElection); - if (!candidatesForThisElection.containsKey(thisVote)) {// candidate not in list for this election - candidatesForThisElection.put(thisVote, new ArrayList<>()); - } - ArrayList thisBallotVotesForThisElection = votes.get(thisElection); - thisBallotVotesForThisElection.add(thisVote);// better be in order + if(thisChoice.length()>0){ + App.registerCandidate(thisElectionName,thisChoice); + mVotes.get(thisElectionName).add(thisChoice);// ballot must have numbered choices in order (choice 1 in column D, 2 in E, etc.) } } } @@ -40,61 +37,41 @@ public Ballot(String ballotLine, int idx) { @Override public String toString() { StringBuilder rBuilder = new StringBuilder(); - for (Map.Entry> thisElectionChoices : votes.entrySet()) { + for (Map.Entry> thisElectionChoices : mVotes.entrySet()) { rBuilder.append("Election " + thisElectionChoices.getKey() + ":"); - for (int i = 0; i < thisElectionChoices.getValue().size(); i++) { - rBuilder.append(" #" + i + "," + thisElectionChoices.getValue().get(i) + "; "); + List listOfChoices = new ArrayList<>(thisElectionChoices.getValue()); + for (int i = 0; i < listOfChoices.size(); i++) { + rBuilder.append(" #" + i + "," + listOfChoices.get(i) + "; "); } } return (rBuilder.toString()); } public boolean cast(String election) throws IOException { - if (!currentChoiceIdxs.containsKey(election)) { - currentChoiceIdxs.put(election, -1); - } - int choice = currentChoiceIdxs.get(election) + 1; - - if (votes.get(election).size() <= choice) { + if (mVotes.get(election).size() == 0) { App.writer.println( - "Ballot " + ballotIndex + " thrown away for election " + election + ". Choice " + (choice + 1) - + " not available."); + "Ballot " + mBallotIndex + " thrown away for election " + election + ". No more choices available."); return false; } - String thisBallotsChoice = votes.get(election).get(choice); + String thisBallotsChoice = mVotes.get(election).peek(); - if (!App.allVotes.get(election).containsKey(thisBallotsChoice)) { - votes.get(election).remove(choice); + if (!App.isCandidateAvailable(election,thisBallotsChoice)) { + mVotes.get(election).poll(); //like pop App.writer.println( - "Ballot " + ballotIndex + " choice #" + (choice + 1) - + " was already eliminated. Moving choices up 1."); + "Ballot " + mBallotIndex + " choice " + thisBallotsChoice + " is captain or has already been eliminated. Trying next choice."); return cast(election); // now the choice should be valid } ArrayList thisCandidatesBallotList = App.allVotes.get(election).get(thisBallotsChoice); - for (String importantElection : App.importantElections) { - if (App.winners.containsKey(importantElection)) { - if (App.winners.get(importantElection).equals(thisBallotsChoice)) { - // this ballot's choice is for someone who already won captain - votes.get(election).remove(choice); - App.writer.println( - "Ballot " + ballotIndex + " choice #" + (choice + 1) + " is captain. Moving choices up 1."); - return cast(election); // now the choice should be valid - } - } - } - if (thisCandidatesBallotList.contains(this)) { System.err.println( - "Ballot " + ballotIndex + " already voted for " + thisBallotsChoice + " in election " + election); + "Ballot " + mBallotIndex + " already voted for " + thisBallotsChoice + " in election " + election); System.exit(-2); } thisCandidatesBallotList.add(this); - App.writer.println("Ballot " + ballotIndex + " cast for " + thisBallotsChoice + ", choice #" + (choice + 1) - + " in election " + election); - currentChoiceIdxs.put(election, choice); + App.writer.println("Ballot " + mBallotIndex + " cast for " + thisBallotsChoice + " in election " + election); return true; } From 695f1d8b2990592ee95499f04f5fb61588fb23cd Mon Sep 17 00:00:00 2001 From: Dan Nichols Date: Sat, 3 May 2025 23:34:55 -0400 Subject: [PATCH 2/2] format again --- src/App.java | 68 +++++++++++++++++++++++++------------------------ src/Ballot.java | 29 +++++++++++---------- 2 files changed, 51 insertions(+), 46 deletions(-) diff --git a/src/App.java b/src/App.java index 1209878..98bf250 100644 --- a/src/App.java +++ b/src/App.java @@ -24,73 +24,75 @@ private App() { public static PrintWriter writer; public static void main(String[] args) throws Exception { - System.out.println("Looking in " + System.getProperty("user.dir")+" for voteTallies.csv"); + System.out.println("Looking in " + System.getProperty("user.dir") + " for voteTallies.csv"); BufferedReader reader; - + try { reader = new BufferedReader(new FileReader("voteTallies.csv")); - writer = new PrintWriter(new FileWriter("voteResults.txt"),true); + writer = new PrintWriter(new FileWriter("voteResults.txt"), true); } catch (Exception e) { e.printStackTrace(); return; } String line; - int lineIdx=1; - while((line = reader.readLine())!=null){ - line=line.replace("\"","");//google sheets adds a bunch of random quotes + int lineIdx = 1; + while ((line = reader.readLine()) != null) { + line = line.replace("\"", "");// google sheets adds a bunch of random quotes - if(line.contains("Timestamp")){ + if (line.contains("Timestamp")) { writer.println("Processing Headings"); columnHeadings = line.split(","); for (String heading : Arrays.asList(columnHeadings)) { - if(heading.contains("Timestamp")||heading.contains("Email Address")||heading.contains("Full Name")){ + if (heading.contains("Timestamp") || heading.contains("Email Address") + || heading.contains("Full Name")) { continue; } - String election = getElectionNameFromHeading(heading);//remove first/second/third - writer.println("Found column "+heading); + String election = getElectionNameFromHeading(heading);// remove first/second/third + writer.println("Found column " + heading); - if(!allVotes.containsKey(election)){ + if (!allVotes.containsKey(election)) { allVotes.put(election, new TreeMap<>()); - writer.println("Created election "+election); - if(election.contains("Captain")){ + writer.println("Created election " + election); + if (election.contains("Captain")) { importantElections.add(election); - writer.println("Important election "+election+" noted"); + writer.println("Important election " + election + " noted"); } } } - writer.println("Elections: "+allVotes.keySet().toString()); - writer.println("Important elections: "+importantElections.toString()); - } - else{ - ballots.put(lineIdx, new Ballot(line,lineIdx)); - writer.println("Found ballot "+line+". Choices: "+ballots.get(lineIdx).toString()); + writer.println("Elections: " + allVotes.keySet().toString()); + writer.println("Important elections: " + importantElections.toString()); + } else { + ballots.put(lineIdx, new Ballot(line, lineIdx)); + writer.println("Found ballot " + line + ". Choices: " + ballots.get(lineIdx).toString()); lineIdx++; } - + } - for(String importantName:importantElections){ + for (String importantName : importantElections) { runElection(importantName); - System.out.println("Running election "+importantName); - if (winners.containsKey(importantName)) { // a winner has been found so they need to be removed from the list of eligible candidates - allVotes.keySet().forEach((electionName) -> {deregisterCandidate(electionName, winners.get(importantName));}); - + System.out.println("Running election " + importantName); + if (winners.containsKey(importantName)) { // a winner has been found so they need to be removed from the + // list of eligible candidates + allVotes.keySet().forEach((electionName) -> { + deregisterCandidate(electionName, winners.get(importantName)); + }); + } } - for(Map.Entry>> elections:allVotes.entrySet()){ - if(!importantElections.contains(elections.getKey())){ - System.out.println("Running election "+elections.getKey()); + for (Map.Entry>> elections : allVotes.entrySet()) { + if (!importantElections.contains(elections.getKey())) { + System.out.println("Running election " + elections.getKey()); runElection(elections.getKey()); } } - + writer.println("Results:"); - winners.forEach((election,winner)->{ - writer.println(" "+election+": "+winner); + winners.forEach((election, winner) -> { + writer.println(" " + election + ": " + winner); }); - writer.flush(); System.out.println("Done"); } diff --git a/src/Ballot.java b/src/Ballot.java index 95b10e2..d5a8fc4 100644 --- a/src/Ballot.java +++ b/src/Ballot.java @@ -7,29 +7,30 @@ import java.util.TreeMap; public final class Ballot { - private TreeMap> mVotes = new TreeMap<>();//ballotData + private TreeMap> mVotes = new TreeMap<>();// ballotData private int mBallotIndex; public Ballot(String ballotLine, int idx) { mBallotIndex = idx; - String[] ballotEntries = ballotLine.split(",");//chosenCandidates + String[] ballotEntries = ballotLine.split(",");// chosenCandidates if (ballotEntries.length != App.columnHeadings.length) { System.err.println("Length of ballot " + ballotLine + " doesn't match number of elections!"); System.exit(-1); } - for (int columnIdx = 3; columnIdx < ballotEntries.length; columnIdx++) {// start at 3 to skip timestamp, email, name - String thisColumnHeading = App.columnHeadings[columnIdx];//thisColumnString + for (int columnIdx = 3; columnIdx < ballotEntries.length; columnIdx++) {// start at 3 to skip timestamp, email, + // name + String thisColumnHeading = App.columnHeadings[columnIdx];// thisColumnString String thisElectionName = App.getElectionNameFromHeading(thisColumnHeading); - String thisChoice = ballotEntries[columnIdx];//chosenCandidate - + String thisChoice = ballotEntries[columnIdx];// chosenCandidate if (!mVotes.containsKey(thisElectionName)) {// no votes yet by this ballot for this election mVotes.put(thisElectionName, new LinkedList<>()); } - if(thisChoice.length()>0){ - App.registerCandidate(thisElectionName,thisChoice); - mVotes.get(thisElectionName).add(thisChoice);// ballot must have numbered choices in order (choice 1 in column D, 2 in E, etc.) + if (thisChoice.length() > 0) { + App.registerCandidate(thisElectionName, thisChoice); + mVotes.get(thisElectionName).add(thisChoice);// ballot must have numbered choices in order (choice 1 in + // column D, 2 in E, etc.) } } } @@ -50,16 +51,18 @@ public String toString() { public boolean cast(String election) throws IOException { if (mVotes.get(election).size() == 0) { App.writer.println( - "Ballot " + mBallotIndex + " thrown away for election " + election + ". No more choices available."); + "Ballot " + mBallotIndex + " thrown away for election " + election + + ". No more choices available."); return false; } String thisBallotsChoice = mVotes.get(election).peek(); - if (!App.isCandidateAvailable(election,thisBallotsChoice)) { - mVotes.get(election).poll(); //like pop + if (!App.isCandidateAvailable(election, thisBallotsChoice)) { + mVotes.get(election).poll(); // like pop App.writer.println( - "Ballot " + mBallotIndex + " choice " + thisBallotsChoice + " is captain or has already been eliminated. Trying next choice."); + "Ballot " + mBallotIndex + " choice " + thisBallotsChoice + + " is captain or has already been eliminated. Trying next choice."); return cast(election); // now the choice should be valid }