forked from cj-schultz/CS1555_Term_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerDriver.java
690 lines (592 loc) · 22 KB
/
CustomerDriver.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
import java.sql.*;
import java.lang.InterruptedException;
import java.text.ParseException;
import java.util.Scanner;
public class CustomerDriver
{
public static void Begin(Connection connection)
{
System.out.println("--------------------");
System.out.println("----- Action 1 -----");
System.out.println("--- Add Customer ---");
System.out.println("--------------------\n");
//AddCustomer(connection);
System.out.println("\n--------------------------");
System.out.println("-------- Action 2 --------");
System.out.println("--- Show Customer Info ---");
System.out.println("--------------------------\n");
ShowCustomerInfo(connection);
System.out.println("\n-------------------------------------------------");
System.out.println("-------------------- Action 3 -------------------");
System.out.println("--- Find Price for Flights Between Two Cities ---");
System.out.println("-------------------------------------------------\n");
FindPriceForFlightsBetweenTwoCities(connection);
System.out.println("\n------------------------------------------");
System.out.println("---------------- Action 4 ----------------");
System.out.println("--- Find All Routes Between Two Cities ---");
System.out.println("------------------------------------------\n");
FindAllRoutesBetweenTwoCities(connection);
System.out.println("\n-------------------------------------------------------------");
System.out.println("------------------------- Action 5 --------------------------");
System.out.println("--- Find All Routes Between Two Cities of a Given Airline ---");
System.out.println("-------------------------------------------------------------\n");
FindAllRoutesBetweenTwoCitiesOfGivenAirline(connection);
System.out.println("\n---------------------------------------------------------------");
System.out.println("------------------------- Action 6 ----------------------------");
System.out.println("--- Find All Routes With Available Seats Between Two Cities ---");
System.out.println("---------------------------------------------------------------\n");
FindAllRoutesWithAvailableSeats(connection);
System.out.println("\n----------------------------------------------------------------------------------");
System.out.println("------------------------------------ Action 7 -------------------------------------");
System.out.println("--- Find All Routes With Available Seats Between Two Cities For a Given Airline ---");
System.out.println("-----------------------------------------------------------------------------------\n");
FindAllRoutesWithAvailableSeatsComplicated(connection);
System.out.println("\n-----------------------");
System.out.println("-------- Action 8 -----");
System.out.println("--- Add Reservation ---");
System.out.println("-----------------------\n");
AddReservation(connection);
System.out.println("\n------------------------");
System.out.println("-------- Action 9 ------");
System.out.println("--- Show Reservation ---");
System.out.println("------------------------\n");
ShowReservation(connection);
System.out.println("\n--------------------------------------------");
System.out.println("------------------ Action 10 ----------------");
System.out.println("--- Buy Ticket from Existing Reservation ---");
System.out.println("--------------------------------------------\n");
BuyTicket(connection);
}
private static void AddCustomer(Connection connection)
{
// DB variables
Statement statement = null;
ResultSet resultSet;
String query;
try
{
/* FINDING THE HIGHEST CID ALREADY IN THE DATABASE */
// Select all the customers
statement = connection.createStatement();
query = "SELECT cid from Customer";
resultSet = statement.executeQuery(query);
// Find the highest id, make the new id 1 higher
int highest = 0;
while(resultSet.next())
{
int id = Integer.parseInt(resultSet.getString(1));
if(id > highest)
{
highest = id;
}
}
highest += 1; // The first new cid is 1 higher than the highest
/****************************************************/
// The starting point for the new CIDs
int cid = highest;
for(int i=0; i<200; i++)
{
if(i % 20 == 0)
{
System.out.println("Inserting customer " + i + " into the database");
}
statement = connection.createStatement();
query = "INSERT INTO Customer VALUES ('" + cid + "', 'Mrs', 'Fletcher', 'Zahl', '0925960155412105', to_date('01/01/2015', 'dd/mm/yyyy'), 'Foo', 'Lititz', 'PA', '1234567890', '[email protected]', '005' )";
statement.executeQuery(query);
cid++;
}
}
catch(SQLException e)
{
System.out.println("Error inserting the customer. Error: " + e.toString());
}
finally
{
try
{
if(statement != null) statement.close();
}
catch(SQLException e)
{
System.out.println("Cannot close Statement. Error: " + e.toString());
}
}
}
private static void ShowCustomerInfo(Connection connection)
{
// DB variables
Statement statement = null;
ResultSet resultSet;
String query;
try
{
System.out.println("Printing every 30th customer's name");
// Select all the customers
statement = connection.createStatement();
query = "SELECT * from Customer";
resultSet = statement.executeQuery(query);
int count = 0;
while(resultSet.next())
{
if(count % 30 == 0)
{
System.out.println("Cid: " + resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t" + resultSet.getString(3) + "\t" + resultSet.getString(4));
}
count ++;
}
}
catch(SQLException e)
{
System.out.println("Error inserting the customer. Error: " + e.toString());
}
finally
{
try
{
if(statement != null) statement.close();
}
catch(SQLException e)
{
System.out.println("Cannot close Statement. Error: " + e.toString());
}
}
}
private static void FindPriceForFlightsBetweenTwoCities(Connection connection)
{
// DB variables
Statement statement = null;
ResultSet resultSet;
String query;
try
{
String cityA = "LTZ";
String cityB = "PHI";
// To keep track of the results
int lowAtoB = 0;
int lowBtoA = 0;
int highAtoB = 0;
int highBtoA = 0;
// high low prices for one-way from A to B
statement = connection.createStatement();
query = "SELECT * from PRICE where departure_city = " + "'" + cityA + "'" + " AND arrival_city = " + "'" + cityB + "'";
resultSet = statement.executeQuery(query);
// Print out the information
System.out.println("High and Low prices for a flight from " + cityA + " to " + cityB + ".");
while(resultSet.next())
{
lowAtoB = resultSet.getInt(5);
highAtoB = resultSet.getInt(4);
System.out.println("Low Price: " + lowAtoB + "\tHighPrice: " + highAtoB);
}
// high low prices for one-way from B to A
statement = connection.createStatement();
query = "SELECT * from PRICE where departure_city = " + "'" + cityB + "'" + " AND arrival_city = " + "'" + cityA + "'";
resultSet = statement.executeQuery(query);
// Print out the information
System.out.println("");
System.out.println("High and Low prices for a flight from " + cityB + " to " + cityA + ".");
while(resultSet.next())
{
lowBtoA = resultSet.getInt(5);
highBtoA = resultSet.getInt(4);
System.out.println("Low Price: " + lowBtoA + "\tHighPrice: " + highBtoA);
}
// Print the round trip prices
System.out.println("");
System.out.println("Round Trip prices between city " + cityA + " and city " + cityB + ".");
System.out.println("Low Price: " + (lowAtoB + lowBtoA) + "\tHigh Price: " + (highAtoB + highBtoA));
}
catch(SQLException e)
{
System.out.println("Error inserting the customer. Error: " + e.toString());
}
finally
{
try
{
if(statement != null) statement.close();
}
catch(SQLException e)
{
System.out.println("Cannot close Statement. Error: " + e.toString());
}
}
}
private static void FindAllRoutesBetweenTwoCities(Connection connection)
{
// DB variables
Statement statement = null;
ResultSet resultSet;
String query;
try
{
String src_city = "LTZ";
String dest_city = "PHI";
statement = connection.createStatement();
query = "SELECT flight_number, departure_city, departure_time, arrival_time " +
"FROM Flight " +
"WHERE departure_city= '"+src_city+"' " +
"AND arrival_city = '" +dest_city+"'";
statement.executeQuery(query);
resultSet = statement.executeQuery(query);
System.out.println("Flights from "+src_city+" to "+dest_city);
System.out.println("Flight No.\tDeparture City\tArrival City\tArrival Time");
while (resultSet.next())
{
System.out.println(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t"+ resultSet.getString(3) + "\t" + resultSet.getString(4));
}
}
catch(SQLException Ex)
{
System.out.println("Error running the sample queries. Machine Error: " +
Ex.toString());
}
finally
{
try
{
if (statement != null) statement.close();
}
catch (SQLException e)
{
System.out.println("Cannot close Statement. Machine error: "+e.toString());
}
}
}
private static void FindAllRoutesBetweenTwoCitiesOfGivenAirline(Connection connection)
{
// DB variables
Statement statement = null;
ResultSet resultSet;
String query;
try
{
String src_city = "LTZ";
String dest_city = "PHI";
String airline_name = "Adlair Aviation";
statement = connection.createStatement();
query = "SELECT Flight.airline_id, flight_number, departure_city, departure_time, arrival_time " +
"FROM Flight JOIN Airline ON Flight.airline_id = Airline.airline_id " +
"WHERE departure_city= '"+src_city+"' " +
"AND airline_name= '"+airline_name+"' " +
"AND arrival_city = '" +dest_city+"'";
statement.executeQuery(query);
resultSet = statement.executeQuery(query);
System.out.println("Flights from "+src_city+" to "+dest_city);
System.out.println("Airline ID\tFlight No.\tDeparture City\tArrival City\tArrival Time");
while (resultSet.next())
{
System.out.println(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t"+ resultSet.getString(3) + "\t" + resultSet.getString(4)+ "\t" + resultSet.getString(5));
}
}
catch(SQLException Ex)
{
System.out.println("Error running the sample queries. Machine Error: " +
Ex.toString());
}
finally
{
// CLose the statement
try
{
if (statement != null) statement.close();
}
catch (SQLException e)
{
System.out.println("Cannot close Statement. Machine error: "+e.toString());
}
}
}
private static void FindAllRoutesWithAvailableSeats(Connection connection)
{
// DB variables
Statement statement = null;
ResultSet resultSet;
String query;
try
{
String src_city = "LTZ";
String dest_city = "PHI";
String date = "01/01/2015";
statement = connection.createStatement();
query = "SELECT Flight.flight_number, departure_city, departure_time, arrival_time " +
"FROM Flight " +
"JOIN Reservation_detail ON Flight.flight_number = Reservation_detail.flight_number " +
"JOIN Reservation ON Reservation_detail.reservation_number = Reservation.reservation_number " +
"WHERE departure_city= '"+src_city+"' " +
"AND arrival_city = '" +dest_city+"' " +
"AND flight_date = to_date('"+date+"', 'dd/mm/yyyy') ";
statement.executeQuery(query);
resultSet = statement.executeQuery(query);
System.out.println("Flights from "+src_city+" to "+dest_city);
System.out.println("Flight No.\tDeparture City\tArrival City\tArrival Time");
while (resultSet.next())
{
System.out.println(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t"+ resultSet.getString(3) + "\t" + resultSet.getString(4));
}
}
catch(SQLException Ex)
{
System.out.println("Error running the sample queries. Machine Error: " +
Ex.toString());
}
finally
{
// CLose the statement
try
{
if (statement != null) statement.close();
}
catch (SQLException e)
{
System.out.println("Cannot close Statement. Machine error: "+e.toString());
}
}
}
private static void FindAllRoutesWithAvailableSeatsComplicated(Connection connection)
{
// DB variables
Statement statement = null;
ResultSet resultSet;
String query;
try
{
String src_city = "LTZ";
String dest_city = "PHI";
String airline_name = "Adlair Aviation";
String date = "01/01/2015";
statement = connection.createStatement();
query = "SELECT Flight.airline_id, Flight.flight_number, departure_city, departure_time, arrival_time " +
"FROM Flight " +
"JOIN Airline ON Airline.airline_id = Flight.airline_id " +
"JOIN Reservation_detail ON Flight.flight_number = Reservation_detail.flight_number " +
"JOIN Reservation ON Reservation_detail.reservation_number = Reservation.reservation_number " +
"WHERE departure_city= '"+src_city+"' " +
"AND arrival_city = '" +dest_city+"' " +
"AND airline_name = '" +airline_name+"' " +
"AND flight_date = to_date('"+date+"', 'dd/mm/yyyy') ";
statement.executeQuery(query);
resultSet = statement.executeQuery(query);
System.out.println("Flights from "+src_city+" to "+dest_city);
System.out.println("Airline ID\tFlight No.\tDeparture City\tArrival City\tArrival Time");
while (resultSet.next())
{
System.out.println(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t"+ resultSet.getString(3) + "\t" + resultSet.getString(4)+ "\t" + resultSet.getString(5));
}
}
catch(SQLException Ex)
{
System.out.println("Error running the sample queries. Machine Error: " +
Ex.toString());
}
finally
{
// CLose the statement
try
{
if (statement != null) statement.close();
}
catch (SQLException e)
{
System.out.println("Cannot close Statement. Machine error: "+e.toString());
}
}
}
private static void AddReservation(Connection connection)
{
// DB variables
Statement statement = null;
ResultSet resultSet;
String query;
try
{
System.out.println("Adding 50 flights...\n");
String flightNum = "1";
String deptDate = "03/09/2016"; // max of 4 legs
// We now have all the data, make sure there are enough seats
boolean enoughSeats = true;
for(int i=0; i<50; i++)
{
int maxCapacity;
// Find the max capacity
statement = connection.createStatement();
query = "SELECT plane_type, airline_id from Flight where flight_number = " + flightNum;
resultSet = statement.executeQuery(query);
resultSet.next();
statement = connection.createStatement();
query = "SELECT plane_capacity from Plane where plane_type = " + "'" + resultSet.getString(1) + "'" + " AND owner_id = " + "'" + resultSet.getString(2) + "'";
resultSet = statement.executeQuery(query);
resultSet.next();
maxCapacity = resultSet.getInt(1);
// Find the current capacity
statement = connection.createStatement();
query = "SELECT COUNT(*) from Reservation_detail where flight_number = " + flightNum;
resultSet = statement.executeQuery(query);
resultSet.next();
int flightCap = resultSet.getInt(1);
// The plane is full
if(flightCap >= maxCapacity)
{
enoughSeats = false;
System.out.println("There are not enough seats on the flight with flight number " + flightNum);
break;
}
if(enoughSeats)
{
// Assign a unique cid
String rNumber;
// Select all the customers
statement = connection.createStatement();
query = "SELECT reservation_number from reservation";
resultSet = statement.executeQuery(query);
// Find the highest id, make the new id 1 higher
int highest = 0;
while(resultSet.next())
{
int id = Integer.parseInt(resultSet.getString(1));
if(id > highest)
{
highest = id;
}
}
highest += 1; // The new cid is 1 higher than the highest
rNumber = (highest + i) + "";
System.out.println("Your reservation has been made. Your unique reservation number is " + rNumber);
if(statement != null) statement.close();
}
}
}
catch(SQLException e)
{
System.out.println("Error adding the reservation. Error: " + e.toString());
}
finally
{
try
{
if(statement != null) statement.close();
}
catch(SQLException e)
{
System.out.println("Cannot close Statement. Error: " + e.toString());
}
}
}
private static void ShowReservation(Connection connection)
{
// DB variables
Statement statement = null;
ResultSet resultSet;
String query;
try
{
String resNumber = "91";
// Get the reservation
statement = connection.createStatement();
query = "SELECT * from Reservation where reservation_number = " + resNumber;
resultSet = statement.executeQuery(query);
// Reservation info
String startCity = "";
String endCity = "";
int legs = 0;
String[] flightNumbers = new String[10]; // just hardcode 10
int count = 0;
while(resultSet.next())
{
startCity = resultSet.getString(7);
endCity = resultSet.getString(8);
count ++;
}
// Exit if it doesn't exist
if(count == 0)
{
System.out.println("The reservation " + resNumber + " does not exist in the database.");
return;
}
// Get all legs of the reservation
statement = connection.createStatement();
query = "SELECT * from Reservation_detail where reservation_number = " + resNumber;
resultSet = statement.executeQuery(query);
// Get all the leg info
while(resultSet.next())
{
int leg = resultSet.getInt(4);
flightNumbers[leg] = resultSet.getString(2);
legs ++;
}
// Print the initial info
System.out.println("Reservation " + resNumber + " has " + legs + " connecting flights between " + startCity + " and " + endCity + ".");
for(int i=0; i<legs; i++)
{
// Get the flight for this leg
statement = connection.createStatement();
query = "SELECT * from Flight where flight_numer = " + flightNumbers[i];
resultSet = statement.executeQuery(query);
resultSet.next();
System.out.println("Leg " + i + " is a flight from " + resultSet.getString(4) + " to " + resultSet.getString(5) + ".");
}
}
catch(SQLException e)
{
System.out.println("Error inserting the customer. Error: " + e.toString());
}
finally
{
try
{
if(statement != null) statement.close();
}
catch(SQLException e)
{
System.out.println("Cannot close Statement. Error: " + e.toString());
}
}
}
private static void BuyTicket(Connection connection)
{
// DB variables
Statement statement = null;
ResultSet resultSet;
String query;
try
{
String resNumber = "85";
connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
// Update the reservation
statement = connection.createStatement();
query = "UPDATE Reservation set ticketed = 'Y' where reservation_number = " + resNumber;
int result = statement.executeUpdate(query);
// Sleep for 4
Thread.sleep(4000);
statement = connection.createStatement();
query = "SELECT ticketed from Reservation where reservation_number = " + resNumber;
resultSet = statement.executeQuery(query);
while(resultSet.next())
{
if(resultSet.getString(1).equals("Y"))
{
System.out.println("The ticket for reservation " + resNumber + " has been bought!");
}
}
}
catch(SQLException e)
{
System.out.println("Error inserting the customer. Error: " + e.toString());
}
catch(InterruptedException e)
{
System.out.println("Interruption error. Error: " + e.toString());
}
finally
{
try
{
if(statement != null) statement.close();
}
catch(SQLException e)
{
System.out.println("Cannot close Statement. Error: " + e.toString());
}
}
}
}