Coders Packet

Bus Seat Booking System Using Java

By MASNA VISHNUDEV

In this module, we are going to build a code for Bus seat booking system using java. In this, we can book a window seat or a non-window seat.

Implementation Of Booking System

In this, the passenger can have a choice to book a window seat or an aisle seat.

Actually, some people like the window seat, and some people like the aisle seat. So this is the Bus ticket reservation gives the choice to book their own type of seat. A person can book multiple seats of their choice until the seats are available.

To build this project different loops are used.

 

The following code gives the complete description:

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

         public class HYD {


         private static int[] seats = new int[12];

        public static void main(String args[]) {
         System.out.println("Welcome to the HYD Bus reservation system!");
         System.out.println("Have a fabulous HYD ride!");
         System.out.println();


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


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


         System.out.print("Please enter your choice\n1.window seat\n2.Aisle seat\n0.Exit.\n");
         choice = s.nextInt();

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


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


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

                     if (seatnumber != -1) {


                         System. out.println("Sorry, we were not able to book a window seat. But do have an aisle seat.");
                         printBoardingPass(seatnumber);
                         }
                    }
                 else {

                     System.out.println("Congratulations, we have a window seat available!");
                     printBoardingPass(seatnumber);
                     }
                 }
             else if (choice == 2) {


                 seatnumber = bookAisle();


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

                     if (seatnumber != -1) {
                         System.out.println("Sorry, we were not able to book an aisle seat. But do have a window seat.");
                         printBoardingPass(seatnumber);
                         }
                     }
                 else {

                     System.out.println("Congratulations, we have an aisle seat available!");
                     printBoardingPass(seatnumber);
                     }
                 }
             else {

                 System.out.println("Invalid choice made. Please try again!");
                 choice = 0;
                 }


             if (seatnumber == -1) {
                 System.out.println("We are sorry, there are no window or aisle seats");
                         System.out.println();
                 }



            System.out.print("Please enter your choice\n1.window seat\n2.Aisle seat\n0.Exit.\n");
             choice = s.nextInt();
             }


         }

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

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

         }


private static void printBoardingPass(int seatnumber) {
         Date timenow = new Date();
         System.out.println();
         System.out.println("Date: " + timenow.toString());
         System.out.println("Boarding pass for seat number: " + seatnumber);
         System.out.println("Your Booking Successful!");
         System.out.println("This ticket is non-refundable and non-transferable.");
         System.out.println("Please be curteous, do not smoke. Enjoy your trip.");
         System.out.println("Have a nice day");
         System.out.println();
        }
 }

 

Output1:

Booking of a Window seat.

 

 

Output 2:

Booking for an Aisle seat.

 

And this is all about the bus ticket reservation system using java.

Thank You.

 

Download project

Reviews Report

Submitted by MASNA VISHNUDEV (vishnudevmasna)

Download packets of source code on Coders Packet