import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet; /** * Contains all of the ballots in an election * * @author Scot Drysdale */ public class Election { private List ballots; /** * Construct and empty election */ public Election() { ballots = new ArrayList(); } /** * Add a ballot to this election */ public void addBallot(Ballot ballot) { ballots.add(ballot); } /** * Creates the initial set of candidates. We only need to include * candidates who got at least one first-round vote * @return the initial set of candidates */ public Set getInitialCandidates() { Set candidateSet = new TreeSet(); // Add all names on all ballots to the set. Adding a candidate who // is already in the set does not change the set. for(Ballot ballot : ballots) { String candidate = ballot.getFirst(); if(candidate != null) candidateSet.add(candidate); } return candidateSet; } /** * Counts the number of votes that each candidate gets during a * round of an instant runoff election. Only the top candidate * on the ballot that appears in candidates is counted. * * @param candidates the current list of candidates * @return the vote tally for each candidate */ public VoteTally countRunoffVotes(Set candidates) { VoteTally voteTally = new VoteTally(); // Tally up top choice on each ballot for(Ballot ballot : ballots) { String topChoice = ballot.getTopChoice(candidates); voteTally.recordVote(topChoice); } return voteTally; } }