A new airline is starting service with a single flight You w
A new airline is starting service with a single flight. You will need to design and build an application which will make reservations for its customers on that flight for a single day. Here are the high level requirements of the system: Allow customers to book one or more seats on the flight Allow a seating assignment for each booking in first-class or economy (by section, not by seat) Generate a random six-letter confirmation code Record the customer’s name, section, and confirmation code for every reservation Print a boarding pass for each customer Allow multiple customers to book seats Suggest alternative seats whenever one section (First Class or Economy) is full, and deny reservation whenever the plane is full Allow for straightforward expansion of the system as the airline’s capacity increases. For the initial airplane, there will be 2 first-class seats and 4 economy seats, but this capacity can increase when a new airplane is replaced with a new plane Design and implement a Java application to satisfy the specified high level requirements. The system must be able to - Allow users to book reservations Require input of the customer’s name Require input of the preferred section, first-class or economy Generate a random six-letter confirmation code Record each booking in a Passenger class having several fields One field will record customer name One field will record the section One field will record the confirmation number getter and / or setter methods for each field, and other methods as needed Eclipse allows you to automatically create these methods if you right-click on the yellow-underlined variable definition Record all the bookings in an array of Passengers held in a Manifest class Book a different class seat when a section is full (First Class to Economy and vice versa) Ask the user if they want to reserve a seat in the other section. If yes, book that reservation. If not, exit reservation without booking a seat Print a boarding pass indicating the person’s name, seating section, and confirmation code Continue making reservations unless both sections are full or the user exits the system. Display (print out) all passengers on the flight (in the Manifest) before exiting program
Solution
PAssenger.java
import java.util.Random;
// Passenger class
public class Passenger {
// Class has three fields
private Section section;
private String name;
private String code;
// Getters and setters for three sections
public Section getSection() {
return section;
}
public void setSection(Section section) {
this.section = section;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
// Here we generate 6 digit code
public void generateCode() {
Random rm=new Random();
// Using strig buffer and random classes to generate random numbers
StringBuffer sb=new StringBuffer();
// Taken letters and digits used for the code
String codeLetters=\"123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\";
// Loop to generate 6 letters each time
for(int i=0;i<6;i++)
{
// appending to string buffer
sb.append(codeLetters.charAt(rm.nextInt(codeLetters.length())));
}
// setting code here
this.code = sb.toString();
}
// to string to represent object in string format
public String toString()
{
if(section instanceof Economy)
return \"passenger name: \"+name+\" section: Economy Code: \"+code;
else
return \"passenger name: \"+name+\" section: First Class Code: \"+code;
}
}
Section.java
// Abstract class for the section
public abstract class Section {
public Passenger p;
public abstract boolean checkAvailability();
}
Economy.java
// The economy class that extends the section properties
public class Economy extends Section{
// Number of seats as given in question
public static final int TOTAL_SEATS=4;
// Varible to count the number of seats booked
public static int bookedCount=0;
public boolean checkAvailability()
{
if(bookedCount>=TOTAL_SEATS)
return false;
else
return true;
}
// incrementing count while allocating new seat
public Economy()
{
bookedCount++;
}
}
FirstClass.java
// Same logic applies here
public class FirstClass extends Section{
public static final int TOTAL_SEATS=2;
public static int bookedCount=0;
public boolean checkAvailability()
{
if(bookedCount>=TOTAL_SEATS)
return false;
else
return true;
}
public FirstClass()
{
bookedCount++;
}
}
Manifest.java
// Manifest has main method to run the program
import java.util.Scanner;
public class Manifest {
public static void main(String[] args)
{
// Passenger array stores the passengers
Passenger[] passengers=new Passenger[6];
Scanner sc=new Scanner(System.in);
int option;
String name;
System.out.println(\"Welcome to filgh booking App\");
System.out.println(\"---------MENU---------\");
boolean loop=true;
int index=0;
// Loop until finishing / full filling seats
while(loop)
{
// propting user for class
System.out.println(\"Enter 1 To book a Ticket in Economy section\");
System.out.println(\"Enter 2 to book a ticket in FirstClass section\");
option=sc.nextInt();
if(option==1)
{
if(Economy.bookedCount<4)
{
// for economy class
System.out.println(\"Enter passenger name:\");
name=sc.next();
Economy e=new Economy();
Passenger p=new Passenger();
p.setName(name);
p.setSection(e);
p.generateCode();
passengers[index]=p;
index++;
}
else if(FirstClass.bookedCount<2)
{
// for first class if economy is full
System.out.println(\"Economy is full Do you want to book in first class\");
System.out.println(\"Enter 1 if you want to book 2 to no\");
int conv=sc.nextInt();
if(conv==1)
{
System.out.println(\"Enter passenger name:\");
name=sc.next();
FirstClass e=new FirstClass();
Passenger p=new Passenger();
p.setName(name);
p.setSection(e);
p.generateCode();
passengers[index]=p;
index++;
}
else
System.out.println(\"Thank you for visiting\");
}
else
{
System.out.println(\"No seats in two sections\");
loop=false;
}
}
// Same logic as above
else if(option==2)
{
if(FirstClass.bookedCount<2)
{
System.out.println(\"Enter passenger name:\");
name=sc.next();
FirstClass e=new FirstClass();
Passenger p=new Passenger();
p.setName(name);
p.setSection(e);
p.generateCode();
passengers[index]=p;
index++;
}
else if(Economy.bookedCount<4)
{
System.out.println(\"FirstClass is full Do you want to book in Economy\");
System.out.println(\"Enter 1 if you want to book 2 to no\");
int conv=sc.nextInt();
if(conv==1)
{
System.out.println(\"Enter passenger name:\");
name=sc.next();
Economy e=new Economy();
Passenger p=new Passenger();
p.setName(name);
p.setSection(e);
p.generateCode();
passengers[index]=p;
index++;
}
else
System.out.println(\"Thank you for visiting\");
}
else
{
System.out.println(\"No seats in two sections\");
loop=false;
}
}
}
System.out.println(\"The list of passengers who booked tickets\");
for(int i=0;i<passengers.length;i++)
{
System.out.println(passengers[i]);
}
}
}
Output:
Welcome to filgh booking App
---------MENU---------
Enter 1 To book a Ticket in Economy section
Enter 2 to book a ticket in FirstClass section
1
Enter passenger name:
shiva
Enter 1 To book a Ticket in Economy section
Enter 2 to book a ticket in FirstClass section
1
Enter passenger name:
raju
Enter 1 To book a Ticket in Economy section
Enter 2 to book a ticket in FirstClass section
1
Enter passenger name:
kumar
Enter 1 To book a Ticket in Economy section
Enter 2 to book a ticket in FirstClass section
1
Enter passenger name:
sam
Enter 1 To book a Ticket in Economy section
Enter 2 to book a ticket in FirstClass section
1
Economy is full Do you want to book in first class
Enter 1 if you want to book 2 to no
1
Enter passenger name:
raj
Enter 1 To book a Ticket in Economy section
Enter 2 to book a ticket in FirstClass section
2
Enter passenger name:
sam
Enter 1 To book a Ticket in Economy section
Enter 2 to book a ticket in FirstClass section
1
No seats in two sections
The list of passengers who booked tickets
passenger name: shiva section: Economy Code: 5Q9SZX
passenger name: raju section: Economy Code: W3UPGB
passenger name: kumar section: Economy Code: V4I4X5
passenger name: sam section: Economy Code: XT618J
passenger name: raj section: First Class Code: CD2GVQ
passenger name: sam section: First Class Code: ZRFGG8





