-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
463 lines (397 loc) · 17.9 KB
/
Main.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import javax.swing.*;
import java.awt.*;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.List;
class Station {
String name;
List<Track> tracks = new ArrayList<>();
Station(String name) {
this.name = name;
}
}
class Track {
Station from;
Station to;
int distance;
Track(Station from, Station to, int distance) {
this.from = from;
this.to = to;
this.distance = distance;
}
}
class Train {
String id;
String name;
Map<Station, String[]> schedule = new LinkedHashMap<>();
int delay; // delay in minutes
Train(String id, String name) {
this.id = id;
this.name = name;
}
}
class Booking {
// OOP concept: here
Train train;
String from;
String to;
String date;
String passengerName;
String gender;
String seat;
String paymentInfo;
Booking(Train train, String from, String to, String date, String passengerName, String gender, String seat, String paymentInfo) {
this.train = train;
this.from = from;
this.to = to;
this.date = date;
this.passengerName = passengerName;
this.gender = gender;
this.seat = seat;
this.paymentInfo = paymentInfo;
}
}
public class Main {
private static Map<String, Station> stations = new HashMap<>();
private static Map<String, Train> trains = new HashMap<>();
private static List<Booking> bookings = new ArrayList<>();
private static Train selectedTrain;
private static String fromStation;
private static String toStation;
private static String travelDate;
public static void main(String[] args) {
setupData();
SwingUtilities.invokeLater(Main::createWelcomePage);
}
private static void setupData() {
Station dhaka = new Station("Dhaka");
Station chittagong = new Station("Chittagong");
Station sylhet = new Station("Sylhet");
Station rajshahi = new Station("Rajshahi");
Station khulna = new Station("Khulna");
Station barisal = new Station("Barisal");
Station rangpur = new Station("Rangpur");
Station coxsbazar = new Station("Cox's Bazar");
Station bogura = new Station("Bogura");
Station comilla = new Station("Comilla");
// Setting up the tracks between stations
// Hashmap: key is the station name, value is the station object
stations.put("Dhaka", dhaka);
stations.put("Chittagong", chittagong);
stations.put("Sylhet", sylhet);
stations.put("Rajshahi", rajshahi);
stations.put("Khulna", khulna);
stations.put("Barisal", barisal);
stations.put("Rangpur", rangpur);
stations.put("Cox's Bazar", coxsbazar);
stations.put("Bogura", bogura);
stations.put("Comilla", comilla);
// OOP concept: Here we are creating a track object and adding it to the tracks list of the 'from' station
// This way, we are creating a two-way track between two stations
// For example, there is a track between Dhaka and Chittagong, and also a track between Chittagong and Dhaka
dhaka.tracks.add(new Track(dhaka, chittagong, 245));
chittagong.tracks.add(new Track(chittagong, dhaka, 245));
dhaka.tracks.add(new Track(dhaka, sylhet, 286));
sylhet.tracks.add(new Track(sylhet, dhaka, 286));
dhaka.tracks.add(new Track(dhaka, rajshahi, 343));
rajshahi.tracks.add(new Track(rajshahi, dhaka, 343));
dhaka.tracks.add(new Track(dhaka, khulna, 335));
khulna.tracks.add(new Track(khulna, dhaka, 335));
dhaka.tracks.add(new Track(dhaka, barisal, 178));
barisal.tracks.add(new Track(barisal, dhaka, 178));
dhaka.tracks.add(new Track(dhaka, rangpur, 296));
rangpur.tracks.add(new Track(rangpur, dhaka, 296));
dhaka.tracks.add(new Track(dhaka, coxsbazar, 414));
coxsbazar.tracks.add(new Track(coxsbazar, dhaka, 414));
dhaka.tracks.add(new Track(dhaka, bogura, 247));
bogura.tracks.add(new Track(bogura, dhaka, 247));
dhaka.tracks.add(new Track(dhaka, comilla, 97));
comilla.tracks.add(new Track(comilla, dhaka, 97));
// OOP concept: Here we are creating train objects and adding the schedule to the train object
Train subornoExpress = new Train("111", "Suborno Express");
subornoExpress.schedule.put(dhaka, new String[]{"06:00", "06:15"});
subornoExpress.schedule.put(chittagong, new String[]{"10:30", "10:45"});
trains.put("111", subornoExpress);
Train mohanagarExpress = new Train("222", "Mohanagar Express");
mohanagarExpress.schedule.put(dhaka, new String[]{"07:00", "07:15"});
mohanagarExpress.schedule.put(sylhet, new String[]{"11:30", "11:45"});
trains.put("222", mohanagarExpress);
Train silkCityExpress = new Train("333", "Silk City Express");
silkCityExpress.schedule.put(dhaka, new String[]{"08:00", "08:15"});
silkCityExpress.schedule.put(rajshahi, new String[]{"12:30", "12:45"});
trains.put("333", silkCityExpress);
Train sundarbanExpress = new Train("444", "Sundarban Express");
sundarbanExpress.schedule.put(dhaka, new String[]{"09:00", "09:15"});
sundarbanExpress.schedule.put(khulna, new String[]{"13:30", "13:45"});
trains.put("444", sundarbanExpress);
Train rocketExpress = new Train("555", "Rocket Express");
rocketExpress.schedule.put(dhaka, new String[]{"10:00", "10:15"});
rocketExpress.schedule.put(barisal, new String[]{"14:30", "14:45"});
trains.put("555", rocketExpress);
Train nilSagarExpress = new Train("666", "Nil Sagar Express");
nilSagarExpress.schedule.put(dhaka, new String[]{"11:00", "11:15"});
nilSagarExpress.schedule.put(rangpur, new String[]{"15:30", "15:45"});
trains.put("666", nilSagarExpress);
Train beachExpress = new Train("777", "Beach Express");
beachExpress.schedule.put(dhaka, new String[]{"12:00", "12:15"});
beachExpress.schedule.put(coxsbazar, new String[]{"16:30", "16:45"});
trains.put("777", beachExpress);
}
// Page 1: Welcome Page
private static void createWelcomePage() {
JFrame welcomeFrame = new JFrame("Welcome Page");
welcomeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
welcomeFrame.setSize(470, 685);
welcomeFrame.setLayout(null);
JLabel welcomeMessage = new JLabel("Welcome To Railway Management System", SwingConstants.CENTER);
welcomeMessage.setBounds(35, 230, 400, 60);
welcomeMessage.setFont(new Font(welcomeMessage.getFont().getName(), Font.BOLD, 17));
JButton enterButton = new JButton("Enter");
enterButton.setBounds(175, 380, 80, 30);
enterButton.addActionListener(e -> {
welcomeFrame.dispose();
createLoginPage();
});
welcomeFrame.add(welcomeMessage);
welcomeFrame.add(enterButton);
welcomeFrame.setVisible(true);
}
// Page 2: Login Page
private static void createLoginPage() {
JFrame loginFrame = new JFrame("Login Page");
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame.setSize(470, 685);
loginFrame.setLayout(null);
JLabel headerLabel = new JLabel("Railway Management System", SwingConstants.CENTER);
headerLabel.setBounds(35, 130, 400, 60);
headerLabel.setFont(new Font(headerLabel.getFont().getName(), Font.PLAIN, 21));
JLabel userLabel = new JLabel("Username:");
userLabel.setBounds(120, 262, 80, 30);
JTextField userField = new JTextField();
userField.setBounds(200, 262, 200, 30);
JLabel passLabel = new JLabel("Password:");
passLabel.setBounds(120, 326, 80, 30);
JPasswordField passField = new JPasswordField();
passField.setBounds(200, 326, 200, 30);
JLabel messageLabel = new JLabel();
messageLabel.setBounds(150, 400, 500, 30);
JButton loginButton = new JButton("Login");
loginButton.setBounds(175, 380, 80, 30);
loginButton.addActionListener(e -> {
String username = userField.getText();
String password = new String(passField.getPassword());
if (isValidLogin(username, password)) {
loginFrame.dispose();
createSearchPage();
} else {
messageLabel.setText("Invalid login");
}
});
loginFrame.add(headerLabel);
loginFrame.add(userLabel);
loginFrame.add(userField);
loginFrame.add(passLabel);
loginFrame.add(passField);
loginFrame.add(messageLabel);
loginFrame.add(loginButton);
loginFrame.setVisible(true);
}
private static boolean isValidLogin(String username, String password) {
return "admin".equals(username) && "admin".equals(password);
}
// Page 3: Search Page
private static void createSearchPage() {
JFrame searchFrame = new JFrame("Search Trains");
searchFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
searchFrame.setSize(470, 685);
searchFrame.setLayout(null);
JLabel headerLabel = new JLabel("Search for Trains", SwingConstants.CENTER);
headerLabel.setBounds(35, 50, 400, 60);
headerLabel.setFont(new Font(headerLabel.getFont().getName(), Font.PLAIN, 21));
JLabel fromLabel = new JLabel("From:");
fromLabel.setBounds(60, 150, 100, 30);
JTextField fromField = new JTextField();
fromField.setBounds(140, 150, 200, 30);
JLabel toLabel = new JLabel("To:");
toLabel.setBounds(60, 200, 100, 30);
JTextField toField = new JTextField();
toField.setBounds(140, 200, 200, 30);
JLabel dateLabel = new JLabel("Date:");
dateLabel.setBounds(60, 250, 100, 30);
JTextField dateField = new JTextField();
dateField.setBounds(140, 250, 200, 30);
JButton searchButton = new JButton("Search");
searchButton.setBounds(175, 300, 100, 30);
searchButton.addActionListener(e -> {
fromStation = fromField.getText();
toStation = toField.getText();
travelDate = dateField.getText();
searchFrame.dispose();
createTrainListPage();
});
searchFrame.add(headerLabel);
searchFrame.add(fromLabel);
searchFrame.add(fromField);
searchFrame.add(toLabel);
searchFrame.add(toField);
searchFrame.add(dateLabel);
searchFrame.add(dateField);
searchFrame.add(searchButton);
searchFrame.setVisible(true);
}
// Page 4: Train List Page
private static void createTrainListPage() {
JFrame trainListFrame = new JFrame("Train List");
trainListFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
trainListFrame.setSize(470, 685);
trainListFrame.setLayout(null);
JLabel trainListLabel = new JLabel("Available Trains", SwingConstants.CENTER);
trainListLabel.setBounds(35, 20, 400, 60);
String[][] data = getTrainData(fromStation, toStation);
String[] column = {"ID", "Train Name", "Arrival", "Departure"};
JTable trainListTable = new JTable(data, column);
trainListTable.setBounds(30, 100, 400, 250);
JScrollPane sp = new JScrollPane(trainListTable);
sp.setBounds(30, 100, 400, 250);
JButton bookButton = new JButton("Book");
bookButton.setBounds(175, 380, 100, 30);
bookButton.addActionListener(e -> {
int selectedRow = trainListTable.getSelectedRow();
if (selectedRow >= 0) {
String selectedTrainId = (String) trainListTable.getValueAt(selectedRow, 0);
selectedTrain = trains.get(selectedTrainId);
trainListFrame.dispose();
createBookingPage();
}
});
JButton backButton = new JButton("Back");
backButton.setBounds(175, 430, 100, 30);
backButton.addActionListener(e -> {
trainListFrame.dispose();
createSearchPage();
});
trainListFrame.add(trainListLabel);
trainListFrame.add(sp);
trainListFrame.add(bookButton);
trainListFrame.add(backButton);
trainListFrame.setVisible(true);
}
private static String[][] getTrainData(String from, String to) {
List<String[]> trainData = new ArrayList<>();
for (Train train : trains.values()) {
// Check if both 'from' and 'to' stations are present in the keys of the schedule map
if (train.schedule.containsKey(stations.get(from)) && train.schedule.containsKey(stations.get(to))) {
String[] scheduleFrom = train.schedule.get(stations.get(from));
String[] scheduleTo = train.schedule.get(stations.get(to));
trainData.add(new String[]{train.id, train.name, scheduleFrom[0], scheduleTo[1]});
System.out.println(train.id + " " + train.name + " " + scheduleFrom[0] + " " + scheduleTo[1]);
}
}
return trainData.toArray(new String[0][0]);
}
// Page 5: Booking Page
private static void createBookingPage() {
JFrame bookingFrame = new JFrame("Booking Details");
bookingFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bookingFrame.setSize(470, 685);
bookingFrame.setLayout(null);
JLabel headerLabel = new JLabel("Passenger Details", SwingConstants.CENTER);
headerLabel.setBounds(35, 20, 400, 60);
headerLabel.setFont(new Font(headerLabel.getFont().getName(), Font.PLAIN, 21));
JLabel nameLabel = new JLabel("Name:");
nameLabel.setBounds(60, 100, 100, 30);
JTextField nameField = new JTextField();
nameField.setBounds(140, 100, 200, 30);
JLabel seatLabel = new JLabel("Seat:");
seatLabel.setBounds(60, 150, 100, 30);
JTextField seatField = new JTextField();
seatField.setBounds(140, 150, 200, 30);
JLabel paymentLabel = new JLabel("Payment Info:");
paymentLabel.setBounds(60, 200, 100, 30);
JTextField paymentField = new JTextField();
paymentField.setBounds(140, 200, 200, 30);
// Gender selection option using radio buttons
JRadioButton maleButton = new JRadioButton ("Male");
JRadioButton femaleButton = new JRadioButton("Female");
JLabel messageLabel = new JLabel("");
messageLabel.setBounds(150, 400, 500, 30);
maleButton.setBounds(60, 250, 100, 30);
femaleButton.setBounds(160, 250, 100, 30);
JButton confirmButton = new JButton("Confirm");
confirmButton.setBounds(175, 300, 100, 30);
confirmButton.addActionListener(e -> {
String passengerName = nameField.getText();
String seat = seatField.getText();
String paymentInfo = paymentField.getText();
String gender = "";
if (maleButton.isSelected()) {
gender = "Male";
} else if (femaleButton.isSelected()) {
gender = "Female";
}
Booking booking = new Booking(selectedTrain, fromStation, toStation, travelDate, passengerName, gender, seat, paymentInfo);
bookings.add(booking);
updateBookingFile();
bookingFrame.dispose();
createThankYouPage(booking);
});
maleButton.addActionListener(e -> {
if (maleButton.isSelected()) {
femaleButton.setSelected(false);
}
});
femaleButton.addActionListener(e -> {
if (femaleButton.isSelected()) {
maleButton.setSelected(false);
}
});
bookingFrame.add(headerLabel);
bookingFrame.add(nameLabel);
bookingFrame.add(nameField);
bookingFrame.add(seatLabel);
bookingFrame.add(seatField);
bookingFrame.add(paymentLabel);
bookingFrame.add(paymentField);
bookingFrame.add(confirmButton);
bookingFrame.add(maleButton);
bookingFrame.add(femaleButton);
bookingFrame.add(messageLabel);
bookingFrame.setVisible(true);
}
// Adding newly updated booking List in a text file
private static void updateBookingFile() {
try {
FileWriter writer = new FileWriter("bookings.txt", true);
for (Booking booking : bookings) {
writer.write(booking.train.id + "," + booking.from + "," + booking.to + "," + booking.date + "," + booking.passengerName + "," + booking.gender + "," + booking.seat + "," + booking.paymentInfo + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// Page 6: Thank You Page
private static void createThankYouPage(Booking booking) {
JFrame thankYouFrame = new JFrame("Thank You");
thankYouFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thankYouFrame.setSize(470, 685);
thankYouFrame.setLayout(null);
JLabel messageLabel = new JLabel("Thank you for booking!", SwingConstants.CENTER);
messageLabel.setBounds(35, 100, 400, 60);
messageLabel.setFont(new Font(messageLabel.getFont().getName(), Font.BOLD, 21));
JLabel bookingDetailsLabel = new JLabel("<html>Booking Details:<br>"
+ "Train: " + booking.train.name + "<br>"
+ "From: " + booking.from + "<br>"
+ "To: " + booking.to + "<br>"
+ "Date: " + booking.date + "<br>"
+ "Passenger: " + booking.passengerName + "<br>"
+ "Gender: " + booking.gender + "<br>"
+ "Seat: " + booking.seat + "<br>"
+ "Payment: " + booking.paymentInfo + "</html>", SwingConstants.CENTER);
bookingDetailsLabel.setBounds(35, 200, 400, 200);
thankYouFrame.add(messageLabel);
thankYouFrame.add(bookingDetailsLabel);
thankYouFrame.setVisible(true);
}
}