-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
276 lines (243 loc) · 12.1 KB
/
Copy pathApp.java
File metadata and controls
276 lines (243 loc) · 12.1 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
public final class App {
private App() {
}
private static TreeMap<Integer, Ballot> sBallots = new TreeMap<>();
public static TreeMap<String, TreeMap<String, ArrayList<Ballot>>> sAllVotes = new TreeMap<>();// first string is
// election, second is
// candidate
public static ArrayList<String> sImportantElections = new ArrayList<>();
public static TreeMap<String, String> sWinners = new TreeMap<>();
public static String[] sColumnHeadings;
public static PrintWriter sWriter;
public static void main(String[] args) throws Exception {
System.out.println("Looking in " + System.getProperty("user.dir") + " for voteTallies.csv");
BufferedReader reader;
reader = new BufferedReader(new FileReader("voteTallies.csv"));
sWriter = new PrintWriter(new FileWriter("voteResults.txt"), true);
String line;
int lineIdx = 1;
while ((line = reader.readLine()) != null) {
line = line.replace("\"", "");// google sheets adds a bunch of random quotes
if (line.contains("Timestamp")) {
sWriter.println("Processing Headings");
sColumnHeadings = line.split(",");
for (String heading : Arrays.asList(sColumnHeadings)) {
if (heading.contains("Timestamp") || heading.contains("Email Address")
|| heading.contains("Full Name")) {
continue;
}
String election = getElectionNameFromHeading(heading);// remove first/second/third
sWriter.println("Found column " + heading);
if (!sAllVotes.containsKey(election)) {
sAllVotes.put(election, new TreeMap<>());
sWriter.println("Created election " + election);
if (election.contains("Captain")) {
sImportantElections.add(election);
sWriter.println("Important election " + election + " noted");
}
}
}
sWriter.println("Elections: " + sAllVotes.keySet().toString());
sWriter.println("Important elections: " + sImportantElections.toString());
} else {
sBallots.put(lineIdx, new Ballot(line, lineIdx));
sWriter.println("Found ballot " + line + ". Choices: " + sBallots.get(lineIdx).toString());
lineIdx++;
}
}
reader.close();
for (String importantName : sImportantElections) {
runElection(importantName);
System.out.println("Running election " + importantName);
if (sWinners.containsKey(importantName)) { // a winner has been found so they need to be removed from the
// list of eligible candidates
sAllVotes.keySet().forEach((electionName) -> {
deregisterCandidate(electionName, sWinners.get(importantName));
});
}
}
for (Map.Entry<String, TreeMap<String, ArrayList<Ballot>>> elections : sAllVotes.entrySet()) {
if (!sImportantElections.contains(elections.getKey())) {
System.out.println("Running election " + elections.getKey());
runElection(elections.getKey());
}
}
sWriter.println("Results:");
sWinners.forEach((election, winner) -> {
sWriter.println(" " + election + ": " + winner);
});
sWriter.flush();
sWriter.close();
System.out.println("Done");
}
public static String getElectionNameFromHeading(String heading) {
return (heading.replaceAll(" \\[.*", ""));
}
public static void runElection(String election) throws IOException {
sWriter.println("Running election " + election);
sBallots.forEach((idx, ballot) -> {
try {
ballot.cast(election);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
sWriter.println("Initial casting complete");
printElectionStandings(election);
for (int round = 0; round <= 10; round++) {
int numVotes = getNumberOfVotesInElection(election);
sWriter.println(numVotes + " votes in election " + election);
SimpleEntry<String, Integer> currentLeader = getCurrentLeader(election);
if (currentLeader.getValue() > numVotes / 2 && currentLeader.getKey() != null) {
sWriter.println("Winner found. Winner is " + currentLeader.getKey() + " with " + currentLeader.getValue()
+ " votes.");
sWinners.put(election, currentLeader.getKey());
break;
}
int numberOfCandidatesBeforeRemoval = sAllVotes.get(election).size();
SimpleEntry<String, Integer> currentLoser = getCurrentLoser(election);
if (currentLoser.getKey() == null) {
sWriter.println("Unbroken tie for the lose. See standings and resolve manually.");
break;
}
ArrayList<Ballot> votesForLoser = sAllVotes.get(election).get(currentLoser.getKey());
sAllVotes.get(election).remove(currentLoser.getKey());
sWriter.println("Candidate " + currentLoser.getKey() + " removed.");
for (Ballot ballotForLoser : votesForLoser) { // now they are eliminated remove those that voted for them
ballotForLoser.cast(election);
}
if (sAllVotes.get(election).size() >= numberOfCandidatesBeforeRemoval) {
System.err.println("Recast all votes for " + currentLoser + " but now there are more candidates");
System.exit(-5);
}
sWriter.println("Starting round " + (round + 2));
printElectionStandings(election);
}
sWriter.println("Finished election " + election);
}
public static ArrayList<Map.Entry<String, ArrayList<Ballot>>> getSortedStandings(String election) {
ArrayList<Map.Entry<String, ArrayList<Ballot>>> currentStandings = new ArrayList<>(
sAllVotes.get(election).entrySet());
currentStandings.sort(Entry.comparingByValue((ArrayList<Ballot> o1, ArrayList<Ballot> o2) -> {
return (int) Math.signum(o2.size() - o1.size());
}));
return currentStandings;
}
public static void printElectionStandings(String election) {
sWriter.println("Standings of election " + election);
for (Map.Entry<String, ArrayList<Ballot>> candidate : getSortedStandings(election)) {
String line = " " + candidate.getValue().size() + ": " + candidate.getKey();
sWriter.print(line);
for (int i = 0; i < 40 - line.length(); i++) {
sWriter.print(" ");
}
sWriter.print("|");
int chunkVotes = candidate.getValue().size() * 20 / getNumberOfVotesInElection(election);
for (int i = 0; i < chunkVotes; i++) {
if (i >= 10)
sWriter.print("*");
else
sWriter.print("#");
}
for (int i = 0; i < 20 - chunkVotes; i++) {
sWriter.print(" ");
}
sWriter.println("|");
}
}
public static int getNumberOfVotesInElection(String election) {
TreeMap<String, ArrayList<Ballot>> votes = sAllVotes.get(election);
int numTotalVotes = 0;
for (Map.Entry<String, ArrayList<Ballot>> candidates : votes.entrySet()) {
numTotalVotes += candidates.getValue().size();
}
return numTotalVotes;
}
public static SimpleEntry<String, Integer> getCurrentLeader(String election) {
TreeMap<String, ArrayList<Ballot>> votes = sAllVotes.get(election);
String potentialCurrentLeader = null;
Integer potentialLeaderVotes = 0;
for (Map.Entry<String, ArrayList<Ballot>> 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);
}
public static int getNextWorstNumberOfVotes(String election) {
ArrayList<Map.Entry<String, ArrayList<Ballot>>> 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) {
return thisNumVotes;
}
}
return 1;
}
public static SimpleEntry<String, Integer> getCurrentLoser(String election) {
TreeMap<String, ArrayList<Ballot>> votes = sAllVotes.get(election);
String potentialCurrentLoser = null;
Integer potentialLoserVotes = 9999;
boolean tie = false;
for (Map.Entry<String, ArrayList<Ballot>> 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 {
sWriter.println("Two candidates tied in the loss of election " + election
+ " but neither are important. Candidate " + potentialCurrentLoser + " will be eliminated.");
}
}
return new SimpleEntry<>(potentialCurrentLoser, potentialLoserVotes);
}
public static void registerCandidate(String electionName, String candidate) {
TreeMap<String, ArrayList<Ballot>> candidatesForThisElection = sAllVotes.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<String, ArrayList<Ballot>> candidatesVotesForThisElection = sAllVotes.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.sAllVotes.get(electionName).containsKey(candidate)) {
return false;
}
return true;
}
}