-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeatAdmin.java
402 lines (339 loc) · 21.2 KB
/
SeatAdmin.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
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
public class SeatAdmin extends Admin{
private boolean taken = false;
public SeatAdmin(DatabaseConnection c){connect = c;}
public void admin(){
final JFrame frame = new JFrame("Admin - Seat");
frame.setVisible(true);
frame.setBounds(200, 200, 600, 260);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblNewLabel = new JLabel("Please enter information about the seat. Leave fields blank if you wish to view all seatss.");
JLabel label = new JLabel("");
JLabel lblSeatId = new JLabel("Seat ID (3)");
final JTextField seatField = new JTextField();
seatField.setColumns(10);
JLabel rowLabel = new JLabel("Row (1)");
final JTextField rowField = new JTextField();
rowField.setColumns(10);
JLabel seatNoLabel = new JLabel("Seat Number");
final JTextField seatNoField = new JTextField();
seatNoField.setColumns(10);
JLabel planeIDLabel = new JLabel("Plane ID");
final JTextField planeIDField = new JTextField();
planeIDField.setColumns(10);
final JCheckBox takenBox = new JCheckBox("Taken");
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(takenBox.isSelected())
taken = true;
else if(!takenBox.isSelected())
taken = false;
if(addSeat(seatField.getText(), rowField.getText(), seatNoField.getText() + "", taken, planeIDField.getText()+""))
JOptionPane.showMessageDialog(frame, "Seat " + seatField.getText() + " has been added to database.");
else
JOptionPane.showMessageDialog(frame, "Please enter complete information and make sure seatID and row is not too long and seatID is not already in the database.");
}
});
JButton btnView = new JButton("View and Edit");
btnView.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(takenBox.isSelected())
taken = true;
else if(!takenBox.isSelected())
taken = false;
viewSeat(seatField.getText(), rowField.getText(), seatNoField.getText() + "", taken, planeIDField.getText()+"");
}
});
JButton btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try {
if(deleteSeat(seatField.getText(), planeIDField.getText()))
JOptionPane.showMessageDialog(frame, "Seat(s) " + seatField.getText() + " of plane" + planeIDField.getText() + " got deleted from database.");
else
JOptionPane.showMessageDialog(frame, "Please enter seatID or planeID that exists in the database!");
} catch (SQLException e) {
e.printStackTrace();
}
}
});
JButton btnCloseWindow = new JButton("Close Window");
btnCloseWindow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.dispose();
}
});
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblSeatId)
.addComponent(rowLabel))
.addContainerGap(516, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(label)
.addComponent(lblNewLabel))
.addGap(66))
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(seatNoLabel)
.addComponent(planeIDLabel))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(seatField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(planeIDField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(seatNoField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(takenBox))
.addComponent(rowField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED, 150, Short.MAX_VALUE)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(btnAdd)
.addComponent(btnView)
.addComponent(btnDelete)
.addComponent(btnCloseWindow))
.addContainerGap(58, Short.MAX_VALUE))))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
.addGap(26)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblSeatId)
.addComponent(seatField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(btnAdd))
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(rowLabel)
.addComponent(rowField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(seatNoLabel)
.addComponent(seatNoField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(takenBox))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(planeIDLabel)
.addComponent(planeIDField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(19)
.addComponent(label))
.addGroup(groupLayout.createSequentialGroup()
.addGap(6)
.addComponent(btnView)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btnDelete)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btnCloseWindow)))
.addContainerGap(11, Short.MAX_VALUE))
);
frame.getContentPane().setLayout(groupLayout);
}
public boolean addSeat(String seatID, String row, String seatNo, boolean t, String planeID)
{
ResultSet rs;
String sql = "SELECT sID from Seat";
rs = connect.execute(sql);
try {
while(rs.next())
{
String id = rs.getString("sID");
if(id.equalsIgnoreCase(seatID))
return false;
}
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(seatID.equals("") || row.equals("") || seatNo.equals("") || planeID.equals(""))
return false;
if(seatID.length()>3 || row.length()>1)
return false;
sql = "INSERT INTO Seat(sID, row, seatNo, taken, planeID) "
+ "VALUES(\""
+ seatID
+ "\", \""
+ row
+ "\", "
+ seatNo
+ ", "
+ t
+ ", "
+ planeID + ")";
try {
System.out.println(sql);
connect.executeUpdate(sql);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public void viewSeat(String seatID, String row, String seatNo, boolean t, String planeID)
{
ResultSet rs;
//default sql statement(if no attributes are specified
String sql = "SELECT * FROM Seat";
if(t)
sql = sql + " WHERE taken = true ";
//if any attribute is specified, append WHERE to sql
if(seatID.compareTo("") + row.compareTo("") + seatNo.compareTo("") + planeID.compareTo("")!= 0)
{
if(t)
sql = sql + " AND ";
else if(!t)
sql = sql + " WHERE ";
//append specified attribute to sql
if(seatID.compareTo("") != 0)
sql = sql + "sID = \"" + seatID + "\" AND ";
if(row.compareTo("") != 0)
sql = sql + "row = \"" + row + "\" AND ";
if(seatNo.compareTo("") != 0)
sql = sql + "seatNo = " + seatNo + " AND ";
if(planeID.compareTo("") != 0)
sql = sql + "planeID = \"" + planeID + "\" AND ";
sql = sql.substring(0, sql.length() - 4);
}
//CHECK, DELETE WHEN DONE
System.out.println(sql);
rs = connect.execute(sql);
//Create frame
final JFrame frame = new JFrame();
frame.setBounds(400, 100, 700, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panel = new JPanel();
frame.add(panel);
// get number of rows
int rowNum = AirlineReservationSystem.getRowNum(rs);
//fill Object[][] array with values
int i = 0;
Object[][] data = new Object[rowNum][5];
try {
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.getInt("taken");
data[i][4] = rs.getInt("planeID");
i++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//header table
String columnNames[] = {"sID", "row", "seatNo", "taken", "planeID"};
//fill table
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(650, 300));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
JLabel text = new JLabel("Edit database except for seat ID");
panel.add(text);
panel.add(scrollPane);
//tablemodellisten for editing database
table.getModel().addTableModelListener(new TableModelListener(){
public void tableChanged(TableModelEvent e)
{
int row = e.getFirstRow();
int column = e.getColumn();
if(column == 0)
JOptionPane.showMessageDialog(frame, "Warning: Cannot change sID");
else
{
TableModel model = (TableModel)e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
if(column == 1 && data.toString().length() > 1)
JOptionPane.showMessageDialog(frame, "Warning: Row is only 1 character long.");
else
{
String sID = table.getValueAt(row, 0).toString();
editSeat(columnName, data, sID);
}
}
}
});
JButton closeButton = new JButton("Close");
closeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
});
panel.add(closeButton);
}
/*
* Admin edit Passenger database except for pasID
*/
public void editSeat(String columnName, Object data, String sID){
String sql = "UPDATE Seat SET " + columnName + " = \"" + data + "\" WHERE sID = \"" + sID + "\"";
try {
connect.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean deleteSeat(String sID, String planeID) throws SQLException
{
if(sID.equals("") && planeID.equals(""))
return false;
ResultSet rs;
String sql = "SELECT * FROM Seat WHERE sID = \'" + sID + "\'";
rs = connect.execute(sql);
if(!rs.next())
{
sql = "SELECT * FROM Seat WHERE planeID = \'" + planeID + "\'";
rs = connect.execute(sql);
if(!rs.next())
return false;
}
if(sID.compareTo("") != 0)
sql = "DELETE FROM Seat WHERE sID = \'" + sID + "\'";
else if(planeID.compareTo("") != 0)
sql = "DELETE FROM Seat WHERE planeID = \'" + planeID + "\'";
try {
System.out.println(sql);
connect.executeUpdate(sql);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}