-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnlineReservationSystem.java
396 lines (343 loc) · 14.1 KB
/
OnlineReservationSystem.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
public class OnlineReservationSystem extends JFrame {
private Map<String, String> users;
private Map<String, Reservation> reservations;
private JPanel registerPanel;
private JPanel loginPanel;
private JPanel reservationPanel;
private JPanel cancellationPanel;
private JMenuBar menuBar;
private JTextField newUsernameField;
private JPasswordField newPasswordField;
private JTextField usernameField;
private JPasswordField passwordField;
private JTextField nameField;
private JTextField trainNumberField;
private JTextField classTypeField;
private JTextField dateField;
private JTextField sourceField;
private JTextField destinationField;
private JTextField pnrField;
public OnlineReservationSystem() {
users = new HashMap<>();
reservations = new HashMap<>();
setTitle("Online Reservation System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
createRegisterPanel();
createLoginPanel();
createReservationPanel();
createCancellationPanel();
createMenuBar();
showRegisterPanel();
}
private void showRegisterPanel() {
setContentPane(registerPanel);
setSize(600, 400);
setJMenuBar(null);
revalidate();
repaint();
}
private void showLoginPanel() {
setContentPane(loginPanel);
setSize(600, 400);
setJMenuBar(null);
revalidate();
repaint();
}
private void showReservationPanel() {
setContentPane(reservationPanel);
setSize(600, 400);
setJMenuBar(menuBar);
revalidate();
repaint();
}
private void showCancellationPanel() {
setContentPane(cancellationPanel);
setSize(600, 400);
setJMenuBar(menuBar);
revalidate();
repaint();
}
private void createRegisterPanel() {
registerPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(10, 10, 10, 10);
JLabel usernameLabel = new JLabel("Username:");
gbc.gridx = 0;
gbc.gridy = 0;
registerPanel.add(usernameLabel, gbc);
JLabel passwordLabel = new JLabel("Password:");
gbc.gridy = 1;
registerPanel.add(passwordLabel, gbc);
newUsernameField = new JTextField(20);
gbc.gridx = 1;
gbc.gridy = 0;
registerPanel.add(newUsernameField, gbc);
newPasswordField = new JPasswordField(20);
gbc.gridy = 1;
registerPanel.add(newPasswordField, gbc);
JButton registerButton = new JButton("Register");
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
registerButton.setBackground(Color.GREEN);
registerButton.setForeground(Color.BLACK);
registerPanel.add(registerButton, gbc);
JButton exitButton = new JButton("Exit");
gbc.gridy = 3;
exitButton.setBackground(Color.RED);
exitButton.setForeground(Color.WHITE);
registerPanel.add(exitButton, gbc);
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = newUsernameField.getText();
String password = new String(newPasswordField.getPassword());
if (users.containsKey(username)) {
JOptionPane.showMessageDialog(null, "Username already exists. Please choose a different username.", "Registration Error", JOptionPane.ERROR_MESSAGE);
} else {
users.put(username, password);
JOptionPane.showMessageDialog(null, "Registration successful! You can now login with your new credentials.", "Registration", JOptionPane.INFORMATION_MESSAGE);
newUsernameField.setText("");
newPasswordField.setText("");
showLoginPanel();
}
}
});
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
private void createLoginPanel() {
loginPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(10, 10, 10, 10);
JLabel usernameLabel = new JLabel("Username:");
gbc.gridx = 0;
gbc.gridy = 0;
loginPanel.add(usernameLabel, gbc);
JLabel passwordLabel = new JLabel("Password:");
gbc.gridy = 1;
loginPanel.add(passwordLabel, gbc);
usernameField = new JTextField(20);
gbc.gridx = 1;
gbc.gridy = 0;
loginPanel.add(usernameField, gbc);
passwordField = new JPasswordField(20);
gbc.gridy = 1;
loginPanel.add(passwordField, gbc);
JButton loginButton = new JButton("Login");
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
loginPanel.add(loginButton, gbc);
loginButton.setBackground(Color.GREEN);
loginButton.setForeground(Color.BLACK);
JButton exitButton = new JButton("Exit");
gbc.gridy = 3;
exitButton.setBackground(Color.RED);
exitButton.setForeground(Color.WHITE);
loginPanel.add(exitButton, gbc);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (users.containsKey(username) && users.get(username).equals(password)) {
JOptionPane.showMessageDialog(null, "Login successful!", "Login", JOptionPane.INFORMATION_MESSAGE);
showReservationPanel();
} else {
JOptionPane.showMessageDialog(null, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE);
}
}
});
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
private void createReservationPanel() {
reservationPanel = new JPanel(new GridLayout(9, 2));
JLabel nameLabel = new JLabel("Name:");
nameField = new JTextField();
JLabel trainNumberLabel = new JLabel("Train Number:");
trainNumberField = new JTextField();
JLabel classTypeLabel = new JLabel("Class Type:");
classTypeField = new JTextField();
JLabel dateLabel = new JLabel("Date of Journey:");
dateField = new JTextField();
JLabel sourceLabel = new JLabel("Source:");
sourceField = new JTextField();
JLabel destinationLabel = new JLabel("Destination:");
destinationField = new JTextField();
JButton reserveButton = new JButton("Reserve");
JButton exitButton = new JButton("Exit");
reservationPanel.add(nameLabel);
reservationPanel.add(nameField);
reservationPanel.add(trainNumberLabel);
reservationPanel.add(trainNumberField);
reservationPanel.add(classTypeLabel);
reservationPanel.add(classTypeField);
reservationPanel.add(dateLabel);
reservationPanel.add(dateField);
reservationPanel.add(sourceLabel);
reservationPanel.add(sourceField);
reservationPanel.add(destinationLabel);
reservationPanel.add(destinationField);
reservationPanel.add(new JLabel());
reservationPanel.add(reserveButton);
reservationPanel.add(new JLabel());
reservationPanel.add(exitButton);
reserveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
int trainNumber = Integer.parseInt(trainNumberField.getText());
String classType = classTypeField.getText();
String date = dateField.getText();
String source = sourceField.getText();
String destination = destinationField.getText();
String pnr = generatePNR();
Reservation reservation = new Reservation(name, trainNumber, classType, date, source, destination);
reservations.put(pnr, reservation);
JOptionPane.showMessageDialog(null, "Reservation successful! Your PNR number is " + pnr, "Reservation", JOptionPane.INFORMATION_MESSAGE);
clearReservationFields();
}
});
reserveButton.setBackground(Color.YELLOW);
reserveButton.setForeground(Color.BLACK);
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
exitButton.setBackground(Color.RED);
exitButton.setForeground(Color.WHITE);
}
private void createCancellationPanel() {
cancellationPanel = new JPanel(new GridLayout(4, 2));
JLabel pnrLabel = new JLabel("PNR Number:");
pnrField = new JTextField();
JButton cancelButton = new JButton("Cancel Reservation");
JButton exitButton = new JButton("Exit");
cancellationPanel.add(pnrLabel);
cancellationPanel.add(pnrField);
cancellationPanel.add(new JLabel());
cancellationPanel.add(cancelButton);
cancellationPanel.add(new JLabel());
cancellationPanel.add(exitButton);
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String pnr = pnrField.getText();
if (reservations.containsKey(pnr)) {
Reservation reservation = reservations.get(pnr);
JOptionPane.showMessageDialog(null, "Reservation details:\n" + reservation, "Reservation Details", JOptionPane.INFORMATION_MESSAGE);
int choice = JOptionPane.showConfirmDialog(null, "Are you sure you want to cancel this reservation?", "Confirm Cancellation", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
reservations.remove(pnr);
JOptionPane.showMessageDialog(null, "Reservation successfully canceled!", "Cancellation", JOptionPane.INFORMATION_MESSAGE);
pnrField.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "Invalid PNR number. Please try again.", "Cancellation Error", JOptionPane.ERROR_MESSAGE);
}
}
});
cancelButton.setBackground(Color.ORANGE);
cancelButton.setForeground(Color.BLACK);
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
exitButton.setBackground(Color.RED);
exitButton.setForeground(Color.WHITE);
}
private void createMenuBar() {
menuBar = new JMenuBar();
JMenu reservationMenu = new JMenu("Reservation");
JMenuItem makeReservationItem = new JMenuItem("Make a Reservation");
reservationMenu.add(makeReservationItem);
makeReservationItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showReservationPanel();
}
});
makeReservationItem.setBackground(Color.GREEN);
makeReservationItem.setForeground(Color.WHITE);
JMenu cancellationMenu = new JMenu("Cancellation");
JMenuItem cancelReservationItem = new JMenuItem("Cancel a Reservation");
cancellationMenu.add(cancelReservationItem);
cancelReservationItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showCancellationPanel();
}
});
cancelReservationItem.setBackground(Color.RED);
cancelReservationItem.setForeground(Color.WHITE);
menuBar.add(reservationMenu);
menuBar.add(cancellationMenu);
}
private String generatePNR() {
return "PNR" + System.currentTimeMillis();
}
private void clearReservationFields() {
nameField.setText("");
trainNumberField.setText("");
classTypeField.setText("");
dateField.setText("");
sourceField.setText("");
destinationField.setText("");
}
private class Reservation {
private String name;
private int trainNumber;
private String classType;
private String date;
private String source;
private String destination;
public Reservation(String name, int trainNumber, String classType, String date, String source, String destination) {
this.name = name;
this.trainNumber = trainNumber;
this.classType = classType;
this.date = date;
this.source = source;
this.destination = destination;
}
@Override
public String toString() {
return "Name: " + name +
"\nTrain Number: " + trainNumber +
"\nClass Type: " + classType +
"\nDate: " + date +
"\nSource: " + source +
"\nDestination: " + destination;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new OnlineReservationSystem().setVisible(true);
}
});
}
}