-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCasting.java
220 lines (169 loc) · 6.61 KB
/
Casting.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
import java.io.*;
import java.util.*;
public class Casting {
// new comparator/sorting method to sort the final results, by number of available casts
public static SortedSet<Map.Entry<String, List<String>>> sortByValues(Map<String, List<String>> map) {
SortedSet<Map.Entry<String, List<String>>> sortedEntries = new TreeSet<Map.Entry<String, List<String>>>(
new Comparator<Map.Entry<String, List<String>>>() {
@Override public int compare(Map.Entry<String, List<String>> e1, Map.Entry<String, List<String>> e2) {
Integer x = (Integer) e1.getValue().size();
Integer y = (Integer) e2.getValue().size();
int res = x.compareTo(y);
return res != 0 ? res : 1;
}
});
sortedEntries.addAll(map.entrySet());
return sortedEntries;
}
// Method that does most of the casting work
// Command line arguments = dancer conflicts
public static void main(String[] args) {
// get rehearsal schedule
Schedule rehearsalSchedule = new Schedule("Rehearsal Times");
TreeMap<String, List<String>> rehearsalBreakdown = new TreeMap<String, List<String>>();
String rehearsalFileName = "Rehearsals.txt";
Scanner reader;
try {
reader = new Scanner(new File(rehearsalFileName));
while (reader.hasNextLine()) {
String[] currentDay = reader.nextLine().split(", ");
int length = currentDay.length;
for (int i = 1; i < length-2; i=i+3) {
Event ev = new Event(Integer.parseInt(currentDay[i]), Integer.parseInt(currentDay[i+1]), currentDay[i+2]);
rehearsalSchedule.addRehearsal(currentDay[0], ev);
rehearsalBreakdown.put(new String(currentDay[0] + ", " + ev.toString()), new ArrayList<String>());
}
}
}
catch(Exception e) {
System.out.println("Error opening/reading rehearsal schedule. Please try again.");
//e.printStackTrace();
return;
}
//rehearsalSchedule.show();
// for (Map.Entry<String, List<String>> entry : rehearsalBreakdown.entrySet()) {
// System.out.println(entry.getKey());
// }
//get conflicts for each person
int N = args.length;
List<Schedule> dancers = new ArrayList<Schedule>();
for (int x = 0; x < N; x++) {
try {
reader = new Scanner(new File(args[x]));
String[] dancerName = args[x].split("\\W+");
Schedule dancer = new Schedule(dancerName[1]);
//System.out.println(dancer.name() + ": ");
while (reader.hasNextLine()) {
String[] currentDay = reader.nextLine().split(", ");
int length = currentDay.length;
for (int i = 1; i < length-2; i=i+3) {
Event ev = new Event(Integer.parseInt(currentDay[i]), Integer.parseInt(currentDay[i+1]), currentDay[i+2]);
dancer.add(currentDay[0], ev);
}
}
dancers.add(dancer);
}
catch(Exception e) {
System.out.println("Error opening/reading conflicts. Please try again.");
}
//dancer.show();
//System.out.println();
}
//System.out.println(dancers.size());
// create consolidated schedule for each cast
List<Schedule> casts = new ArrayList<Schedule>();
try {
reader = new Scanner(new File("Casting.txt"));
while (reader.hasNextLine()) {
String[] currentCast = reader.nextLine().split(", ");
int length = currentCast.length;
ArrayList<String> casting = new ArrayList<String>(Arrays.asList(Arrays.copyOfRange(currentCast, 1, length)));
Schedule cast = new Schedule(currentCast[0]);
for (String name : casting) {
for (Schedule dancer : dancers) {
if (name.equals(dancer.name())) {
//System.out.println("found");
dancer.addTo(cast);
}
}
}
casts.add(cast);
}
}
catch(Exception e) {
System.out.println("Error opening/reading casting information. Please try again.");
}
//System.out.println(casts.size());
// for (Schedule cast : casts) {
// cast.show();
// }
// get and print the available rehearsal times for each cast
TreeMap<String, Schedule> castAvailabilities = new TreeMap<String, Schedule>();
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("CastingOutput.txt"), "utf-8"));
String finalBreakdown1 = "Available Rehearsals for Each Cast\n\n";
for (Schedule cast : casts) {
Schedule rehearsalTimes = rehearsalSchedule.freeTimes(cast.name(), cast);
castAvailabilities.put(cast.name(), rehearsalTimes);
//writer.write(rehearsalTimes.toString());
finalBreakdown1 += rehearsalTimes.toString();
}
// check how many casts are free on each day/rehearsal
String[] daysOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int length = daysOfWeek.length;
// go through each casts schedule
for (Map.Entry<String, Schedule> entry : castAvailabilities.entrySet()) {
Schedule current = entry.getValue(); // current cast's schedule
// check the data for each day of the week
for (int i = 0; i < length; i++) {
// get the current day (and all its events)
// for both the cast and rehearsal
Day currentDay = current.get(daysOfWeek[i]);
Queue<Event> currentEvents = currentDay.allEvents();
Day rehearsalDay = rehearsalSchedule.get(daysOfWeek[i]);
Queue<Event> rehearsals = rehearsalDay.allEvents();
// note for which rehearsals the cast is free
for (Event ev : rehearsals) {
for (Event e : currentEvents) {
// check if events are the same
if (ev.getStart() == e.getStart() && ev.getEnd() == e.getEnd()) {
List<String> availableCasts = rehearsalBreakdown.get(daysOfWeek[i] + ", " + ev.toString());
availableCasts.add(entry.getKey());
}
}
}
}
}
// for(int i = 0; i < length; i++) {
// System.out.println("# of Casts Available on " + daysOfWeek[i] + ": " + available[i]);
// }
// sort then print the breakdown
SortedSet<Map.Entry<String, List<String>>> sortedRehearsals = sortByValues(rehearsalBreakdown);
String finalBreakdown2 = "Available Casts for Each Rehearsal\n\n";
for (Map.Entry<String, List<String>> entry : sortedRehearsals) {
String rehearsal = entry.getKey();
List<String> availCasts = entry.getValue();
finalBreakdown2 += rehearsal;
finalBreakdown2 += "\n======================\n";
for (String s : availCasts) {
finalBreakdown2 += s;
finalBreakdown2 += ", ";
}
finalBreakdown2 += "\n\n";
}
writer.write(finalBreakdown1);
writer.write("\n");
writer.write(finalBreakdown2);
//String formattedOutput = "%40s %s";
//writer.write(String.format(formattedOutput, finalBreakdown1, finalBreakdown2));
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {writer.close();} catch(Exception e) {}
}
// save all free times to text file?
}
}