dsaProject
user_0846129
plain_text
3 years ago
6.6 kB
12
Indexable
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class DSAProject implements ActionListener {
private ArrayList<String> seats = new ArrayList<>();
private JFrame frame;
private JPanel panel;
private JButton reserveButton, viewButton, cancelButton;
private JTextArea textArea;
public DSAProject() {
// create the GUI components
frame = new JFrame("Bus Reservation System");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(new GridLayout(2, 2));
reserveButton = new JButton("Reserve");
viewButton = new JButton("View available schedules");
cancelButton = new JButton("Cancel a reservation");
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
// add the components to the panel
panel.add(reserveButton);
panel.add(viewButton);
panel.add(cancelButton);
// add the panel and text area to the frame
frame.add(panel, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
// register event listeners for the buttons
reserveButton.addActionListener(this);
viewButton.addActionListener(this);
cancelButton.addActionListener(this);
// make the frame visible
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == reserveButton) {
reserveSeat();
} else if (e.getSource() == viewButton) {
viewSeats();
} else if (e.getSource() == cancelButton) {
cancelReservation();
}
}
private void reserveSeat() {
String from = JOptionPane.showInputDialog(frame, "Where are you coming from?");
String[] destinations = {"Manila", "Cebu", "Davao"};
String destination = (String) JOptionPane.showInputDialog(frame, "Select your destination:",
"Destination", JOptionPane.QUESTION_MESSAGE, null, destinations, destinations[0]);
// Display available schedules
String[] schedules = {"9:00 AM - 12:00 PM", "1:00 PM - 4:00 PM", "5:00 PM - 8:00 PM"};
String schedule = (String) JOptionPane.showInputDialog(frame, "Select your preferred schedule:",
"Schedule", JOptionPane.QUESTION_MESSAGE, null, schedules, schedules[0]);
// Display available seats for selected schedule
ArrayList<String> availableSeats = new ArrayList<>();
for (int i = 1; i <= 20; i++) {
String seatNumber = Integer.toString(i);
if (!seats.contains(schedule + ", Seat " + seatNumber)) {
availableSeats.add("Seat " + seatNumber);
}
}
if (availableSeats.isEmpty()) {
JOptionPane.showMessageDialog(frame, "Sorry, all seats for the selected schedule are taken.");
} else {
// Prompt user to select a seat
String[] seatOptions = availableSeats.toArray(new String[0]);
String seatNumber = (String) JOptionPane.showInputDialog(frame, "Select your preferred seat:",
"Seat", JOptionPane.QUESTION_MESSAGE, null, seatOptions, seatOptions[0]);
// Generate a random reservation ID
String reservationId = String.format("%04d", (int) (Math.random() * 10000));
// Reserve the selected seat for the selected schedule
String reservation = reservationId + ", " + schedule + ", " + seatNumber;
seats.add(reservation);
JOptionPane.showMessageDialog(frame, "Your reservation from " + from + " to " + destination +
" at " + schedule + " for " + seatNumber + " with reservation ID " + reservationId + " has been confirmed.");
// Remove the selected seat from the list of available seats
availableSeats.remove(seatNumber);
// Update the view button to show the updated list of available seats for the selected schedule
String availableSeatsText = "";
for (String s : availableSeats) {
availableSeatsText += s + "\n";
}
JOptionPane.showMessageDialog(frame, "Available seats for " + schedule + ":\n" + availableSeatsText);
}
}
private void viewSeats() {
textArea.setText("Available seats:\n");
String[] destinations = {"Manila", "Cebu", "Davao"};
String[] schedules = {"9:00 AM - 12:00 PM", "1:00 PM - 4:00 PM", "5:00 PM - 8:00 PM"};
// iterate over destinations and schedules to show available seats
for (String destination : destinations) {
textArea.append("\n" + destination + ":\n");
for (String schedule : schedules) {
ArrayList<String> availableSeats = new ArrayList<>();
// check available seats for current destination and schedule
for (int i = 1; i <= 20; i++) {
String seatNumber = Integer.toString(i);
String reservation = schedule + ", Seat " + seatNumber + " to " + destination;
if (!seats.contains(reservation)) {
availableSeats.add("Seat " + seatNumber);
}
}
// show available seats for current destination and schedule
textArea.append(schedule + ":\n");
if (availableSeats.isEmpty()) {
textArea.append(" All seats are taken.\n");
} else {
for (String seat : availableSeats) {
textArea.append(" " + seat + " ");
textArea.append("\n");
}
}
}
}
}
private void cancelReservation() {
String seatNumber = JOptionPane.showInputDialog(frame, "Enter the seat number you wish to cancel:");
if (seats.contains(seatNumber)) {
seats.remove(seatNumber);
JOptionPane.showMessageDialog(frame, "Seat " + seatNumber + " has been cancelled.");
} else {
JOptionPane.showMessageDialog(frame, "Seat " + seatNumber + " is not reserved.");
}
}
public static void main(String[] args) {
new DSAProject();
}
}
Editor is loading...