Airline Reservation System in Java

Code Blah
6 min readFeb 15, 2019

--

  • (Airline Reservations System) A small airline has just purchased a computer for its new automated reservations system. You’ve been asked to develop the new system. You’re to write an application to assign seats on each flight of the airline’s only plane (capacity: 10 seats).
  1. Your application should display the following alternatives: Please type 1 for First Class and Please type 2 for Economy.
  2. If the user types 1, your application should assign a seat in the first class section (seats 1–5).
  3. If the user types 2, your application should assign a seat in the economy section (seats 6–10).
  4. Your application should then display a boarding pass indicating the person’s seat number and whether it’s in the first-class or economy section of the plane.
  5. Use a one-dimensional array of primitive type boolean to represent the seating chart of the plane. Initialize all the elements of the array to false to indicate that all the seats are empty.
  6. As each seat is assigned, set the corresponding element of the array to true to indicate that the seat is no longer available.
  7. Your application should never assign a seat that has already been assigned. When the economy section is full, your application should ask the person if it’s acceptable to be placed in the first-class section (and vice versa). If yes, make the appropriate seat assignment. If no, display the message “Next flight leaves in 3 hours.”
*
* Filename: AirlineReservationSystem.java
*
* Description: Exercise 7.19 - Airline Reservation System
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
import java.util.Scanner;

public class AirlineReservationSystem{
boolean[] arrSeats = new boolean[10];
Scanner sc = new Scanner(System.in);

// SETTERS
// assigns first empty seat in relevant section
public boolean assignSeat(String section){
if(section == "first"){
if(getFreeSeats(section) > 0){
for(int i=0; i<5; i++){
if(arrSeats[i] == false){
arrSeats[i] = true;
printBoardingPass(i);
return true;
}
}
}
}else if(section == "economy"){
if(getFreeSeats(section) > 0){
for(int i=5; i<arrSeats.length; i++){
if(arrSeats[i] == false){
arrSeats[i] = true;
printBoardingPass(i);
return true;
}
}

}
}
// seats in chosen section full
// check if ok to assign to other section
System.out.printf("All seats in section \"%s\" are booked.\n", section);
System.out.printf("Would you like to be moved to section \"%s\" (y/n)? ",
(section == "first") ? "economy" : "first");

if(sc.next().charAt(0) == 'y')
assignSeat((section == "first") ? "economy" : "first");
else
System.out.println("\nNext flight leaves in 3 hours.\n");

return false;
}
// GETTERS
// returns number of free seats in each section
private int getFreeSeats(String section){
int total = 0;
if(section == "first"){
// first class 1-5 (array 0-4)
for(int i=0; i<5; i++){
if(arrSeats[i] == false)
total += 1;
}
}else if(section == "economy"){
// economy 6-10 (array 5-9)
for(int i=5; i<arrSeats.length; i++){
if(arrSeats[i] == false)
total += 1;
}
}
return total;
}
// see whether or not all seats are booked
public boolean seatsAvailable(){
// if empty seat found return true
for(boolean seat : arrSeats)
if(seat == false)
return true;

// if none found plane is full
return false;
}

public void printGreeting(){
System.out.println("\nWelcome to Crap Airlines booking system.\n");
}
// print the menu with remaining number of seats for each section
public void printMenu(){
System.out.printf("1. Economy class %s\n",
(getFreeSeats("economy") > 0) ?
"(" + Integer.toString(getFreeSeats("economy")) + ")" : "(full)");
System.out.printf("2. First class %s\n",
(getFreeSeats("first") > 0 ?
"(" + Integer.toString(getFreeSeats("first")) + ")" : "(full)"));
System.out.print("> ");
}
// prints the boarding pass
private void printBoardingPass(int seat){
System.out.println("\nBoarding pass for Crap Airlines.");
System.out.printf("\nSECTION: %s\nSEAT NUMBER: %d\n\n\n",
(seat < 5) ? "first" : "economy", seat + 1);
}
}

Method 2 : Airline Reservation System in Java

import TerminalIO.KeyboardReader;
public class CW12{
public static void main(String[] args){
KeyboardReader reader = new KeyboardReader();

int planeclass[] = new int[10];
boolean available = false;
boolean y=true;
int number, FirstClass = 1, Economy = 6 ;
String x;

while(FirstClass<=5&&Economy<=10)
{
System.out.print("Enter 1 for first class or 2 for Economy or -1 to exit :\t");
number = reader.readInt();

if(number==-1)
{
break;
}

if( number == 1 && FirstClass <= 5 )
{

System.out.println("You are assigned to first class\t"+"Seat#"+FirstClass+"\t");
FirstClass++;


}


else if( number == 2 && Economy <=10 )
{

// planeclass[ Economy++ ] = true;

System.out.println("You are assigned to Economy class\t" + Economy);)/>/>
Economy++;

}

else if( number == 1 && FirstClass >= 5 )
{
System.out.print("First class full"+" ");
if( Economy <= 10)
{
System.out.print("Is it acceptable to be placed in the Economy class (y / n)\t");
x = reader.readLine();
if (x.equals("y"))
{
// planeclass[Economy++ ] = true;

System.out.print("Seat reserved in Economy class\t" + Economy);)/>/>;

}
else
System.out.println("Next flight leaves in 3 hours.\t");
}
}


else if( number == 2 && Economy >= 10 )
{
System.out.println("Economy class full\t");
x=reader.readLine();
if (x.equals("y"))
{

// planeclass[ FirstClass++ ] = true;

System.out.println("You are assigned to first class\t"+FirstClass);)/>/>

}
else if(FirstClass==5&&Economy==10)
{
System.out.println("Plane is full, next flight is in 3 hours");
}
else
System.out.println("Next flight leaves in 3 hours.\t");

}
for(int i=1;i<planeclass.length;i++)
{
System.out.println(planeclass[FirstClass]+planeclass[Economy]);

}
}

}

