-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvancedBusBookingSystem.java
More file actions
300 lines (248 loc) · 9.54 KB
/
AdvancedBusBookingSystem.java
File metadata and controls
300 lines (248 loc) · 9.54 KB
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
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
/*
Simple bus booking program
Written in a student-like style — small comments and simple names.
Save as AdvancedBusBookingSystem.java
*/
public class AdvancedBusBookingSystem extends JFrame {
private JTabbedPane tabs;
private JTextField userFld, nameFld, seatsFld, dateFld;
private JPasswordField passFld;
private JComboBox<String> busBox, payBox;
private JTextArea viewArea;
private String loggedIn = "";
private final int MAX_SEATS = 40;
private final int FARE = 200;
private HashMap<String, Integer> seatMap = new HashMap<String, Integer>();
public AdvancedBusBookingSystem() {
setTitle("Advanced Bus Booking System");
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
tabs = new JTabbedPane();
tabs.add("Login", makeLoginPanel());
tabs.add("Book Ticket", makeBookingPanel());
tabs.add("View Tickets", makeViewPanel());
tabs.add("Cancel Ticket", makeCancelPanel());
// disable tabs except login
tabs.setEnabledAt(1, false);
tabs.setEnabledAt(2, false);
tabs.setEnabledAt(3, false);
add(tabs);
setVisible(true);
}
private JPanel makeLoginPanel() {
JPanel p = new JPanel(null);
JLabel t = new JLabel("Login to Book", JLabel.CENTER);
t.setBounds(200, 30, 300, 30);
t.setFont(new Font("Arial", Font.BOLD, 20));
p.add(t);
JLabel u = new JLabel("Username:");
u.setBounds(200, 100, 100, 25);
p.add(u);
userFld = new JTextField();
userFld.setBounds(300, 100, 150, 25);
p.add(userFld);
JLabel pw = new JLabel("Password:");
pw.setBounds(200, 140, 100, 25);
p.add(pw);
passFld = new JPasswordField();
passFld.setBounds(300, 140, 150, 25);
p.add(passFld);
JButton loginBtn = new JButton("Login");
loginBtn.setBounds(300, 180, 100, 30);
loginBtn.addActionListener(e -> {
String usr = userFld.getText();
String pwtext = new String(passFld.getPassword());
// simple check - student project
if ("admin".equals(usr) && "1234".equals(pwtext)) {
loggedIn = usr;
tabs.setEnabledAt(1, true);
tabs.setEnabledAt(2, true);
tabs.setEnabledAt(3, true);
tabs.setSelectedIndex(1);
} else {
JOptionPane.showMessageDialog(this, "Invalid credentials!");
}
});
p.add(loginBtn);
return p;
}
private JPanel makeBookingPanel() {
JPanel p = new JPanel();
p.setLayout(null);
JLabel lblName = new JLabel("Name:");
lblName.setBounds(100, 40, 100, 25);
p.add(lblName);
nameFld = new JTextField();
nameFld.setBounds(200, 40, 200, 25);
p.add(nameFld);
JLabel lblBus = new JLabel("Bus No:");
lblBus.setBounds(100, 80, 100, 25);
p.add(lblBus);
busBox = new JComboBox<String>(new String[] {"BUS101", "BUS202", "BUS303"});
busBox.setBounds(200, 80, 200, 25);
p.add(busBox);
JLabel lblSeats = new JLabel("Seats:");
lblSeats.setBounds(100, 120, 100, 25);
p.add(lblSeats);
seatsFld = new JTextField();
seatsFld.setBounds(200, 120, 200, 25);
p.add(seatsFld);
JLabel lblDate = new JLabel("Travel Date (dd-mm-yyyy):");
lblDate.setBounds(100, 160, 200, 25);
p.add(lblDate);
dateFld = new JTextField();
dateFld.setBounds(300, 160, 100, 25);
p.add(dateFld);
JLabel lblPay = new JLabel("Payment Method:");
lblPay.setBounds(100, 200, 150, 25);
p.add(lblPay);
payBox = new JComboBox<String>(new String[] {"Cash", "UPI", "Card"});
payBox.setBounds(250, 200, 150, 25);
p.add(payBox);
JButton bookBtn = new JButton("Book Ticket");
bookBtn.setBounds(200, 250, 150, 30);
bookBtn.addActionListener(e -> doBooking());
p.add(bookBtn);
return p;
}
private JPanel makeViewPanel() {
JPanel p = new JPanel(new BorderLayout());
viewArea = new JTextArea();
viewArea.setEditable(false);
p.add(new JScrollPane(viewArea), BorderLayout.CENTER);
JButton r = new JButton("Refresh");
r.addActionListener(e -> loadTickets());
p.add(r, BorderLayout.SOUTH);
return p;
}
private JPanel makeCancelPanel() {
JPanel p = new JPanel();
p.setLayout(null);
JLabel lab = new JLabel("Enter Ticket ID to cancel:");
lab.setBounds(150, 100, 200, 25);
p.add(lab);
JTextField cancelField = new JTextField();
cancelField.setBounds(150, 130, 200, 25);
p.add(cancelField);
JButton cancelBtn = new JButton("Cancel Ticket");
cancelBtn.setBounds(180, 170, 150, 30);
cancelBtn.addActionListener(e -> {
String id = cancelField.getText().trim();
if (id.length() == 0) {
JOptionPane.showMessageDialog(this, "Please enter an ID");
return;
}
File f = new File("ticket_" + id + ".txt");
if (f.exists()) {
boolean ok = f.delete();
if (ok) {
JOptionPane.showMessageDialog(this, "Ticket " + id + " cancelled.");
} else {
JOptionPane.showMessageDialog(this, "Could not delete file.");
}
loadTickets();
} else {
JOptionPane.showMessageDialog(this, "Ticket ID not found.");
}
});
p.add(cancelBtn);
return p;
}
private void doBooking() {
String name = nameFld.getText().trim();
String bus = (String) busBox.getSelectedItem();
String seatsText = seatsFld.getText().trim();
String date = dateFld.getText().trim();
String pay = (String) payBox.getSelectedItem();
if (name.length() == 0 || seatsText.length() == 0 || date.length() == 0) {
JOptionPane.showMessageDialog(this, "All fields are required!");
return;
}
int seats = 0;
try {
seats = Integer.parseInt(seatsText);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid seat number.");
return;
}
Integer already = seatMap.get(bus);
if (already == null) already = 0;
if (already + seats > MAX_SEATS) {
JOptionPane.showMessageDialog(this, "Not enough seats on " + bus);
return;
}
int fare = seats * FARE;
String ticketId = "TICK" + (new Random().nextInt(9000) + 1000); // like 1000-9999
String ticket =
"Ticket ID: " + ticketId + "\n" +
"Name: " + name + "\n" +
"Bus: " + bus + "\n" +
"Seats: " + seats + "\n" +
"Date: " + date + "\n" +
"Payment: " + pay + "\n" +
"Fare: ₹" + fare + "\n";
// show preview
JOptionPane.showMessageDialog(this, ticket, "Ticket Preview", JOptionPane.INFORMATION_MESSAGE);
// save to file
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter("ticket_" + ticketId + ".txt"));
out.write(ticket);
out.close();
} catch (IOException ex) {
try { if (out != null) out.close(); } catch (Exception ignored) {}
JOptionPane.showMessageDialog(this, "Error saving ticket!");
return;
}
seatMap.put(bus, already + seats);
clearInputs();
loadTickets();
}
private void clearInputs() {
nameFld.setText("");
seatsFld.setText("");
dateFld.setText("");
}
private void loadTickets() {
viewArea.setText("");
File dir = new File(".");
// beginner-style filename filter using anonymous class
File[] files = dir.listFiles(new FilenameFilter() {
public boolean accept(File d, String name) {
return name.startsWith("ticket_") && name.endsWith(".txt");
}
});
if (files == null) {
viewArea.append("No tickets found.\n");
return;
}
for (File f : files) {
BufferedReader r = null;
try {
r = new BufferedReader(new FileReader(f));
String line;
while ((line = r.readLine()) != null) {
viewArea.append(line + "\n");
}
viewArea.append("\n----------------------------\n");
r.close();
} catch (IOException ex) {
try { if (r != null) r.close(); } catch (Exception ignored) {}
viewArea.append("Error reading " + f.getName() + "\n");
}
}
}
public static void main(String[] args) {
// simple start
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new AdvancedBusBookingSystem();
}
});
}
}