import java.util.Iterator; import java.util.List; import java.util.ArrayList; import java.util.Set; /** * Holds a voter's ballot. * @author Scot Drysdale */ public class Ballot { private List ballot; // holds the voter's choices (in order given) /** * Constructs an empty ballot. */ public Ballot() { ballot = new ArrayList(); } /** * Finds and returns the top choice among candidates. * @param candidates the current set of candidates * @return name of top choice in candidates, or null if none exists. */ public String getTopChoice(Set candidates) { Iterator iter = ballot.iterator(); while (iter.hasNext()) { String candidate = iter.next(); if (candidates.contains(candidate)) return candidate; } return null; // none of the candidates on ballot were in candidates } /** * Returns the first candidate on the ballot * @return the first candidate on the ballot, or null if none exists */ public String getFirst() { if (ballot.size() > 0) return ballot.get(0); else return null; } /** * Adds candidate to this ballot. * @param candidate the candidate to add */ public void addCandidate(String candidate) { ballot.add(candidate); } /** * Returns a string with the candidates */ public String toString() { return ballot.toString(); } }