public class ManyRaces
1: import java.util.ArrayList;
2: import java.util.Collections;
3: import java.util.List;
4: import java.util.Scanner;
5:
6: /**
7: * A program demonstrating various List and Collections methods.
8: *
9: * @author Mark Young (A00000000)
10: */
11: public class ManyRaces {
12:
13: public static final Scanner kbd = new Scanner(System.in);
14: public static final int NUM_RACES = 4;
15:
16: /**
17: * Do everything.
18: *
19: * @param args command line arguments (ignored)
20: */
21: public static void main(String[] args) {
22: // introduce yourself
23: System.out.println("\n\n"
24: + "This program simulates an Olympic race event, "
25: + "where we run several races,\n"
26: + "and the top two runners from each race "
27: + "compete in a final race for medals.\n");
28:
29: // list of results for all races (a list of lists)
30: List<List<String>> races = new ArrayList<>();
31:
32: // get the racers
33: for (int r = 1; r <= NUM_RACES; ++r) {
34: // read racers into a NEW list (don't re-use old one!)
35: List<String> racers = readList("Enter the racers in heat #" + r
36: + " below, one per line.");
37:
38: // add list of racers to list of races
39: races.add(racers);
40: }
41:
42: // run the races
43: System.out.println("\n\n"
44: + "Time for the heats!\n");
45: for (int r = 1; r <= NUM_RACES; ++r) {
46: // announce the list
47: System.out.println("Heat #" + r);
48: List<String> racers = races.get(r-1);
49: System.out.println("Racers: " + racers);
50:
51: // run the race
52: Collections.shuffle(racers);
53:
54: // announce the results
55: System.out.println("Finish order: " + racers);
56: }
57:
58: // get drug test results
59: System.out.println("\n\n"
60: + "Drug Tests!\n");
61: List<String> cheaters = readList("Enter the names of the racers "
62: + "who failed their drug test below.");
63: for (int r = 1; r <= NUM_RACES; ++r) {
64: List<String> result = races.get(r-1);
65:
66: // removeAll returns true if any racers were removed
67: if (result.removeAll(cheaters)) {
68: // racer(s) removed, so report revised race results
69: System.out.println("Revised results for race #" + r + ": "
70: + result);
71: }
72: }
73:
74: // create final heat
75: List<String> finalHeat = new ArrayList<>();
76: for (List<String> result : races) {
77: // just in case everyone or almost everyone disqualified
78: int numAdvancing = Math.min(2, result.size());
79: finalHeat.addAll(result.subList(0, numAdvancing));
80: }
81:
82: // run final heat
83: System.out.println("\n\n"
84: + "Championship Race!\n");
85: Collections.sort(finalHeat);
86: System.out.println("Racers: " + finalHeat);
87: Collections.shuffle(finalHeat);
88: System.out.println("Finish order: " + finalHeat);
89: System.out.println("\tGold: " + finalHeat.get(0));
90: System.out.println("\tSilver: " + finalHeat.get(1));
91: System.out.println("\tBronze: " + finalHeat.get(2));
92: }
93:
94: /**
95: * Read a list of Strings one line at a time. The end of list is
96: * represented by a blank line.
97: *
98: * @param prompt the message to prompt the user with.
99: * @return a List of Strings entered by the user.
100: */
101: public static List<String> readList(String prompt) {
102: List<String> list = new ArrayList<>();
103: System.out.println(prompt);
104: System.out.println("Enter a blank line to finish.");
105: String name = kbd.nextLine();
106: while (!name.equals("")) {
107: list.add(name);
108: name = kbd.nextLine();
109: }
110: return list;
111: }
112:
113: }