-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaneAdmin.java
349 lines (279 loc) · 16.4 KB
/
PlaneAdmin.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
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
public class PlaneAdmin extends Admin{
public PlaneAdmin(DatabaseConnection c)
{
connect = c;
}
public void admin(){
final JFrame frame = new JFrame("Admin - Plane");
frame.setVisible(true);
frame.setBounds(200, 200, 578, 256);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblNewLabel = new JLabel("Please enter information about the pilot. Leave fields blank if you wish to view all pilots.");
JLabel label = new JLabel("");
JButton btnAdd = new JButton("Add");
JButton btnView = new JButton("View and Edit");
JButton btnDelete = new JButton("Delete");
JButton btnCloseWindow = new JButton("Close Window");
JLabel idLabel = new JLabel("Plane ID");
final JTextField idField = new JTextField();
idField.setColumns(10);
JLabel ageLabel = new JLabel("Plane Age");
final JTextField ageField = new JTextField();
ageField.setColumns(10);
final JCheckBox olderBox = new JCheckBox("Older Than");
final JCheckBox youngerBox = new JCheckBox("Younger Than");
btnDelete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try {
if(deletePlane(idField.getText()))
JOptionPane.showMessageDialog(frame, "Plane " + idField.getText() +" has been deleted from the database.");
else
JOptionPane.showMessageDialog(frame, "Please enter a Plane ID that exists in the database!");
} catch (SQLException e) {
e.printStackTrace();
}
}
});
btnAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(addPlane(ageField.getText(), idField.getText()))
JOptionPane.showMessageDialog(frame, "Plane " + idField.getText() + " has been added to database.");
else
JOptionPane.showMessageDialog(frame, "Please enter unused planeID and an age!");
}
});
btnView.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(!ageField.getText().equals("") && olderBox.isSelected() && youngerBox.isSelected())
JOptionPane.showMessageDialog(frame, "Please only pick either greater or less than!");
else
{
int compareExp;
if(olderBox.isSelected())
compareExp = 1;
else if(youngerBox.isSelected())
compareExp = -1;
else
compareExp = 0;
viewPlanes(idField.getText(), ageField.getText() +"", compareExp);
}
}
});
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(label)
.addComponent(lblNewLabel)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(idLabel)
.addGap(18)
.addComponent(idField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(ageLabel)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(ageField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addComponent(btnAdd)
.addGap(34)
.addComponent(btnView)))
.addGap(33)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(olderBox)
.addComponent(btnDelete))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(btnCloseWindow)
.addGap(46))
.addComponent(youngerBox))))
.addContainerGap(68, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(idLabel)
.addComponent(idField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(ageLabel)
.addComponent(ageField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(olderBox)
.addComponent(youngerBox))
.addGap(50)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(btnAdd)
.addComponent(btnView)
.addComponent(btnDelete)
.addComponent(btnCloseWindow))
.addGap(51)
.addComponent(label)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
frame.getContentPane().setLayout(groupLayout);
}
public boolean deletePlane(String id) throws SQLException {
if (id.equals(""))
return false;
ResultSet rs;
// default sql statement(if no attributes are specified
String sql = "SELECT * FROM Plane WHERE planeID = " + id + "";
rs = connect.execute(sql);
if (!rs.next()) {
return false;
}
if (id.compareTo("") != 0)
sql = "DELETE FROM Plane WHERE planeID = \'" + id + "\'";
try {
System.out.println(sql);
connect.executeUpdate(sql);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public boolean addPlane(String age, String id) {
if (age.equals("") || id.equals(""))
return false;
String sql = "INSERT INTO Plane VALUES(" + id + ", " + age + ")";
String sqlCheck = "SELECT * FROM PLANE WHERE planeID = " + id;
System.out.println(sqlCheck);
ResultSet rs;
rs = connect.execute("SELECT * FROM PLANE WHERE planeID = " + id);
try {
if(AirlineReservationSystem.getRowNum(rs) == 0)
{
connect.executeUpdate(sql);
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}// addPilot
public void viewPlanes(String pID, String age, int compareExp) {
ResultSet rs;
// default sql statement(if no attributes are specified
String sql = "SELECT * FROM Plane";
// if any attribute is specified, append WHERE to sql
if (pID.compareTo("") + age.compareTo("") != 0) {
sql = sql + " WHERE ";
// append specified attribute to sql
if (pID.compareTo("") != 0)
sql = sql + "planeID = " + pID + " AND ";
if (age.compareTo("") != 0 && compareExp == 0)
sql = sql + "age = " + age + " AND ";
else if (age.compareTo("") != 0 && compareExp > 0)
sql = sql + "age > " + age +" AND ";
else if (age.compareTo("") != 0 && compareExp < 0)
sql = sql + "age < " + age + " 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, 500, 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][2];
try {
while (rs.next()) {
data[i][0] = rs.getString("planeID");
data[i][1] = rs.getInt("age");
i++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// header table
String columnNames[] = { "Plane ID", "Age" };
// fill table
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(450, 300));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
JLabel text = new JLabel("Edit database except for PlaneID");
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, "Cannot change planeID");
else
{
TableModel model = (TableModel) e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
String planeID = (String) table.getValueAt(row, 0);
editPlane(columnName, data, planeID);
}
}
});
JButton closeButton = new JButton("Close");
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
panel.add(closeButton);
}// viewPilots
public void editPlane(String columnName, Object data, String planeID) {
String sql = "UPDATE Plane SET " + columnName + " = " + data
+ " WHERE planeID = " + planeID;
System.out.println(sql);
try {
connect.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}