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
| import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
/**
* Displays the information in an address book.
*
* @author Sabdirova Aidyn
* @version 1.0.0
*/
public class AirSystemGUI extends JPanel {
/* Delimiter */
private final String DELIM = "_";
private static PrintWriter stdErr =
new PrintWriter(System.err, true);
/* Window width in pixels */
static private int WIDTH = 450;
/* Window height in pixels */
static private int HEIGHT = 300;
private JLabel name;
private JLabel date;
private JLabel surname;
private JTextArea nameText;
private JTextArea dateText;
private JTextArea surnameText;
private JButton saveButton;
private JButton cancelButton;
private JButton openButton;
private JButton displayButton;
private JButton orderButton;
private JLabel numberOfEntriesLabel;
private JTextField fileNameTextField;
private JTextField dateOfFlightTextField;
private JTextField numberOfRouteTextField;
private JTextField numberOfFreePlaceTextField;
private JList dateList;
private JTextArea statusTextArea;
private AirSystem airSystem;
/**
* Creates a window.
*
* @param args not used.
*/
public static void main(String[] args) {
JFrame frame = new JFrame("Air System");
frame.setResizable(false);
frame.setContentPane(new AirSystemGUI());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
/**
* Creates a graphical user interface.
*/
public AirSystemGUI() {
setBackground(Color.blue);
// Create the components
openButton = new JButton("Open File");
displayButton = new JButton("Display");
orderButton = new JButton("Order");
numberOfEntriesLabel = new JLabel("0 entries");
fileNameTextField = new JTextField("", 17);
dateOfFlightTextField = new JTextField("", 7);
numberOfRouteTextField = new JTextField("", 5);
numberOfFreePlaceTextField = new JTextField("", 2);
dateList = new JList();
statusTextArea = new JTextArea(4, 50);
// Customize the components
dateList.setVisibleRowCount(8);
dateList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
dateList.setFixedCellHeight(16);
dateList.setFixedCellWidth(130);
numberOfEntriesLabel.setHorizontalAlignment(JLabel.CENTER);
dateOfFlightTextField.setEditable(false);
numberOfRouteTextField.setEditable(false);
numberOfFreePlaceTextField.setEditable(false);
statusTextArea.setEditable(false);
// Register the listeners for the buttons
openButton.addActionListener(new OpenButtonListener());
displayButton.addActionListener(new DisplayButtonListener());
orderButton.addActionListener(new OrderButtonListener());
// North panel
JPanel northPanel = new JPanel();
northPanel.add(new JLabel("File name: "));
northPanel.add(fileNameTextField);
northPanel.add(openButton);
// West panel
JPanel westPanel= new JPanel(new BorderLayout());
westPanel.add(numberOfEntriesLabel,BorderLayout.CENTER);
westPanel.add(new JScrollPane(dateList), BorderLayout.NORTH);
westPanel.add(displayButton, BorderLayout.SOUTH);
JPanel eastPanel= new JPanel(new BorderLayout());
eastPanel.add(orderButton, BorderLayout.SOUTH);
// Center panel
JPanel centerPanel = new JPanel();
JPanel infoPanel = new JPanel(new BorderLayout());
GridLayout layout = new GridLayout(3, 1);
JPanel labelsPanel = new JPanel(layout);
JPanel valuesPanel = new JPanel(layout);
layout.setVgap(10);
labelsPanel.add(new JLabel ("Date Of Flight: "));
labelsPanel.add(new JLabel ("Number Of Route: "));
labelsPanel.add(new JLabel("Number Of Free Place: "));
valuesPanel.add(dateOfFlightTextField);
valuesPanel.add(numberOfRouteTextField);
valuesPanel.add(numberOfFreePlaceTextField);
infoPanel.add(labelsPanel, BorderLayout.WEST);
infoPanel.add(valuesPanel, BorderLayout.CENTER);
centerPanel.add(infoPanel);
// Arrange panels in window
setLayout(new BorderLayout());
add(northPanel, BorderLayout.NORTH);
add(westPanel, BorderLayout.WEST);
add(eastPanel, BorderLayout.EAST);
add(centerPanel, BorderLayout.CENTER);
add(statusTextArea, BorderLayout.SOUTH);
airSystem = new AirSystem();
}
/**
* This inner class handles <code>openButton</code> events.
*/
class OpenButtonListener implements ActionListener {
/**
* Loads address book data from the specified file and displays
* the names in a list. Also displays, beneath the list, the number2
* of names in the address book.
*
* @param event the event object.
*/
public void actionPerformed(ActionEvent event) {
try {
String file = fileNameTextField.getText();
BufferedReader in =
new BufferedReader(new FileReader(file));
/* Removing all entries in the address book if it has any. */
airSystem.removeAllEntries();
/* Clearing the status area. */
statusTextArea.removeAll();
/* Read the first line in the specified file. */
String line = in.readLine();
/* The information of the person in the address book. */
String dateOfFlight = "";
int numberOfRoute;
int numberOfFreePlace;
/* Load the data of the specified file into the empty address book. */
while (line != null) {
StringTokenizer token =
new StringTokenizer(line, DELIM);
if( token.countTokens() != 3 ) {
/* A line does not have the expected number of tokens. */
statusTextArea.setText( "Badly-formed line:"+"\n"+line+"\".");
} else {
/* Load the data of the current line into the now-empty address book. */
dateOfFlight = token.nextToken();
numberOfRoute = Integer.parseInt(token.nextToken());
numberOfFreePlace = Integer.parseInt(token.nextToken());
AirSystemEntry addressBE =
new AirSystemEntry(dateOfFlight,numberOfRoute,numberOfFreePlace);
airSystem.addEntry( addressBE );
}
/* Read next line in the specified file. */
line = in.readLine();
}
/* Close the file. */
in.close();
/* Display the names of the people in the names list. */
dateList.setListData( airSystem.getNames());
/* Update the label that displays the number of people in the address book. */
numberOfEntriesLabel.setText( airSystem.getNumberOfEntries()+"entries" );
/* Clears nameTextField, addressTextField, and telephoneTextField. */
dateOfFlightTextField.removeAll();
numberOfRouteTextField.removeAll();
numberOfFreePlaceTextField.removeAll();
} catch(FileNotFoundException fnfn) {
/* The specified file does not exist. */
statusTextArea.setText( "The file does not exist." );
} catch (IOException ioe) {
/* An error occurs while reading data from the specified file. */
statusTextArea.setText( ioe.toString() );
}
}
}
/**
* This inner class handles displayButton events.
*/
class DisplayButtonListener implements ActionListener {
/**
* Displays the address book entry for the selected person.
*
* @param event the event object.
*/
public void actionPerformed(ActionEvent event) {
if ( airSystem.getNumberOfEntries()==0){
statusTextArea.setText("The empty");
}
else if (dateList.getSelectedIndex()==-1) {
statusTextArea.setText("Please select a date");
}
else if (airSystem.getNumberOfEntries()!=0) {
statusTextArea.setText((airSystem.getNames()[dateList.getSelectedIndex()])+" has been displayed.");
}
dateOfFlightTextField.setText((airSystem.getNames()[dateList.getSelectedIndex()]));
numberOfRouteTextField.setText((airSystem.getEntry((airSystem.getNames()[dateList.getSelectedIndex()])).getNumberOfRoute());
numberOfFreePlaceTextField.setText(airSystem.getEntry((airSystem.getNames()[dateList.getSelectedIndex()])).getNumberOfFreePlace());
}
}
class OrderButtonListener implements ActionListener {
JFrame orderPanel;
public void actionPerformed(ActionEvent event) {
orderPanel = new JFrame("Order");
orderPanel.setResizable(false);
orderPanel.setVisible(true);
orderPanel.setSize(350, 200);
orderPanel.setDefaultCloseOperation(orderPanel.HIDE_ON_CLOSE);
orderPanel.setBackground(Color.BLUE);
surname = new JLabel("Surname:");
name = new JLabel("Name:");
date = new JLabel("Date:");
surnameText = new JTextArea(15,10);
nameText = new JTextArea(15,10);
dateText = new JTextArea(15,10);
surnameText.setBackground(Color.lightGray);
nameText.setBackground(Color.lightGray);
dateText.setBackground(Color.lightGray);
dateText.setText(airSystem.getNames()[dateList.getSelectedIndex()]);
saveButton = new JButton("Save");
cancelButton = new JButton("Cancel");
JPanel all = new JPanel(new GridLayout(2,1));
JPanel text = new JPanel(new GridLayout(5,1));
text.add(surnameText);
text.add(nameText);
text.add(dateText);
JPanel datePanel = new JPanel(new GridLayout(1,6));
text.add(datePanel);
JPanel labels = new JPanel(new GridLayout(5,1));
labels.add(surname);
labels.add(name);
labels.add(date);
JPanel up = new JPanel(new GridLayout(1,2));
up.add(labels);
up.add(text);
JPanel down = new JPanel(new FlowLayout());
down.add(saveButton);
down.add(cancelButton);
down.setBackground(Color.BLUE);
all.add(up);
all.add(down);
orderPanel.add(all);
cancelButton.addActionListener(new cancelButtonListener());
saveButton.addActionListener(new saveButtonListener());
}
class cancelButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
orderPanel.setVisible(false);
}
}
class saveButtonListener implements ActionListener{
/**public int showSaveDialog(Component parent)
throws HeadlessException);*/
private String dateOfFlight;
/* Address of the entry */
private int numberOfRoute;
/* Telephone of the entry */
private int numberOfFreePlace;
public void actionPerformed(ActionEvent event) {
// write here
}
}
}
} |