-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicketSales.java
More file actions
404 lines (364 loc) · 13 KB
/
Copy pathTicketSales.java
File metadata and controls
404 lines (364 loc) · 13 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
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
import java.io.IOException;
import java.util.ArrayList;
/**
* Class that holds the Theater and Movie objects and a Price Guide for the
* movies. This class is able to Initialize a cinema, process orders, and report
* sales.
*
* @author Alfred Ababio
* @version K12
*/
public class TicketSales {
/** The list of movies */
ArrayList<Movie> movies;
/** The list of theaters */
ArrayList<Theater> theaters;
/** The list of actual screenings for the day */
ArrayList<Showing> shows;
/** The Map of Prices */
ArrayList<Ticket> priceGuide;
/** The Report Number */
Integer repNum;
/** Sales Report */
String log;
/** Error Log */
String errLog;
/**
* Constructor for a TicketSales class
*/
public TicketSales() {
repNum = 1;
log = "";
errLog = "";
}
/**
* This method will initialize an entire Movie Complex (the same as opening
* the Theater for the day) with the data from the fileName in the String
*
* @param fileName The name of the file to initialize the theater from
*/
public void initCinema(String fileName) {
/** Will read the file for me */
ReadFile crf = new ReadFile(fileName);
/** Reads to this ArrayList */
ArrayList<String> stuffs = new ArrayList<String>();
ArrayList<String> moviestr = new ArrayList<String>();
ArrayList<String> theaterstr = new ArrayList<String>();
ArrayList<String> showstr = new ArrayList<String>();
try {
stuffs = crf.openFile();
moviestr = breakStuff(stuffs, "Theaters");
this.movies = makeMovies(moviestr);
stuffs.removeAll(moviestr);
theaterstr = breakStuff(stuffs, "Shows");
this.theaters = buildTheaters(theaterstr);
stuffs.removeAll(theaterstr);
showstr = breakStuff(stuffs, "Prices");
this.shows = createShowings(showstr);
stuffs.removeAll(showstr);
this.priceGuide = setPrices(stuffs);
}
catch (IOException e) {
this.errLog += e.toString() + "<-- Read error messages." + "\n";
}
}
/**
* This function will process the orders from a file
* @param fileName The file to read orders from
*/
public void processOrders(String fileName) {
/** Will read the file for me */
ReadFile crf = new ReadFile(fileName);
/** Reads to this ArrayList */
ArrayList<String> stuffs = new ArrayList<String>();
try {
stuffs = crf.openFile();
// Now we have an ArrayList of every order.
for (int i = 0; i < stuffs.size(); i++) {
if (stuffs.get(i).equals("report")) {
this.log += this.reportOnce();
}
else {
this.salesReportHelper(stuffs.get(i));
}
}
}
catch (IOException e) {
this.errLog += e.toString() + "<-- Read error messages." + "\n";
}
}
/**
* This function will essentially only print the sales
* It does this by looking at the log and filtering out all the reports
* @return String of only sales made
*/
public String reportSales() {
String[] hate = this.log.split("\n");
String repor = "";
for (int a = 0; a < hate.length; a++) {
String[] c = hate[a].split(" ");
if (c[0].equals("Report")) {
a += this.shows.size();
}
// If the first thing was a number then it is
// an order that should be added to this list
String[] q = c[0].split(",");
if (q[0].matches("\\d")) {
repor += hate[a] + "\n";
}
}
return repor;
}
/**
* Function to output all of the reports that the manager has
* requested
* @return String all of the reports the manager has requested.
*/
public String managerReport() {
String[] ood = this.log.split("\n");
String bah = "";
for (int a = 0; a < ood.length; a++) {
String[] gdk = ood[a].split(" ");
if (gdk[0].equals("Report")) {
for (int b = 0; b <= this.shows.size(); b++) {
bah += ood[a + b] + "\n";
}
a += this.shows.size();
}
}
return bah;
}
/**
* Another manager report helper
* @return String that is the report
*/
public String consoleManageReport() {
String csshhh = "Report " + repNum + "\n";
for (Showing s : this.shows) {
csshhh += this.showingReport(s) + "\n";
}
this.repNum++;
return csshhh;
}
/**
* Prints the log of everything that has happened since initialization
* @return String the log of this TicketSales
*/
public String logReport() {
return this.errLog;
}
/**
* A helper method for salesReport to add to the log correctly.
* @param string The line of the order to process and log
*/
private void salesReportHelper(String string) {
String[] w = string.split(",");
try {
Showing s = new Showing(
this.movies.get(Integer.parseInt(w[0]) - 1),
this.theaters.get(Integer.parseInt(w[1]) - 1),
Integer.parseInt(w[2]));
int k = this.shows.indexOf(s);
/** Finds how many tickets total were purchased */
int sum = 0;
for (int a = 3; a < w.length; a++) {
sum += Integer.parseInt(w[a]);
}
int g = this.shows.get(k).theater.seatsLeft;
/** Subtract the seats left from the Theater */
if (g >= sum) {
this.shows.get(k).theater.seatsLeft -= sum;
int total = 0;
for (int h = 3; h < w.length; h++) {
total += Integer.parseInt(w[h]) *
this.priceGuide.get((h - 3)).price;
}
// Update this log to show the purchase was complete
this.log += string + "," + total + "\n";
// Add the order to the theater log
String o = "";
for (int a = 3; a < w.length; a++) {
o += w[a] + ",";
}
o = o.substring(0, o.length() - 1);
this.shows.get(k).orders.add(o);
}
/** Otherwise it's sold out */
else {
this.log += string + "," + "0" + "\n";
}
}
catch (Exception e) {
this.errLog += "Incorrectly formatted order: " + string + "\n";
}
}
/**
* This outputs one manager report
* @return A manager report for the sales of every Show
*/
private String reportOnce() {
String s = "";
s += "\n";
s += "Report " + this.repNum.toString() + "\n";
for (Showing show : this.shows) {
s += this.showingReport(show) + "\n";
}
this.repNum++;
return s;
}
/**
* Returns a report of a Showing
* [Movie #,Theater #,Start,Ticket[0] #,...Ticket[n] #]
* @param s Showing to take report of
* @return String report of this showing
*/
public String showingReport(Showing s) {
String str = "";
/** First get the Movie */
str += s.movie.name + ",";
/** Now get the Theater */
str += s.theater.name + ",";
/** Get the start time */
String start = s.start.toString();
str += start + ",";
/** Get the maximum capacity for the Theater */
str += s.theater.seatsMax + ",";
/** Now lets pull the tickets */
//Above initializes ArrayList with 0 values for each ticket type
ArrayList<Integer> ot = new ArrayList<Integer>();
for (int c = 0; c < this.priceGuide.size(); c++) {
ot.add(0);
}
//Now let's add each ticket amount to the list so we total up the
//types of each ticket purchased.
for (int a = 0; a < s.orders.size(); a++) {
String[] w = s.orders.get(a).split(",");
for (int z = 0; z < w.length; z++) {
ot.set(z, Integer.parseInt(w[z]) + ot.get(z));
}
}
//Turn the ArrayList back to a String
String tix = "";
for (int b = 0; b < ot.size(); b++) {
tix += ot.get(b) + ",";
}
// Add the string of tickets to the output str
str += tix;
/** Getting the seats left */
/**str += s.theater.seatsLeft.toString(); */
str = str.substring(0, str.length() - 1);
return str;
}
/**
* This basically does sublist but by elements in the list
* @param alist The list to be broken
* @param term Broken up to this term
* @return ArrayList<String> that is the list we wanted
*/
public ArrayList<String> breakStuff(ArrayList<String> alist, String term) {
int i = 0;
ArrayList<String> strarr = new ArrayList<String>();
while (!alist.get(i).equals(term)) {
strarr.add(alist.get(i));
i++;
}
return strarr;
}
/**
* Turns every element except 0-th into a Movie object because 0 is "Movie"
* The List must be formatted as [Movies, Name1:Time1, Name2:Time2, ...]
*
* @param alist
* ArrayList<String> of Movies as Strings
* @return ArrayList<Movie> with Strings turned into Movies
*/
public ArrayList<Movie> makeMovies(ArrayList<String> alist) {
ArrayList<Movie> mlist = new ArrayList<Movie>();
for (int a = 1; a < alist.size(); a++) {
try {
mlist.add(Movie.fromString(alist.get(a)));
}
catch (Exception e) {
this.errLog += "Incorrectly formatted string: "
+ alist.get(a) + "\n";
}
}
return mlist;
}
/**
* Builds the theater rooms from the string representation of them The List
* must be formatted as [Theaters, Name1:Capacity1, Name2:Capacity2, ...]
*
* @param alist
* The list of theaters
* @return ArrayList<Theater> The list of theaters
*/
public ArrayList<Theater> buildTheaters(ArrayList<String> alist) {
ArrayList<Theater> tlist = new ArrayList<Theater>();
for (int a = 1; a < alist.size(); a++) {
try {
tlist.add(Theater.fromString(alist.get(a)));
}
catch (Exception e) {
this.errLog += "Incorrectly formatted string: "
+ alist.get(a) + "\n";
}
}
return tlist;
}
/**
* Creates a list of Showings from the String list given It does handles
* conflicting Shows and will not add conflicts
*
* @param alist
* The list of existing shows
* @return ArrayList<Showing> the list of shows already in
*/
public ArrayList<Showing> createShowings(ArrayList<String> alist) {
ArrayList<Showing> slist = new ArrayList<Showing>();
for (int a = 1; a < alist.size(); a++) {
try {
String[] w = alist.get(a).split(",");
Showing s = new Showing(
this.movies.get(Integer.parseInt(w[0]) - 1),
new Theater(
this.theaters.get(Integer.parseInt(w[1]) - 1)),
Integer.parseInt(w[2]));
if (!s.conflicts(slist)) {
slist.add(s);
}
else {
this.errLog += "Conflicting Showing not added: "
+ alist.get(a) + "\n";
}
}
catch (Exception e) {
this.errLog += "Incorrectly formatted string: "
+ alist.get(a) + "\n";
}
}
return slist;
}
/**
* This function reads in the prices and creates a nice lookup table of
* prices to work with
*
* @param slist
* The arrayList of strings to work with
* @return HashMap<String, Integer> That will act as a table
*/
public ArrayList<Ticket> setPrices(ArrayList<String> slist) {
ArrayList<Ticket> hm = new ArrayList<Ticket>();
for (int i = 1; i < slist.size(); i++) {
try {
String[] w = slist.get(i).split(":");
hm.add(new Ticket(w[0], Integer.parseInt(w[1])));
}
catch (Exception e) {
this.errLog += "Incorrectly formatted string: " +
slist.get(i) + "\n";
}
}
return hm;
}
}