-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBusManagementSystem.java
More file actions
147 lines (130 loc) · 6.01 KB
/
Copy pathBusManagementSystem.java
File metadata and controls
147 lines (130 loc) · 6.01 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
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 BusManagementSystem extends JFrame implements ActionListener {
// Fields for ticket booking and management
private JTextField nameField, seatField, feedbackField;
private JTextArea outputArea, trackingArea;
private JButton bookButton, cancelButton, feedbackButton, paymentButton;
private JLabel statusLabel;
private Map<String, String> bookedTickets = new HashMap<>(); // Seat, Passenger
private double ticketPrice = 10.0;
private boolean isPaymentProcessed = false;
public BusManagementSystem() {
// Set up the frame
setTitle("Bus Management System");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 1));
// Ticket Booking Panel
JPanel bookingPanel = new JPanel(new GridLayout(6, 2));
bookingPanel.setBorder(BorderFactory.createTitledBorder("Ticket Booking"));
JLabel nameLabel = new JLabel("Passenger Name:");
nameField = new JTextField();
JLabel seatLabel = new JLabel("Seat Number (1-30):");
seatField = new JTextField();
bookButton = new JButton("Book Ticket");
cancelButton = new JButton("Cancel Ticket");
bookingPanel.add(nameLabel);
bookingPanel.add(nameField);
bookingPanel.add(seatLabel);
bookingPanel.add(seatField);
bookingPanel.add(bookButton);
bookingPanel.add(cancelButton);
// Payment Panel
JPanel paymentPanel = new JPanel(new GridLayout(2, 2));
paymentPanel.setBorder(BorderFactory.createTitledBorder("Payment Processing"));
JLabel paymentLabel = new JLabel("Ticket Price: $" + ticketPrice);
paymentButton = new JButton("Process Payment");
statusLabel = new JLabel("Payment Status: Pending");
paymentPanel.add(paymentLabel);
paymentPanel.add(paymentButton);
paymentPanel.add(new JLabel(""));
paymentPanel.add(statusLabel);
// Feedback Panel
JPanel feedbackPanel = new JPanel(new GridLayout(2, 2));
feedbackPanel.setBorder(BorderFactory.createTitledBorder("Feedback"));
JLabel feedbackLabel = new JLabel("Enter Feedback:");
feedbackField = new JTextField();
feedbackButton = new JButton("Submit Feedback");
feedbackPanel.add(feedbackLabel);
feedbackPanel.add(feedbackField);
feedbackPanel.add(new JLabel(""));
feedbackPanel.add(feedbackButton);
// Tracking Panel
JPanel trackingPanel = new JPanel(new BorderLayout());
trackingPanel.setBorder(BorderFactory.createTitledBorder("Navigation and Tracking"));
trackingArea = new JTextArea("Bus tracking information will appear here...");
trackingArea.setEditable(false);
trackingPanel.add(new JScrollPane(trackingArea));
// Output Area
outputArea = new JTextArea(10, 50);
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
// Add panels to frame
add(bookingPanel);
add(paymentPanel);
add(feedbackPanel);
add(trackingPanel);
add(scrollPane);
// Add action listeners
bookButton.addActionListener(this);
cancelButton.addActionListener(this);
feedbackButton.addActionListener(this);
paymentButton.addActionListener(this);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String seat = seatField.getText();
// Ensure seat is a valid number between 1 and 30
try {
int seatNumber = Integer.parseInt(seat);
if (seatNumber < 1 || seatNumber > 30) {
JOptionPane.showMessageDialog(this, "Seat number must be between 1 and 30.");
return;
}
if (e.getSource() == bookButton) {
if (!name.isEmpty() && !seat.isEmpty() && !bookedTickets.containsKey(seat)) {
if (isPaymentProcessed) {
bookedTickets.put(seat, name);
outputArea.append("Ticket Booked: " + name + " - Seat " + seat + "\n");
trackingArea.setText("Bus is departing from location A to B.\nEstimated time: 1 hour.\nCurrent Location: A.");
} else {
JOptionPane.showMessageDialog(this, "Please process the payment first.");
}
} else {
JOptionPane.showMessageDialog(this, "Invalid booking details or seat already booked.");
}
} else if (e.getSource() == cancelButton) {
if (!seat.isEmpty() && bookedTickets.containsKey(seat)) {
bookedTickets.remove(seat);
outputArea.append("Ticket Cancelled: Seat " + seat + "\n");
} else {
JOptionPane.showMessageDialog(this, "Seat is not booked.");
}
} else if (e.getSource() == feedbackButton) {
String feedback = feedbackField.getText();
if (!feedback.isEmpty()) {
outputArea.append("Feedback Received: " + feedback + "\n");
feedbackField.setText("");
} else {
JOptionPane.showMessageDialog(this, "Please enter feedback.");
}
} else if (e.getSource() == paymentButton) {
isPaymentProcessed = true;
statusLabel.setText("Payment Status: Processed");
outputArea.append("Payment of $" + ticketPrice + " processed.\n");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter a valid seat number.");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(BusManagementSystem::new);
}
}