Railway Reservation System …Made Simply with Java

Vamsinaik
4 min readSep 14, 2023

--

Task 1.19🔰

Task Description 📄

Project Description

The Railway Reservation System is a command-line application that simplifies the process of booking and managing railway tickets. This project is built using Object-Oriented Programming (OOP) principles in Java and offers a range of functionalities to make railway ticket management efficient and user-friendly.

Functionalities

Train Information

This module provides information about the available trains, including their names, departure times, passenger strength, and unique train numbers. Currently, the system offers three trains:

Mumbai — Delhi Superfast (Train Number: 1010)

  • Departure Time: 13:05
  • Passenger Strength: 50

Delhi — Jaipur SuperFast (Train Number: 2013)

  • Departure Time: 7:00
  • Passenger Strength: 50

Prayagraj — Delhi Express (Train Number: 3045)

  • Departure Time: 10:00
  • Passenger Strength: 50

Seat Availability

This module allows users to check the availability of seats on a selected train. Users can input the train number, and the system will display the number of available seats on that train.

Booking

The booking module enables users to reserve seats on their chosen train. Users must provide their name and the train number they wish to book. If seats are available, the system confirms the booking; otherwise, it informs the user that the train is fully booked.

Canceling

Users have the option to cancel their booked tickets. To cancel a ticket, users must provide their name. The system then processes the cancellation and provides a confirmation message.

Ticket Display

This module displays details of booked tickets, including passenger names, seat numbers, train numbers, and fares. It provides users with a comprehensive overview of their reservations.

Error Handling

The project includes robust error handling mechanisms to ensure smooth user interactions. It manages potential issues like invalid input or attempting to book a seat that is already reserved, offering clear error messages and guidance to users.

How to Use the Railway Reservation System

  1. Display Available Trains: Select option 1 from the menu to see a list of available trains with their details.
  2. Check Seat Availability: Choose option 2 and enter the train number to check the availability of seats on that train.
  3. Book a Ticket: Opt for option 3, enter the train number, and provide your name to book a ticket. The system will confirm if the booking is successful.
  4. Cancel a Ticket: To cancel a booked ticket, select option 4 and enter your name. The system will confirm the cancellation if the booking exists; otherwise, it will inform you if no booking exists for that name.
  5. Display Booked Tickets: Option 5 allows you to view details of the booked tickets, including passenger names, seat numbers, train numbers, and fares.
  6. Exit: Select option 6 to exit the Railway Reservation System.

CODE AT A GLANCE

// Online Java Railway system

import java.util.ArrayList;
import java.util.Scanner;

class Train {
String name;
String time;
int passengerStrength;
int trainNumber;

public Train(String name, String time, int passengerStrength, int trainNumber) {
this.name = name;
this.time = time;
this.passengerStrength = passengerStrength;
this.trainNumber = trainNumber;
}
}

class ReservationSystem {
private ArrayList<Train> availableTrains = new ArrayList<>();
private ArrayList<String> bookedSeats = new ArrayList<>();

public ReservationSystem() {
availableTrains.add(new Train("Mumbai - Delhi", "13:05", 50, 1010));
availableTrains.add(new Train("Delhi - Jaipur", "7:00", 50, 2013));
availableTrains.add(new Train("Prayagraj - Delhi", "10:00", 50, 3045));
}

public void displayAvailableTrains() {
System.out.println("Available Trains:");
System.out.println("Train Name\tTime\tPassenger Strength\tTrain Number");
for (Train train : availableTrains) {
System.out.println(train.name + "\t" + train.time + "\t" + train.passengerStrength + "\t" + train.trainNumber);
}
}

public void checkSeatAvailability(int trainNumber) {
for (Train train : availableTrains) {
if (train.trainNumber == trainNumber) {
int availableSeats = train.passengerStrength - bookedSeats.size();
System.out.println("Available seats on Train " + train.trainNumber + ": " + availableSeats);
return;
}
}
System.out.println("Train not found.");
}

public void bookTicket(int trainNumber, String passengerName) {
for (Train train : availableTrains) {
if (train.trainNumber == trainNumber) {
if (bookedSeats.size() < train.passengerStrength) {
bookedSeats.add(passengerName);
System.out.println("Ticket booked successfully for " + passengerName);
} else {
System.out.println("Sorry, the train is fully booked.");
}
return;
}
}
System.out.println("Train not found.");
}

public void cancelTicket(String passengerName) {
if (bookedSeats.remove(passengerName)) {
System.out.println("Ticket canceled successfully for " + passengerName);
} else {
System.out.println("Passenger not found or no booking exists for this passenger.");
}
}

public void displayBookedTickets() {
System.out.println("Booked Tickets:");
for (String passenger : bookedSeats) {
System.out.println(passenger);
}
}
}
class Main {
public static void main(String[] args) {
ReservationSystem reservationSystem = new ReservationSystem();
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("\nRailway Reservation System Menu:");
System.out.println("1. Display Available Trains");
System.out.println("2. Check Seat Availability");
System.out.println("3. Book a Ticket");
System.out.println("4. Cancel a Ticket");
System.out.println("5. Display Booked Tickets");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");

int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character

switch (choice) {
case 1:
reservationSystem.displayAvailableTrains();
break;
case 2:
System.out.print("Enter Train Number: ");
int trainNumber = scanner.nextInt();
reservationSystem.checkSeatAvailability(trainNumber);
break;
case 3:
System.out.print("Enter Train Number: ");
trainNumber = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
System.out.print("Enter Passenger Name: ");
String passengerName = scanner.nextLine();
reservationSystem.bookTicket(trainNumber, passengerName);
break;
case 4:
System.out.print("Enter Passenger Name to Cancel: ");
passengerName = scanner.nextLine();
reservationSystem.cancelTicket(passengerName);
break;
case 5:
reservationSystem.displayBookedTickets();
break;
case 6:
System.out.println("Exiting Railway Reservation System. Thank you!");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}

Conclusion

The Railway Reservation System project offers a simplified and organized approach to booking and managing railway tickets. By leveraging Object-Oriented Programming principles, it ensures modularity and maintainability. This terminal-based application is user-friendly and comes with error handling to enhance the overall user experience. Whether you’re a passenger looking to book a ticket or an administrator managing reservations, this system streamlines the process, making it a valuable tool for both travelers and railway personnel.

Github repo link: https://github.com/vamsinaik452/java_railway_reservation_system.git

Thank you for reading this article…..😊😍

Please Comment down your thoughts, feedback, or suggestions.

--

--