bus booking system in java (GUI)

introduction

The Bus Booking System in Java is a user-friendly application that replicates real-world online ticket booking systems for buses.

 Users can select destinations, travel dates, preferred buses, and available seats. Upon selecting, the system calculates fare, generates a ticket with a unique ID, and simulates saving or printing the ticket. Admins can manage bus schedules, fare details, and seat capacity. 

The GUI interface enhances usability with dropdown menus, calendars for date selection, and real-time seat availability. 

This project is ideal for transport agencies, private operators, or academic use to demonstrate real-world application development involving file handling, GUI interaction, and object-oriented programming.

steps to create bus booking system

  1. Identify booking features — select route, date, seat.

  2. Design GUI with dropdowns for route, calendar picker for date.

  3. Create Bus and Ticket classes.

  4. Implement seat availability logic.

  5. Generate unique ticket IDs and save tickets.

  6. Store data in files or DB.

  7. Add admin panel to manage buses and routes.

  8. Test booking flow with seat selection.

  9. Add print/save ticket option.

  10. Finalize and document.

code explanation

1. Main Window Setup

setTitle("Advanced Bus Booking System");

setSize(700, 500);

  • Sets up the main window title and size.

  • Uses JTabbedPane to create 4 tabs:

    • Login

    • Book Ticket

    • View Tickets

    • Cancel Ticket

 2. Login Panel

 
if (user.equals("admin") && pass.equals("1234"))
  • Username: admin

  • Password: 1234

  • If correct:

    • Enables other tabs.

    • Switches to Book Ticket tab.

  • If wrong:

    • Shows an “Invalid credentials” message.

 3. Booking Panel

Input Fields:

  • nameField: Customer name.

  • busCombo: Dropdown with buses like BUS101, BUS202.

  • seatField: Number of seats to book.

  • dateField: Travel date (manually typed).

  • paymentCombo: Payment method (Cash, UPI, Card).

Book Ticket button:

Triggers handleBooking():

  • Validates all fields.

  • Checks seat availability (MAX_SEATS = 40).

  • Calculates fare (₹200 per seat).

  • Generates a random ticket ID.

  • Previews ticket in popup.

  • Saves ticket to a file:
    Example: ticket_TICK1234.txt

  • Updates internal seat count (seatMap).

  • Clears fields and refreshes ticket view.

 4. Ticket File Storage

Each ticket is saved as a separate .txt file:

ticket_TICK1234.txt

Stored data includes:

  • Ticket ID

  • Name

  • Bus No

  • Seats

  • Date

  • Payment

  • Fare

 5. View Tickets Panel

  • Shows a JTextArea with contents of all ticket_*.txt files.

  • Refreshes the view every time “Refresh” button is clicked.

 6. Cancel Ticket Panel

  • User enters a ticket ID.

  • Deletes the corresponding file (ticket_TICKxxxx.txt) if it exists.

  • Shows confirmation or error message.

  • Refreshes ticket list.

 7. Important Logic Points

Seat Tracking:

Map seatMap = new HashMap();

  • Keeps track of how many seats are booked for each bus.

  • Prevents overbooking.

Ticket ID:

 
"TICK" + new Random().nextInt(9999)
  • Random 4-digit ticket ID.

