-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallot.java
More file actions
81 lines (70 loc) · 3.52 KB
/
Copy pathBallot.java
File metadata and controls
81 lines (70 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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;
public final class Ballot {
private TreeMap<String, Queue<String>> mVotes = new TreeMap<>();// ballotData
private int mBallotIndex;
public Ballot(String ballotLine, int idx) {
mBallotIndex = idx;
String[] ballotEntries = ballotLine.split(",");// chosenCandidates
if (ballotEntries.length != App.sColumnHeadings.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.sColumnHeadings[columnIdx];// thisColumnString
String thisElectionName = App.getElectionNameFromHeading(thisColumnHeading);
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.)
}
}
}
@Override
public String toString() {
StringBuilder rBuilder = new StringBuilder();
for (Map.Entry<String, Queue<String>> thisElectionChoices : mVotes.entrySet()) {
rBuilder.append("Election " + thisElectionChoices.getKey() + ":");
List<String> 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 (mVotes.get(election).size() == 0) {
App.sWriter.println(
"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
App.sWriter.println(
"Ballot " + mBallotIndex + " choice " + thisBallotsChoice
+ " is captain or has already been eliminated. Trying next choice.");
return cast(election); // now the choice should be valid
}
ArrayList<Ballot> thisCandidatesBallotList = App.sAllVotes.get(election).get(thisBallotsChoice);
if (thisCandidatesBallotList.contains(this)) {
System.err.println(
"Ballot " + mBallotIndex + " already voted for " + thisBallotsChoice + " in election " + election);
System.exit(-2);
}
thisCandidatesBallotList.add(this);
App.sWriter.println("Ballot " + mBallotIndex + " cast for " + thisBallotsChoice + " in election " + election);
return true;
}
}