Coders Packet

TRAIN RESERVATION SYSTEM USING JAVA PROGRAMMING

By BODA KAVYA SREE

In this tutorial, we are going to learn how to build a code for Train Reservation System using JAVA programming. Here, in this case, we can book an Upper Berth or a Lower Berth.

Process of implementing the Train Reservation System :

In the java program, an individual can book either an Upper Berth or Lower Birth.

Generally, every individual have a right to choose any of the available berth seats. So the below java program allows an individual to book their seats as per their requirement. They can book their seats until the seats are available. On successful booking of seats, an individual will receive a non-refundable boarding pass. If the seats are not available an individual will get a message saying unable to book an Upper/Lower berth. This is all about the brief description of the below java project.

As we know that java has different loops, we have used some of the loops to build this particular java project. 

import java.util.Scanner;
 import java.util.Date;

         public class Main 
         {
         private static int[] berths = new int[13];

        public static void main(String[] args) {
         System.out.println("           ********WELCOME TO THE SOUTH CENTRAL RAILWAY RESERVATION SYSTEM!!*********");
         System.out.println("                                  HAVE A SAFE JOURNEY!!");
         System.out.println();

         for (int i = 0; i < 12; i++) {
             berths[i] = 0;
             }


         Scanner s = new Scanner(System.in);
         int choice = 1;


         System.out.print("Please select your type of berth:\n1.Upper Berth\n2.Lower Berth\n0.Exit.\n");
         choice = s.nextInt();

        while (choice != 0) {
            int seatnumber = 0;


            if (choice == 1) {
                 seatnumber = bookUpper();


                 if (seatnumber == -1) {
                      seatnumber = bookLower();

                     if (seatnumber != -1) {


                         System.out.println("Sorry, there are no Upper Berths available. But we do have a Lower Berth.");
                         boardingPass(seatnumber);
                         }
                    }
                 else {

                     System.out.println("         Congratulations, we have a Upper Berth available!");
                     boardingPass(seatnumber);
                     }
                 }
             else if (choice == 2) {


                 seatnumber = bookLower();


                 if (seatnumber == -1) {
                     seatnumber = bookUpper();

                     if (seatnumber != -1) {
                         System.out.println("Sorry, there are no Lower Berths available. But we do have a Upper Berth.");
                         boardingPass(seatnumber);
                         }
                     }
                 else {

                     System.out.println("         Congratulations, we have a Lower berth available!");
                     boardingPass(seatnumber);
                     }
                 }
             else {

                 System.out.println("Your choice is invalid. Please provide valid choice!");
                 choice = 0;
                 }


             if (seatnumber == -1) {
                 System.out.println("Sorry, there are no Upper Berths or no Lower Berths available");
                         System.out.println();
                 }



            System.out.print("Please select your type of berth:\n1.Upper Berth\n2.Lower Berth\n0.Exit.\n");
             choice = s.nextInt();
             }


         }

         private static int bookUpper() {
         for (int i = 0; i < 6; i++) {
            if (berths[i] == 0) {
                 berths[i] = 1;
                 return i + 1;
                 }
             }
         return -1;
         }

         private static int bookLower() {
         for (int i = 6; i < 12; i++) {
            if (berths[i] == 0) {
                 berths[i] = 1;
                 return i + 1;
                 }
             }
         return -1;

         }


private static void boardingPass(int berthnumber) {
         Date timenow = new Date();
         System.out.println();
         System.out.println("Date: " + timenow.toString());
         System.out.println("Boarding pass for berth number: " + berthnumber);
         System.out.println("Your Booking Successful!!!");
         System.out.println("This ticket is non-refundable.");
         System.out.println("Please be polite,keep your place clean . Have a safe journey.");
         System.out.println("!!Have a great day!!!");
         System.out.println();
        }
 }

 

OUTPUT 1:

Booking of Upper Berth.

OUTPUT 2:

Booking for a Lower Berth.

OUTPUT 3:

If Upper Berths are not available.

OUTPUT 4:

If Lower Berths are not available.

Download project

Reviews Report

Submitted by BODA KAVYA SREE (Kavyasree1903)

Download packets of source code on Coders Packet