-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlightManagementPanel.java
142 lines (115 loc) · 4.82 KB
/
FlightManagementPanel.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
package flightscheduleryjw5018.gui;
import flightscheduleryjw5018.data.Booking;
import flightscheduleryjw5018.data.Flight;
import flightscheduleryjw5018.services.DateUtils;
import flightscheduleryjw5018.services.SchedulingService;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.NumberFormatter;
public class FlightManagementPanel extends JPanel {
private SchedulingService schedulingService;
private final List<FlightUpdateListener> listeners = new ArrayList();
private final JTable flightTable = new JTable();
public FlightManagementPanel() {
}
public void init() {
setLayout(new BorderLayout());
JButton addButton = new JButton("Add");
addButton.addActionListener((ActionEvent e) -> {
addFlight();
});
JButton editButton = new JButton("Edit");
editButton.setEnabled(false);
editButton.addActionListener((ActionEvent e) -> {
int row = flightTable.getSelectedRow();
String name = (String) flightTable.getValueAt(row, 0);
if (name != null) {
editFlight(name);
}
});
JButton dropButton = new JButton("Drop");
dropButton.addActionListener((ActionEvent e) -> {
int row = flightTable.getSelectedRow();
String name = (String) flightTable.getValueAt(row, 0);
if (name != null) {
dropFlight(name);
}
});
JPanel topPanel = new JPanel();
topPanel.add(addButton);
topPanel.add(editButton);
topPanel.add(dropButton);
flightTable.setModel(new DefaultTableModel(new Object[]{"Name", "Number Of Seats"}, 0));
refreshFlights();
add(topPanel, BorderLayout.NORTH);
add(new JScrollPane(flightTable), BorderLayout.CENTER);
}
public void refreshFlights() {
DefaultTableModel model = (DefaultTableModel) flightTable.getModel();
model.setRowCount(0);
Collection<Flight> flights = schedulingService.findFlights();
flights.forEach((flight) -> {
model.addRow(new Object[]{flight.getName(), flight.getNumberOfSeats()});
});
}
public void addFlight() {
JTextField flightName = new JTextField();
NumberFormat format = NumberFormat.getInstance();
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
formatter.setMaximum(Integer.MAX_VALUE);
formatter.setAllowsInvalid(false);
formatter.setCommitsOnValidEdit(true);
JFormattedTextField flightSeats = new JFormattedTextField(formatter);
Object[] message = {
"Flight:", flightName,
"Number of seats:", flightSeats
};
int option = JOptionPane.showConfirmDialog(null, message, "Add Flight", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
schedulingService.addFlight(flightName.getText(), Integer.valueOf(flightSeats.getText()));
refreshFlights();
notifyListeners();
}
}
public void editFlight(String flightName) {
// Not implemetned yet
}
public void dropFlight(String flightName) {
Collection<Booking> bookings = schedulingService.dropFlight(flightName);
refreshFlights();
notifyListeners();
String msg = flightName + " has been successfully dropped.\n";
for (Booking booking : bookings) {
if (booking.getFlight() != null)
msg += booking.getCustomer() + " has been booked for an alternative flight " + booking.getFlight() + " on " + DateUtils.formatDate(booking.getDate()) + ".\n";
else
msg += booking.getCustomer() + " couldn't get booked for any alternative flight on " + DateUtils.formatDate(booking.getDate()) + " :(\n";
}
JOptionPane.showMessageDialog(null, msg);
}
public void setSchedulingService(SchedulingService bookingService) {
this.schedulingService = bookingService;
}
public void addFlightUpdateListener(FlightUpdateListener listener) {
listeners.add(listener);
}
protected void notifyListeners() {
listeners.forEach(listener -> {
listener.flightUpdated();
});
}
}