-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookingPanel.java
96 lines (79 loc) · 3.02 KB
/
BookingPanel.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
package flightscheduleryjw5018.gui;
import flightscheduleryjw5018.data.Flight;
import flightscheduleryjw5018.services.DateUtils;
import flightscheduleryjw5018.services.SchedulingService;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.Collection;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class BookingPanel extends JPanel implements FlightUpdateListener, DayUpdateListener {
private SchedulingService schedulingService;
private final JTextField customer = new JTextField("", 20);
private final JComboBox flightList = new JComboBox();
private final JComboBox dayList = new JComboBox();
public BookingPanel() {
}
public void init() {
setLayout(new FlowLayout());
add(new JLabel("Customer"));
add(customer);
add(new JLabel("Date"));
add(dayList);
add(new JLabel("Flight"));
add(flightList);
JButton bookButton = new JButton("Book");
bookButton.addActionListener((ActionEvent e) -> {
String flight = (String) flightList.getSelectedItem();
Date day = DateUtils.parseDate((String) dayList.getSelectedItem());
String customerName = customer.getText();
boolean booked = false;
try {
booked = schedulingService.bookFlight(flight, day, customerName);
} catch (Exception ex) {
ex.printStackTrace();
}
if (booked) {
// Booked
JOptionPane.showMessageDialog(null, customerName + " has been successfully booked for flight " + flight + " on " + DateUtils.formatDate(day));
} else {
// Added to waiting list
JOptionPane.showMessageDialog(null, "No seats available, " + customerName + " have been put on the waiting list for flight " + flight + " on " + DateUtils.formatDate(day));
}
});
add(bookButton);
updateFlightList();
updateDayList();
}
public void updateFlightList() {
flightList.removeAllItems();
Collection<Flight> flights = schedulingService.findFlights();
for (Flight flight : flights) {
flightList.addItem(flight.getName());
}
}
public void updateDayList() {
dayList.removeAllItems();
Collection<Date> days = schedulingService.getDays();
for (Date day : days) {
String d = DateUtils.formatDate(day);
dayList.addItem(d);
}
}
@Override
public void flightUpdated() {
updateFlightList();
}
@Override
public void dayUpdated() {
updateDayList();
}
public void setSchedulingService(SchedulingService schedulingService) {
this.schedulingService = schedulingService;
}
}