-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eurovision.java
474 lines (436 loc) · 17 KB
/
Eurovision.java
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Random;
import java.io.Serializable;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* A program for Eurovision.
* The user will input the countries and acts for Eurovision which can then
* be saved. It can then allocate out countries for each player to support.
*
* @author Daniella McSorley
* @version 0.2
*/
public class Eurovision implements Serializable {
private ArrayList<Country> setList;
private Scanner sc;
private final long serialVersionUID = 1L;
private int numOfPlayers;
/**
* Constructor for a Eurovision object.
*
* @param numOfPlayers The number of people being allocated countries to
* support.
*/
public Eurovision(int numOfPlayers) {
setList = new ArrayList<>();
sc = new Scanner(System.in);
this.numOfPlayers = numOfPlayers;
}
/**
* The main method which utilises the rest of the methods.
* @throws InterruptedException
* @throws IOException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException, IOException, InterruptedException {
runEurovision();
}
/**
* Getter for the setList ArrayList.
*
* @return setList ArrayList.
*/
public ArrayList<Country> getSetList() {
return setList;
}
/**
* Getter for the numOfPlayers field.
*
* @return numOfPlayers field.
*/
public int getNumOfPlayers() {
return numOfPlayers;
}
/**
* Setter for the numOfPlayers field.
*
* @param numOfPlayers The number of people playing.
*/
public void setNumOfPlayers(int numOfPlayers) {
this.numOfPlayers = numOfPlayers;
}
/**
* This can be used for any Country objects created manually. The
* addCountryConsole method is recommended for most cases.
*
* @param country a Country Object to add to the setList ArrayList.
*/
public void addCountry(Country country) {
setList.add(country);
}
/**
* Allows a user to manually remove a country by just the name rather
* than requiring a Country object.
*
* @param aCountry a String name of a country to be removed.
* @return true if the country has been removed, or false if not.
*/
public boolean removeCountry(String aCountry) {
Iterator<Country> it = setList.iterator();
while (it.hasNext()) {
if (it.next().getCountry().equals(aCountry)) {
it.remove();
return true;
}
}
return false;
}
/**
* Empties the setList ArrayList.
*/
public void clearSetList() {
setList.clear();
}
/**
* Allows to user to see every Country object in setList ArrayList.
*/
public void outputString() {
for (Country country : setList) {
System.out.println(country.toString());
}
}
/**
* This is the preferred method to add new countries to setList as the
* Song and Country objects are made behind the scenes and added in a user
* friendly way.
*/
public void addCountryConsole() {
String ans = "";
String correct = "";
while (!(ans.equals("N"))) {
ans = "";
correct = "";
System.out.println();
System.out.println("What is the name of the country?");
String country = sc.nextLine();
System.out.println();
System.out.println("What artist is performing for " + country + "?");
String artist = sc.nextLine();
System.out.println();
System.out.println("What is the name of the song performed by " + artist + "?");
String title = sc.nextLine();
Song theSong = new Song(artist, title);
Country theCountry = new Country(country, theSong);
while (!correct.equals("Y") && !correct.equals("N")) {
System.out.println();
System.out.println("So that is...");
System.out.println(country.toUpperCase());
System.out.println(artist + ": " + title);
System.out.println();
System.out.println("Does that look right? Y/N");
correct = sc.nextLine().toUpperCase();
}
if (correct.equals("Y")) {
setList.add(theCountry);
System.out.println();
System.out.println("Country added!");
} else {
System.out.println();
System.out.println("Let's scrap that one then and start it again.");
}
while (!ans.equals("Y") && !ans.equals("N")) {
System.out.println();
System.out.println("Add another country? Y/N.");
ans = sc.nextLine().toUpperCase();
}
}
}
/**
* Allocates the maximum amount of countries to each player so that each
* person has the same amount to support. This is then output into the
* terminal to show who is allocated each country.
*
* @param playernames The list of player names.
* @throws InterruptedException
*/
public void allocateCountries(String[] playerNames) throws InterruptedException {
int songsPerPlayer = setList.size() / numOfPlayers;
int remainingSongs = setList.size() % numOfPlayers;
Random rand = new Random();
for (int i = 0; i < numOfPlayers; i++) {
System.out.println();
System.out.println();
System.out.println("***" + playerNames[i].toUpperCase() + "***");
int numToBeAllocated = songsPerPlayer + (i < remainingSongs ? 1 : 0);
Thread.sleep(1000);
for (int j = 0; j < numToBeAllocated; j++) {
int index = rand.nextInt(setList.size());
System.out.println(setList.get(index));
setList.remove(index);
Thread.sleep(1000);
}
}
}
/**
* Saves the setList ArrayList so it can be used again later without
* having to manually input everything from scratch each time.
*
* @throws ClassNotFoundException Throws if a class is not found.
* @throws IOException Throws if unable to save the file.
* @param year The year of the Eurovision set list being saved.
*/
public void saveSetList(String year) throws IOException, ClassNotFoundException {
FileOutputStream fos = new FileOutputStream("Set_Lists/" + year);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(setList);
oos.close();
}
/**
* Loads a saved setList ArrayList which has already been set up and saved.
*
* @throws InterruptedException Throws when interrupted.
* @throws ClassNotFoundException Throws if a class is not found.
* @throws IOException Throws if unable to locate the file.
* @param year The year of the Eurovision set list being loaded.
*/
public void loadSetList(String year) throws IOException, ClassNotFoundException, InterruptedException {
try {
FileInputStream fis = new FileInputStream("Set_Lists/" + year);
ObjectInputStream ois = new ObjectInputStream(fis);
@SuppressWarnings("unchecked")
ArrayList<Country> loadedList = (ArrayList<Country>) ois.readObject();
setList = loadedList;
ois.close();
} catch (IOException e) {
System.out.println("Problem finding file. Please restart.");
Thread.sleep(3000);
}
}
/**
* Adds countries and songs from a CSV file.
*
* @param cssFilePath The path of the CSV file.
*/
public String addCountriesFromCSV(boolean useUserInput) {
if (useUserInput) {
String file = "";
try {
System.out.println();
System.out.println("What is the name of the CSV file?");
file = sc.nextLine();
String csvFilePath = "Set_Lists/CSV/" + file + ".csv";
BufferedReader reader = new BufferedReader(new FileReader(csvFilePath));
String line;
boolean firstLine = true;
while ((line = reader.readLine()) != null) {
if (firstLine) {
firstLine = false;
continue; // Skip the first line
}
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length >= 3) {
String countryName = parts[1].trim().replace("\"", "");
String artistName = parts[2].trim().replace("\"", "");
String songTitle = parts[3].trim().replace("\"", "");
Song theSong = new Song(artistName, songTitle);
Country theCountry = new Country(countryName, theSong);
setList.add(theCountry);
}
}
System.out.println("Countries added from CSS file.");
}
} catch (IOException e) {
System.err.println("Error reading CSS file: " + e.getMessage());
}
return file;
} else {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("Set_Lists/CSV/"), "*.csv")) {
for (Path filePath : stream) {
BufferedReader reader = new BufferedReader(new FileReader(filePath.toString()));
String line;
boolean firstLine = true;
while ((line = reader.readLine()) != null) {
if (firstLine) {
firstLine = false;
continue; // Skip the first line
}
String[] parts = line.split(",");
if (parts.length >= 3) {
String countryName = parts[1].trim().replace("\"", "");
String artistName = parts[2].trim().replace("\"", "");
String songTitle = parts[3].trim().replace("\"", "");
Song theSong = new Song(artistName, songTitle);
Country theCountry = new Country(countryName, theSong);
setList.add(theCountry);
}
}
reader.close();
// Extract year and save setList outside the loop
String year = extractYearFromFilePath(filePath.toString());
if (!year.equals("")) {
try {
saveSetList(year);
clearSetList();
} catch (Exception e) {
System.out.println(e);
}
}
}
System.out.println();
System.out.println("Countries loaded.");
} catch (IOException e) {
System.err.println("Error reading CSV files: " + e.getMessage());
}
return "";
}
}
/**
* Extracts the given year from a filepath.
*
* @param filePath The path of the CSV file.
* @return The year being extracted.
*/
public static String extractYearFromFilePath(String filePath) {
Pattern pattern = Pattern.compile("\\b(\\d{4})\\.csv\\b");
Matcher matcher = pattern.matcher(filePath);
if (matcher.find()) {
return matcher.group(1);
}
return "";
}
/**
* Extracts the given year from a filepath.
*
* @return A line separated string of available years in the system.
*/
public String getAvailableYears() {
ArrayList<String> output = new ArrayList<>();
StringBuilder outputStringBuilder = new StringBuilder();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("Set_Lists/CSV"), "*.csv")) {
for (Path filePath : stream) {
String year = extractYearFromFilePath(filePath.toString());
output.add(year);
}
Collections.sort(output);
for (String item : output) {
outputStringBuilder.append(item).append("\n");
}
} catch (IOException e) {
System.err.println("Error reading CSV files: " + e.getMessage());
}
return outputStringBuilder.toString();
}
public void cleanOutFiles(String directoryPath) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(directoryPath))) {
for (Path filePath : stream) {
if (Files.isRegularFile(filePath)) {
try {
Files.delete(filePath);
} catch (IOException e) {
System.err.println("Error deleting file " + filePath.toString() + ": " + e.getMessage());
}
}
}
} catch (IOException e) {
System.err.println("Error reading directory: " + e.getMessage());
}
}
public static void runEurovision() throws IOException, ClassNotFoundException, InterruptedException {
System.out.println("Welcome to the Eurovision allocator!");
Thread.sleep(3000);
Scanner scan = new Scanner(System.in);
int players = -1;
while (players <= 0) {
System.out.println();
System.out.println("How many players?");
try {
players = scan.nextInt();
if (players <= 0) {
System.out.println();
System.out.println("Number of players must be greater than 0. Try again.");
}
} catch (Exception e) {
System.out.println();
System.out.println("Number must be a whole integer. Try again.");
// Clear the buffer
scan.nextLine();
players = -1;
}
}
String[] playerNames = new String[players];
for (int i = 0; i < players; i++) {
System.out.println();
System.out.println("What is the name of PLAYER " + (i + 1));
playerNames[i] = scan.next();
}
Eurovision euro = new Eurovision(players);
euro.addCountriesFromCSV(false);
String ans = "";
System.out.println();
System.out.println("Available years:\n" + euro.getAvailableYears());
while (!ans.equals("Y") && !ans.equals("N")) {
System.out.println();
System.out.println("Is the set list one of the above? Y/N");
ans = scan.next().toUpperCase();
}
if (ans.equals("Y")) {
System.out.println();
System.out.println("What year is the set list?");
String year = scan.next().toUpperCase();
euro.loadSetList(year);
} else {
ans = "";
String fileName = "";
while (!ans.equals("Y") && !ans.equals("N")) {
System.out.println();
System.out.println("Load from CSV? Y/N");
ans = scan.next().toUpperCase();
}
if (ans.equals("Y")) {
fileName = euro.addCountriesFromCSV(true);
} else {
euro.addCountryConsole();
}
ans = "";
while (!ans.equals("Y") && !ans.equals("N")) {
System.out.println();
System.out.println("Save the current set list? Y/N");
ans = scan.next().toUpperCase();
}
if (ans.equals("Y")) {
if (!fileName.isEmpty()) {
euro.saveSetList(fileName);
} else {
System.out.println();
System.out.println("What year is this set list for?");
String year = scan.next().toUpperCase();
euro.saveSetList(year);
}
}
}
System.out.println();
if (players > 0) {
System.out.println("Now allocating countries to each player...");
Thread.sleep(1000);
euro.allocateCountries(playerNames);
scan.close();
}
euro.cleanOutFiles("Set_Lists/");
}
}