source code

				
					import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class AdvancedBusBookingSystem extends JFrame {
    private JTabbedPane tabs;
    private JTextField loginUserField, nameField, seatField, dateField;
    private JPasswordField loginPassField;
    private JComboBox busCombo, paymentCombo;
    private JTextArea viewArea;
    private String currentUser = "";
    private final int MAX_SEATS = 40;
    private final int FARE_PER_SEAT = 200;
    private Map seatMap = new HashMap();

    public AdvancedBusBookingSystem() {
        setTitle("Advanced Bus Booking System");
        setSize(700, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        tabs = new JTabbedPane();

        tabs.add("Login", loginPanel());
        tabs.add("Book Ticket", bookingPanel());
        tabs.add("View Tickets", viewPanel());
        tabs.add("Cancel Ticket", cancelPanel());

        add(tabs);
        tabs.setEnabledAt(1, false);
        tabs.setEnabledAt(2, false);
        tabs.setEnabledAt(3, false);

        setVisible(true);
    }

    private JPanel loginPanel() {
        JPanel panel = new JPanel(null);

        JLabel title = new JLabel("Login to Book", JLabel.CENTER);
        title.setBounds(200, 30, 300, 30);
        title.setFont(new Font("Arial", Font.BOLD, 20));
        panel.add(title);

        JLabel userLabel = new JLabel("Username:");
        userLabel.setBounds(200, 100, 100, 25);
        panel.add(userLabel);

        loginUserField = new JTextField();
        loginUserField.setBounds(300, 100, 150, 25);
        panel.add(loginUserField);

        JLabel passLabel = new JLabel("Password:");
        passLabel.setBounds(200, 140, 100, 25);
        panel.add(passLabel);

        loginPassField = new JPasswordField();
        loginPassField.setBounds(300, 140, 150, 25);
        panel.add(loginPassField);

        JButton loginBtn = new JButton("Login");
        loginBtn.setBounds(300, 180, 100, 30);
        loginBtn.addActionListener(e -> {
            String user = loginUserField.getText();
            String pass = new String(loginPassField.getPassword());
            if (user.equals("admin") && pass.equals("1234")) {
                currentUser = user;
                tabs.setEnabledAt(1, true);
                tabs.setEnabledAt(2, true);
                tabs.setEnabledAt(3, true);
                tabs.setSelectedIndex(1);
            } else {
                JOptionPane.showMessageDialog(this, "Invalid credentials!");
            }
        });
        panel.add(loginBtn);

        return panel;
    }

    private JPanel bookingPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(null);

        JLabel nameLabel = new JLabel("Name:");
        nameLabel.setBounds(100, 40, 100, 25);
        panel.add(nameLabel);

        nameField = new JTextField();
        nameField.setBounds(200, 40, 200, 25);
        panel.add(nameField);

        JLabel busLabel = new JLabel("Bus No:");
        busLabel.setBounds(100, 80, 100, 25);
        panel.add(busLabel);

        busCombo = new JComboBox(new String[]{"BUS101", "BUS202", "BUS303"});
        busCombo.setBounds(200, 80, 200, 25);
        panel.add(busCombo);

        JLabel seatLabel = new JLabel("Seats:");
        seatLabel.setBounds(100, 120, 100, 25);
        panel.add(seatLabel);

        seatField = new JTextField();
        seatField.setBounds(200, 120, 200, 25);
        panel.add(seatField);

        JLabel dateLabel = new JLabel("Travel Date (dd-mm-yyyy):");
        dateLabel.setBounds(100, 160, 200, 25);
        panel.add(dateLabel);

        dateField = new JTextField();
        dateField.setBounds(300, 160, 100, 25);
        panel.add(dateField);

        JLabel payLabel = new JLabel("Payment Method:");
        payLabel.setBounds(100, 200, 150, 25);
        panel.add(payLabel);

        paymentCombo = new JComboBox(new String[]{"Cash", "UPI", "Card"});
        paymentCombo.setBounds(250, 200, 150, 25);
        panel.add(paymentCombo);

        JButton bookBtn = new JButton("Book Ticket");
        bookBtn.setBounds(200, 250, 150, 30);
        bookBtn.addActionListener(e -> handleBooking());
        panel.add(bookBtn);

        return panel;
    }

    private JPanel viewPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        viewArea = new JTextArea();
        viewArea.setEditable(false);
        panel.add(new JScrollPane(viewArea), BorderLayout.CENTER);

        JButton refresh = new JButton("Refresh");
        refresh.addActionListener(e -> loadTickets());
        panel.add(refresh, BorderLayout.SOUTH);

        return panel;
    }

    private JPanel cancelPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(null);

        JLabel label = new JLabel("Enter Ticket ID to cancel:");
        label.setBounds(150, 100, 200, 25);
        panel.add(label);

        JTextField cancelField = new JTextField();
        cancelField.setBounds(150, 130, 200, 25);
        panel.add(cancelField);

        JButton cancelBtn = new JButton("Cancel Ticket");
        cancelBtn.setBounds(180, 170, 150, 30);
        cancelBtn.addActionListener(e -> {
            String id = cancelField.getText();
            File file = new File("ticket_" + id + ".txt");
            if (file.exists()) {
                file.delete();
                JOptionPane.showMessageDialog(this, "Ticket " + id + " cancelled.");
                loadTickets();
            } else {
                JOptionPane.showMessageDialog(this, "Ticket ID not found.");
            }
        });
        panel.add(cancelBtn);

        return panel;
    }

    private void handleBooking() {
        String name = nameField.getText();
        String bus = (String) busCombo.getSelectedItem();
        String seatStr = seatField.getText();
        String date = dateField.getText();
        String payment = (String) paymentCombo.getSelectedItem();

        if (name.isEmpty() || seatStr.isEmpty() || date.isEmpty()) {
            JOptionPane.showMessageDialog(this, "All fields are required!");
            return;
        }

        int seats;
        try {
            seats = Integer.parseInt(seatStr);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Invalid seat number.");
            return;
        }

        int booked = seatMap.getOrDefault(bus, 0);
        if (booked + seats > MAX_SEATS) {
            JOptionPane.showMessageDialog(this, "Not enough seats on " + bus);
            return;
        }

        int fare = seats * FARE_PER_SEAT;
        String ticketID = "TICK" + new Random().nextInt(9999);

        String ticket = "Ticket ID: " + ticketID +
                "\nName: " + name +
                "\nBus: " + bus +
                "\nSeats: " + seats +
                "\nDate: " + date +
                "\nPayment: " + payment +
                "\nFare: ₹" + fare;

        JOptionPane.showMessageDialog(this, ticket, "Ticket Preview", JOptionPane.INFORMATION_MESSAGE);

        try (BufferedWriter writer = new BufferedWriter(new FileWriter("ticket_" + ticketID + ".txt"))) {
            writer.write(ticket);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "Error saving ticket!");
        }

        seatMap.put(bus, booked + seats);
        clearFields();
        loadTickets();
    }

    private void clearFields() {
        nameField.setText("");
        seatField.setText("");
        dateField.setText("");
    }

    private void loadTickets() {
        viewArea.setText("");
        File dir = new File(".");
        File[] files = dir.listFiles((d, name) -> name.startsWith("ticket_") && name.endsWith(".txt"));

        if (files != null) {
            for (File f : files) {
                try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        viewArea.append(line + "\n");
                    }
                    viewArea.append("\n----------------------------\n");
                } catch (IOException e) {
                    viewArea.append("Error reading " + f.getName() + "\n");
                }
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(AdvancedBusBookingSystem::new);
    }
}
				
			

output

More java Pojects
Get Huge Discounts