Coders Packet

Train Booking System in Java

By SAI PRAVEEN ALLLAMPALLY

In this packet, we are going to build a code for the Train booking system using Java, By using different loops.

Implementation Of Booking System:

In this train booking system, I have to build code using Java. In this, a traveler or a passenger can book a window seat, aisle seat in the general compartment, and AC tier like 2-tier, 3-tier, etc...!

Using different kinds of statements and loops, the system is allocated seat numbers and a time in a passenger ticket. A person can book multiple seats of their choice until the seats are available.

The following code gives the complete description:

import java.util.Scanner;
 import java.util.Date;
 public class train
 {
private static int[] seats = new int[14];
public static void main(String args[])
{
System.out.println("Welcome to the train reservation system!");
System.out.println("Happy journey!");
System.out.println();
for (int i = 0; i < 14; i++) {
seats[i] = 0;
}
Scanner s = new Scanner(System.in);
int choice = 1;
System.out.print("Please enter your choice\n1.window seat in general compartment and AC tier(2,3) \n2.Aisle seat in general compartment and AC tier(2,3)\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("You are lucky, 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("You are lucky, 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 available.");
System.out.println();
}
System.out.print("Please enter your choice\n1.window seat in general compartment and AC tier(2,3) \n2.Aisle seat in general compartment and AC tier(2,3)\n0.Exit.\n");
choice = s.nextInt();
}

}
private static int bookWindow() 
{
for (int i = 0; i < 4; i++) 
{
if (seats[i] == 0)
 {
seats[i] = 1;
return i + 1;
}
}
return -1;
}
private static int bookAisle()
 {
for (int i = 4; i < 14; i++)
 {
if (seats[i] == 0)
 {
seats[i] = 1;
return i + 1;
}
}
return -1;
}
public 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("This ticket is refundable with in a period of time ");
System.out.println("Be aware, keep clean! ");
System.out.println();
}
}

OUTPUT 1:

window seat in the general compartment and AC tiers.

OUTPUT 2:

Aisle seat in the general compartment and AC tiers.

OUTPUT 3:

exit from the code!

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by SAI PRAVEEN ALLLAMPALLY (9676539961SAIPRAVEEN)

Download packets of source code on Coders Packet