import java.util.ArrayList; import java.util.Scanner; import java.util.Set; import java.util.Random; import java.io.*; /** * Runs an instant runoff election. * In an instant runoff election each voter submits a list of candidates, * in order of preference. The first name on the list is the voter's * first choice, the second the voter's second choice, etc. The voter * need not list all of the candidates. * * The election is conducted in rounds. Each voter's first choice is * tallied, and the candidate with the fewest first-place votes is * eliminated. (If there is a tie one of the lowest vote-getters is chosen * at random to be eliminated.) In each round the voter's top choice * amongst the remaining candidates gets that voter's vote for that round. * If no current candidate is on a voter's list that voter casts no vote * for this round. * * The process ends when there is a single candidate left, who is declared * the winner. * * This version is written in a more object-oriented style. * * @author Scot Drysdale, minor changes by Tom Cormen */ public class InstantRunoffOO { private static boolean debugOn = true; // print debugging output? private static Random generator = new Random(); // random number generator /** * Picks a random item from a list. * @list the list to choose from * @return an item chosen randomly from list */ public static String pickRandomItem(ArrayList list) { return list.get(generator.nextInt(list.size())); } /** * Runs an instant-runoff election * @param ballots the ballots for this election * @return winner of the election */ public static String runInstantRunoffElection(Election ballots) { Set candidates = ballots.getInitialCandidates(); // Run rounds until down to a single candidate. while (candidates.size() > 1) { VoteTally tally = ballots.countRunoffVotes(candidates); String loser = pickRandomItem(tally.getLosers()); candidates.remove(loser); if (debugOn) { System.out.println("Vote tally:\n" + tally); System.out.println("Loser: " + loser); } } if (candidates.size() > 0) return candidates.iterator().next(); // return the surviving candidate else return null; } /** * Test program */ public static void main(String[] args) { Election ballots = new Election(); Ballot ballot; String ballotFileName = "/Users/scot/Documents/workspace/cs10proj/src/ballots.txt"; Scanner ballotFile = null; try { ballotFile = new Scanner(new File(ballotFileName)); } catch (FileNotFoundException e) { System.err.println("No such file: " + ballotFileName); System.exit(1); } while (ballotFile.hasNext()) { String line = ballotFile.nextLine(); ballot = new Ballot(); Scanner inLine = new Scanner(line); while (inLine.hasNext()) { String candidate = inLine.next(); ballot.addCandidate(candidate); } ballots.addBallot(ballot); } String winner = runInstantRunoffElection(ballots); if (winner != null) System.out.println("The winner of the instant runoff election is: " + winner); else System.out.println("No valid votes cast"); } }