-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirlineReservationSystem.java
650 lines (548 loc) · 19.6 KB
/
AirlineReservationSystem.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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.*;
import javax.swing.table.*;
public class AirlineReservationSystem {
// Connection to the mySQL database
static DatabaseConnection connect = new DatabaseConnection(
"jdbc:mysql://localhost/cs157a", "com.mysql.jdbc.Driver", "root",
"1234");
static private String seatID = "";
static private int compareExp = 0;
// static ArrayList<Integer> ids = new ArrayList<Integer>();
public static void main(String[] args) throws SQLException {
//opens main menu, determines whether user is regular user or admin
mainMenu();
}// main
/** mainMenu
* main menu for the application
*
* - user selects between user/admin/exit button
*
*/
public static void mainMenu()
{
JPanel panel = new JPanel();
JLabel label = new JLabel(
" User or Admin? ");
//button declaration
JButton userButton = new JButton("User");
JButton adminButton = new JButton("Admin");
JButton closeButton = new JButton("Exit");
//frame declaration, initialization
final JFrame frame = new JFrame();
frame.setTitle("Airline Reservation System");
frame.setBounds(100, 100, 500, 200);
//User clicked
userButton.addActionListener(new ActionListener() {
//close frame, open user menu
public void actionPerformed(ActionEvent e) {
frame.dispose();
userMenu();
}
});
//admin clicked
adminButton.addActionListener(new ActionListener() {
//close frame, open admin menu
public void actionPerformed(ActionEvent e) {
frame.dispose();
adminMenu();
}
});
//exit clicked
closeButton.addActionListener(new ActionListener(){
//close frame
public void actionPerformed(ActionEvent e){
frame.dispose();
}
});
//add components to panel
panel.add(label);
panel.add(userButton);
panel.add(adminButton);
panel.add(closeButton);
//create container
Container con = frame.getContentPane();
con.add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/** adminMenu
* Menu for administrators
*
*/
public static void adminMenu(){
JPanel panel = new JPanel();
JLabel label = new JLabel(
" What database would you like to access? ");
//button declaration
JButton pilotButton = new JButton("Pilot");
JButton planeButton = new JButton("Plane");
JButton flightButton = new JButton("Flight");
JButton passengerButton = new JButton("Passenger");
JButton seatButton = new JButton("Seat");
//button actionlisteners
pilotButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//adminPilots();
PilotAdmin pa = new PilotAdmin(connect);
pa.admin();
}
});
planeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//adminPlanes();
PlaneAdmin pa = new PlaneAdmin(connect);
pa.admin();
}
});
flightButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//adminFlights();
FlightAdmin fa = new FlightAdmin(connect);
fa.admin();
}
});
passengerButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//adminPassengers();
PassengerAdmin pa = new PassengerAdmin(connect);
pa.admin();
}
});
seatButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//adminSeats();
SeatAdmin sa = new SeatAdmin(connect);
sa.admin();
}
});
//frame declaration, initialization
final JFrame frame = new JFrame();
frame.setTitle("Airline Reservation System");
frame.setBounds(100, 100, 500, 200);
//add components to panel
panel.add(label);
panel.add(pilotButton);
panel.add(planeButton);
panel.add(flightButton);
panel.add(passengerButton);
panel.add(seatButton);
//create container
Container con = frame.getContentPane();
con.add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* getFlights
*
* @return all current flights in ResultSet form
*/
public static ResultSet getFlights() {
String query = "SELECT * FROM Flight";
return connect.execute(query);
}
/**
* getFlightDepTimes
*
* @return all flight departure times in ResultSet form
*/
public static ResultSet getFlightDepTimes() {
String query = "SELECT depTime FROM Flight";
return connect.execute(query);
}
/**
* getSeats
*
* Gets all available(non-taken) seats from a plane
*
* @param s
* - the planeID to get the seats from
* @return available seats in ResultSet form
*/
public static ResultSet getSeats(String s) {
String query = "SELECT * FROM Seat WHERE taken = false AND planeID ="
+ s;
return connect.execute(query);
}
/**
* getRowNum get number of rows in a result set
*
* @param rs
* - ResultSet to operate on
* @return number of rows
*/
public static int getRowNum(ResultSet rs) {
int num = 0;
try {
rs.last();
num = rs.getRow();
rs.beforeFirst();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
/**
* editFlight Performs process of booking a flight
*
* @param b - whether the action is to view or book a flight
* @throws SQLException
*
*/
public static void editFlight(boolean b) throws SQLException {
final JFrame frame = new JFrame();
final boolean isbook = b;
ResultSet rs;
frame.setTitle("Flight Table");
frame.setBounds(300, 100, 800, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panel = new JPanel();
frame.add(panel);
JLabel label = new JLabel("Please select a flight");
panel.add(label);
String[] columnNames = { "Flight ID", "Destination", "Departure Date",
"Departure Time", "Gate" };
// get Flights
rs = getFlights();
// get number of rows
int rowNum = getRowNum(rs);
int i = 0;
Object[][] data = new Object[rowNum][rowNum];
while (rs.next()) {
data[i][0] = rs.getInt("flightID");
data[i][1] = rs.getString("destination");
data[i][2] = rs.getDate("depDate");
data[i][3] = rs.getTime("depTime");
data[i][4] = rs.getInt("planeID");
data[i][5] = rs.getInt("pilotID");
data[i][6] = rs.getString("gateID");
i++;
}
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(750, 200));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
// mouse click listen to select the flight. it will return flightID
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int column = table.getColumnModel().getColumnIndexAtX(
e.getX());
int row = e.getY() / table.getRowHeight();
Object value = "";
if (row < table.getRowCount() && row >= 0
&& column < table.getColumnCount())
value = table.getValueAt(row, 4);
System.out.println(value);
String planeID = value.toString();
value = table.getValueAt(row, 0);
String flightID = value.toString();
try {
bookSeat(planeID, flightID, isbook);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frame.dispose();
}
});
JButton closeButton = new JButton("Close");
closeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
});
panel.add(closeButton);
}// editFlight
/** bookSeat
* This is the menu to book a seat
* @param planeID - ID of plane to book seat on
* @param flightID - ID of flight to book seat for
* @throws SQLException
*/
public static void bookSeat(String planeID, final String flightID, boolean isbook)
throws SQLException {
final JFrame frame = new JFrame();
frame.setBounds(300, 100, 500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panel = new JPanel();
frame.add(panel);
JLabel label = new JLabel(
"Please select a seat and enter your information. 'F' seats = First class.");
final JLabel nameLabel = new JLabel("Name: ");
final JLabel ageLabel = new JLabel("Age: ");
final JTextField nameField = new JTextField(20);
final JTextField ageField = new JTextField(5);
panel.add(label);
// final String chosen = "";
String[] columnNames = { "Seat ID", "Row", "Seat Number", };
// sample data
ResultSet rs = getSeats(planeID);
int rowNum = getRowNum(rs);
int i = 0;
Object[][] data = new Object[rowNum][rowNum];
while (rs.next()) {
data[i][0] = rs.getString("sID");
data[i][1] = rs.getString("Row");
data[i][2] = rs.getInt("seatNo");
data[i][3] = rs.getBoolean("taken");
data[i][4] = rs.getInt("planeID");
i++;
}
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(450, 300));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int column = table.getColumnModel().getColumnIndexAtX(e.getX());
int row = e.getY() / table.getRowHeight();
String value = "";
if (row < table.getRowCount() && row >= 0
&& column < table.getColumnCount())
value = (String) table.getValueAt(row, 0);
seatID = value;
}
});
if(isbook)
{
panel.add(nameLabel);
panel.add(nameField);
panel.add(ageLabel);
panel.add(ageField);
JButton submitButton = new JButton("Submit");
panel.add(submitButton);
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
try {
String age = ageField.getText();
String name = nameField.getText();
System.out.println(name);
int pasNum = 0;
String pasID = "";
while(pasID.equals("") || isInDB(pasID))
{
while (pasNum < 99)
pasNum = (int) (Math.random()*1000);
pasID = pasNum + "C";
}
if(!age.equals("") && !name.equals(""))
{
addSeatQuery(age, name, seatID, flightID, pasID);
JOptionPane.showMessageDialog(frame, "Thank you for using our system! Your passenger ID is " + pasID + ".");
frame.dispose();
}
else
{
JOptionPane.showMessageDialog(frame, "Please enter your information!");
}
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
JButton closeButton = new JButton("Close");
closeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
});
panel.add(closeButton);
}
/*
* checks if pasID is in Passenger table for insert and deletion
*/
public static boolean isInDB(String pasID) throws SQLException
{
ResultSet rs;
//default sql statement(if no attributes are specified
String sql = "SELECT pasID FROM Passenger";
rs = connect.execute(sql);
while(rs.next())
{
if(pasID.equalsIgnoreCase(rs.getString("pasID")))
return true;
}
return false;
}
/** addSeatQuery
*
* Performs insertion of passenger to database
* @param age - age of passenger
* @param name - name of passenger
* @param seatID - seat of passenger
* @param flightID - flight of passenger
*/
public static void addSeatQuery(String age, String name, String seatID,
String flightID, String pasID) {
boolean firstClass = false;
if (seatID.charAt(2) == '7' || seatID.charAt(2) == '8')
firstClass = true;
String sql = "INSERT INTO Passenger(name, age, flightID, firstClass, seatID, pasID) "
+ "VALUES(\""
+ name
+ "\", "
+ age
+ ", "
+ flightID
+ ", "
+ firstClass
+ ", \'"
+ seatID
+ "\', \'"
+ pasID + "\')";
try {
connect.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** userMenu
*
* Creates the menu for a user
*/
public static void userMenu() {
JPanel panel = new JPanel();
JLabel label = new JLabel(
" WELCOME TO AIRLINE RESERVATION SYSTEM ");
JButton cancelRevButton = new JButton("Cancel Reservation");
JButton selectFlightButton = new JButton("Book Flight");
JButton viewButton = new JButton("View Flights");
selectFlightButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
editFlight(true);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
cancelRevButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cancelRev();
}
});
viewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
editFlight(false);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
panel.add(label);
panel.add(selectFlightButton);
panel.add(viewButton);
panel.add(cancelRevButton);
final JFrame frame = new JFrame();
frame.setTitle("Airline Reservation System");
frame.setBounds(100, 100, 500, 200);
JButton closeButton = new JButton("Exit");
closeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
});
panel.add(closeButton);
Container con = frame.getContentPane();
con.add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}// user
/** cancelRev
* This is the menu to cancel a reservation
*/
public static void cancelRev() {
final JFrame frame = new JFrame();
frame.setTitle("Cancel Reservation");
frame.setBounds(300, 100, 300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panel = new JPanel();
frame.add(panel);
final JLabel text = new JLabel("Please enter your passenger ID");
panel.add(text);
final JTextField pasID = new JTextField(10);
panel.add(pasID);
JButton submitButton = new JButton("Submit");
panel.add(submitButton);
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
if(!pasID.getText().equals("") && isInDB(pasID.getText()))
{
cancelSeat(pasID.getText());
JOptionPane.showMessageDialog(frame, "Your reservation has been cancelled.");
frame.dispose();
}
else if(pasID.getText().equals(""))
JOptionPane.showMessageDialog(frame, "Please enter your passenger ID!");
else
JOptionPane.showMessageDialog(frame, "Your passenger ID is not in the system. Please make sure you enter the correct ID!");
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
JButton closeButton = new JButton("Close");
closeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
});
panel.add(closeButton);
}
/** cancelSeat
* Cancels the seat reservation of a passenger(pasID)
* @param pasID - ID of passenger to cancel seat of
*/
public static void cancelSeat(String pasID) {
String sql = "DELETE FROM Passenger WHERE pasID = \'" + pasID + "\'";
// System.out.println(sql);
try {
connect.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}