-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElectionManager.java
More file actions
246 lines (207 loc) · 10.1 KB
/
ElectionManager.java
File metadata and controls
246 lines (207 loc) · 10.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
//////////////// FILE HEADER (INCLUDE IN EVERY FILE) //////////////////////////
//
// Title: ElectionManager
// Course: CS 300 Fall 2024
//
// Author: Matthew Suri
// Email: mssuri@wisc.edu
// Lecturer: Blerina Gkotse
//
//////////////////// PAIR PROGRAMMERS COMPLETE THIS SECTION ///////////////////
//
// Partner Name: (name of your pair programming partner)
// Partner Email: (email address of your programming partner)
// Partner Lecturer's Name: (name of your partner's lecturer)
//
// VERIFY THE FOLLOWING BY PLACING AN X NEXT TO EACH TRUE STATEMENT:
// ___ Write-up states that pair programming is allowed for this assignment.
// ___ We have both read and understand the course Pair Programming Policy.
// ___ We have registered our team prior to the team registration deadline.
//
//////////////////////// ASSISTANCE/HELP CITATIONS ////////////////////////////
//
// Persons: N/A
// Online Sources: https://www.geeksforgeeks.org/bubble-sort-algorithm/, https://docs.oracle.com/en/java/
//
///////////////////////////////////////////////////////////////////////////////
/**
* This class provides methods that allow for manipulation of a 2D array
* data structure that contains candidates in an election
*/
public class ElectionManager {
/**
* Checks if a candidate exists in the list of candidates.
*
* @param candidates 2D array containing the list of candidates
* @param numCandidates the number of candidates
* @param name the name of the candidate
* @param party the party of the candidate
* @return status variable, true if the candidate exists, false otherwise
*/
public static boolean containsCandidate(String[][] candidates, int numCandidates, String name, String party) {
boolean status = false; // false initially and true when candidate is detected
// Iterates through candidates and if name and party match than status is set to true.
for (int i = 0; i < numCandidates; i++) {
boolean nameMatches = candidates[i][0].equals(name);
boolean partyMatches = candidates[i][1].equals(party);
if (nameMatches && partyMatches) {
status = true;
}
}
return status;
}
/**
* Adds a new candidate to the list of candidates, sorts the list after adding,
* and returns the new number of candidates.
*
* @param candidates 2D array containing the list of candidates
* @param numCandidates the number of candidates
* @param name the name of the candidate
* @param party the party of the candidate
* @param numVotes the number of votes the candidate has
* @return the updated number of candidates
*/
public static int addCandidate(String[][] candidates, int numCandidates, String name, String party, int numVotes) {
String[] candidate = {name, party, Integer.toString(numVotes)};
boolean isSpace = false; // false initially and true if null is detected in 2D array
// Iterates through entire candidates 2D array and if null data in array isSpace is true.
for (String[] data : candidates) {
if (data == null) {
isSpace = true;
}
}
// candidateExists checks whether candidate is in 2D array
boolean candidateExists = containsCandidate(candidates, numCandidates, name, party);
boolean validVotes = numVotes > -1; // validVotes checks candidate has positive votes
// If candidate isn't in 2D array, has a valid # of votes and there is space, iterate through and add
// candidate to first space available.
if (!candidateExists && validVotes && isSpace) {
numCandidates += 1;
for (int i = 0; i < candidates.length; i++) {
if (candidates[i] == null) {
candidates[i] = candidate;
break;
}
}
}
// Bubble Sort for sorting the 2D array lexicographic order.
for (int i = 0; i < numCandidates - 1; i++) {
for (int j = 0; j < numCandidates - 1 - i; j++) {
boolean isGreater = candidates[j][0].compareTo(candidates[j + 1][0]) > 0;
if (isGreater) {
String[] temp = candidates[j];
candidates[j] = candidates[j + 1];
candidates[j + 1] = temp;
}
}
}
return numCandidates;
}
/**
* Removes a candidate from the list of candidates, re-sorts the list after removing,
* and returns the updated number of candidates.
*
* @param candidates 2D array containing the list of candidates
* @param numCandidates the number of candidates
* @param name the name of the candidate
* @param party the party of the candidate
* @return the updated number of candidates
*/
public static int dropCandidate(String[][] candidates, int numCandidates, String name, String party) {
// candidateExists checks whether candidate is in 2D array
boolean candidateExists = containsCandidate(candidates, numCandidates, name, party);
// If the candidate is in the 2D array and is not null drop candidate and replace with null.
if (candidateExists) {
for (int i = 0; i < numCandidates; i++) {
boolean candidateNotNull = candidates[i] != null;
boolean nameMatches = candidates[i][0].equals(name);
boolean partyMatches = candidates[i][1].equals(party);
if (candidateNotNull && nameMatches && partyMatches) {
candidates[i] = null;
numCandidates -= 1;
break;
}
}
// Iterates through 2D array and moves null to the back.
for (int i = 0; i < numCandidates; i++) {
boolean isNull = candidates[i] == null;
boolean nextIsNotNull = candidates[i + 1] != null;
if (isNull && nextIsNotNull) {
String[] temp = candidates[i];
candidates[i] = candidates[i + 1];
candidates[i + 1] = temp;
}
}
// Bubble Sort for sorting the 2D array lexicographic order.
for (int i = 0; i < numCandidates - 1; i++) {
for (int j = 0; j < numCandidates - 1 - i; j++) {
boolean isGreater = candidates[j][0]
.compareTo(candidates[j + 1][0]) > 0;
if (isGreater) {
String[] temp = candidates[j];
candidates[j] = candidates[j + 1];
candidates[j + 1] = temp;
}
}
}
}
return numCandidates;
}
/**
* Determines the winner of the election based on the number of votes.
*
* @param candidates 2D array containing the list of candidates
* @param numCandidates the number of candidates
* @return a string containing the winner's information or "CONTINGENT"
* if no candidate has more than 50% of the votes
*/
public static String findWinner(String[][] candidates, int numCandidates) {
// winnerInfo is initialized with CONTINGENT and can be set to winner if winner detected
String winnerInfo = "CONTINGENT";
// If candidate has more than 0 votes look through the 2D array for the candudate with the most votes.
if (numCandidates != 0) {
int totalVotes = 0; // initialzie total votes in election
// Tally total votes.
for (int i = 0; i < numCandidates; i++) {
totalVotes += Integer.valueOf(candidates[i][2]);
}
// Calculate votePct for each candidate and if one candidate has >50% assign them to winnerInfo.
for (int i = 0; i < numCandidates; i++) {
double candidateVotes = Double.parseDouble(candidates[i][2]);
double votePercentage = candidateVotes / (double) totalVotes;
if (votePercentage > 0.5) {
double votePct = votePercentage * 100.0;
winnerInfo = candidates[i][0] + " (" + candidates[i][1] + ") - " + votePct + "%";
}
}
}
return winnerInfo;
}
/**
* Finds the candidate with the lowest number of votes.
*
* @param candidates 2D array containing the list of candidates
* @param numCandidates the number of candidates
* @return a string containing the lowest polling candidate's information
* or "UNCONTESTED" if there is only one candidate
*/
public static String findLowestPollingCandidate(String[][] candidates, int numCandidates) {
// lowestPollingCandidate is initialized to UNCONTESTED and can be set to lowestPolling
// Candidate if there are multiple candidates
String lowestPollingCandidate = "UNCONTESTED";
// If multiple candidates running compare candidates to figure out who has the lowest votes
if (numCandidates > 1) {
int lowestVotes = Integer.parseInt(candidates[0][2]);
lowestPollingCandidate = candidates[0][0] + " (" + candidates[0][1] + ") - " + lowestVotes;
// Compare and then set lowest votes and assign that candidate to lowestPollingCandidate.
for (int i = 1; i < numCandidates; i++) {
int currentVotes = Integer.parseInt(candidates[i][2]);
if (currentVotes < lowestVotes) {
lowestVotes = currentVotes;
lowestPollingCandidate = candidates[i][0] + " (" + candidates[i][1] + ") - " + lowestVotes;
}
}
}
return lowestPollingCandidate;
}
}