Method 3 : Airline Reservation System in Java

import java.util.Scanner;
public class RevisedAirline
{
enum Seat{FIRSTCLASS, ECONOMY};
Seat seatClass;
boolean[] seating = new boolean[11]; //create 10 seats. Empty seat indicated by false
int upper;
int lower;

Scanner input = new Scanner(System.in);

public void makeReservation()
{
while (true) //Sorry, I'm still using this loop for now
{
bookClass();
System.out.println(seatClass == Seat.FIRSTCLASS ? "First Class " : "Economy");
System.out.printf("Seat# %d\n", bookSeat(lower, upper));
}
}

public void bookClass()
{
int section = 0;
System.out.print("Please type 1 for First Class or 2 for Economy: ");
section = input.nextInt();
if (section == 1)
{
seatClass = Seat.FIRSTCLASS;
lower = 0;
upper = 4;
}
else if (section == 2)
{
seatClass = Seat.ECONOMY;
lower = 5;
upper = 9;
}
}

public int bookSeat(int low, int upp) //assign a seat
{
boolean seatAssigned = false; // flag
int seatNumber = -1;
for (int i = low; seatAssigned == false && i <= upp; i++)
{
if (seating[i] == false) //if false, then a seat is available for assignment
{
seating[i] = true; //set to true to mark seat as unavailable next time
seatNumber = i + 1;
seatAssigned = true; //set flag to true to exit for loop
}
}
if (seatAssigned == false)
{
System.out.println("Class full");
}
return seatNumber;
}
}

Method 4 : Airline Reservation System in Java

package newairlinesreservation;
//Program for airline seat reservation

//import for taking input from user
import java.util.Scanner;
public class NewAirlinesReservation
{
//Array of seats
boolean[] flightSeats = new boolean[11];
Scanner input = new Scanner(System.in);

public void start()
{
while ( true )
{
bookSeat();
}
}


// Function for booking a seat in reservtion system
public void bookSeat()
{
//Asks user for his preffered class to travel
System.out.println("Choose Your preference : type '1' for First Class or '2' for Economy Class:");
int pessangerClass = input.nextInt();
if ( pessangerClass == 1 )
{
firstClassBooking();
}
else
{
economyClassBooking();
}
}

// Check and book for first class seating
public void firstClassBooking()
{
for ( int cnt = 1; cnt <= 5; cnt++ )
{
//check if seat is available to allocate to user booking.
if ( flightSeats[cnt] == false )
{
//book seat
flightSeats[cnt] = true;
System.out.printf("First Class Seat Booking: Seat# %d\n", cnt);
break;
}
else if ( flightSeats[5] == true )
{
if ( flightSeats[10] == true)
{
//if both classes are fully booked
System.out.println("Apologies!! All seats are booked. Next flight is scheduled in '3' hours.");
}
else
{
// provide pessanger another available class option
System.out.println("Sorry,First Class bookings are over. Would you like to opt for Economy class ? select '1' for Yes and '2' for No");
int selection = input.nextInt();
if ( selection == 1 )
{
economyClassBooking();
start();
}
else
{
System.out.println("Next flight is scheduled in '3' hours.");
System.exit(0);
}
}
}
}
}

// Check and book for economy class seating
public void economyClassBooking() // assign an economy seat
{
for ( int cnt = 6; cnt <= 10; cnt++ )
{
if ( flightSeats[cnt] == false )
{
flightSeats[cnt] = true;
System.out.printf("economy class seat booking :# %d\n", cnt);
break;
}
else if ( flightSeats[10] == true )
{
if ( flightSeats[5] == true)
{
System.out.println("Apologies!! All seats are booked. Next flight is scheduled in '3' hours.");
System.exit(0);
}
else
{
System.out.println("Sorry, Economy Class seat bookings are over. Would you like to opt for first Class seat? press '1' for Yes and '2' for No");
int selection = input.nextInt();
if ( selection == 1 )
{
firstClassBooking();
start();
}
else
{
System.out.println("Next flight is scheduled in 3 hours");
System.exit(0);
}
}
}
}
}
}

Originally published at codeblah.com on February 15, 2019.

--

--

Code Blah

Software Engineer and Architect, Teacher, Writer,Android Developer.|Coding | Programming | Blogger