staticScannerinput=newScanner(System.in);// global scanner
staticStringname;staticStringocity;staticStringdcity;//assigning global variables for customer details
staticStringseat;
staticString[]flight={"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28",};//generating the random flight numbers
staticRandomrandom=newRandom();
publicstaticvoidmain(String[]args){
System.out.print(plane[0][0]);
details();//to allow repeatability at the end of the program
}
publicstaticvoiddetails(){
System.out.print("What is your name?");
name=input.next();
System.out.print("Where will you be flying from?");
ocity=input.next();
System.out.print("Where are you flying to?");
dcity=input.next();// getting customer details
Class();
}
publicstaticvoidClass(){
intfclass;//variable to determine class
System.out.print("Choose a flight class-"
+"\nPlease type 1 for First Class"
+"\nPlease type 2 for Business"
+"\nPlease type 3 for Economy \n");
switch(fclass=input.nextInt()){
case1://if user chooses first class
first();
break;
case2://if user chooses business class
business();
break;
case3://case if user chooses economy class
economy();
break;
default://case if user chooses an invalid option
System.out.println("Invalid option. Please choose a number from 1 to 3");//prints out an error message
break;
}
}
privatestaticvoidfirst(){
Stringfclass="First Class";
booleanavailable=false;
System.out.println("You have chosen First Class");//confirms class
for(inti=0;i<1;i++){
for(intj=0;j<3;j++){
if(plane[i][j]!="XX"){
available=true;//checking if seats are available
}
else
available=false;
}
}
if(available==true){//if seats are available
System.out.println("Would you like to choose a seat?(y/n)");
Stringchooseseat=input.next();
switch(chooseseat){
case"n":
System.out.println("You will be assigned a seat");//if user doesn't want to choose seat the subroutine to assign them one runs
assign(fclass);
case"y":
System.out.println("Choose a seat:");//otherwise they'll be sent to the subroutine to pick a seat
chooseseat(fclass);
return;
default:
System.out.println("Please pick y or n");//making sure they make a choice
}
}
else
noseat();//if there are no seats available
}
privatestaticvoidbusiness(){
Stringfclass="Business";
booleanavailable=false;
System.out.println("You have chosen Business");//confirms class
for(inti=2;i<4;i++){
for(intj=0;j<3;j++){
if(plane[i][j]!="XX"){
available=true;//checking if seats are available
}
else
available=false;
}
}
if(available==true){//if seats are available
System.out.println("Would you like to choose a seat?(y/n)");
Stringchooseseat=input.next();
switch(chooseseat){
case"n":
System.out.println("You will be assigned a seat");//if user doesn't want to choose seat the subroutine to assign them one runs
assign(fclass);
return;
case"y":
System.out.println("Choose a seat:");//otherwise they'll be sent to the subroutine to pick a seat
chooseseat(fclass);
return;
default:
System.out.println("Please pick y or n");//making sure they make a choice
break;
}
}
else
noseat();//if there are no seats available
}
privatestaticvoideconomy(){
Stringfclass="Economy";
booleanavailable=false;
System.out.println("You have chosen Economy");//confirms class
for(inti=5;i<9;i++){
for(intj=0;j<3;j++){
if(plane[i][j]!="XX"){
available=true;//checking if seats are available
}
else
available=false;
}
}
if(available==true){//if seats are available
System.out.println("Would you like to choose a seat?(y/n)");
Stringchooseseat=input.next();
switch(chooseseat){
case"n":
System.out.println("You will be assigned a seat");//if user doesn't want to choose seat the subroutine to assign them one runs
assign(fclass);
return;
case"y":
System.out.println("Choose a seat:");//otherwise they'll be sent to the subroutine to pick a seat
chooseseat(fclass);
return;
default:
System.out.println("Please pick y or n");//making sure they make a choice
break;
}
}
else
noseat();//if there are no seats available
}
privatestaticvoidnoseat(){
while(true){
System.out.println("There are no more seats in that class, can we move you to a different class?");
Stringnoseat=input.next();
switch(noseat){
case"n":
System.out.println("We are afraid we cannot accomodate you. The next flight to "+dcity+" is in 6 hours");//ends the program for them to restart for the next flight
return;
case"y":
Class();//takes them back to the class selection
return;
default:
System.out.println("Please pick y or n");
break;
}
}
}
privatestaticvoidassign(Stringfclass){
switch(fclass){
case"First Class":
for(inti=0;i<1;i++){
for(intj=0;j<3;j++){
if(plane[i][j]!="XX"){//checks through seats, finds one that hasn't been filled yet
seat=plane[i][j];
plane[i][j]="XX";
break;
}
}
}
case"Business":
for(inti=2;i<4;i++){
for(intj=0;j<3;j++){
if(plane[i][j]!="XX"){//checks through seats, finds one that hasn't been filled yet
seat=plane[i][j];
plane[i][j]="XX";
break;
}
}
}
case"Economy":
for(inti=5;i<9;i++){
for(intj=0;j<3;j++){
if(plane[i][j]!="XX"){//checks through seats, finds one that hasn't been filled yet
seat=plane[i][j];
plane[i][j]="XX";
break;
}
}
}
}
boardingpass(fclass);//loads up the boarding class
}
publicstaticvoidchooseseat(Stringfclass){
display();
System.out.println("");
System.out.println("Please choose a seat in your class");
System.out.println("1-18 is First Class, 19-45 is Business, 46-90 is Economy");
switch(fclass){
case"First Class":
seat=input.next();
for(inti=0;i<6;i++)
for(intj=0;j<3;j++){
System.out.println(plane[i][j]);
if(seat==plane[i][j])
plane[i][j]="XX";
}
if(Integer.parseInt(seat)>18){//making sure the seat they choose is in their chosen class
chooseseat(fclass);}
boardingpass(fclass);//loads up the boarding class
break;
case"Business":
seat=input.next();
for(inti=6;i<15;i++){
for(intj=0;j<3;j++){
if(seat==plane[i][j])
plane[i][j]="XX";
}
if(Integer.parseInt(seat)<18){//making sure the seat they choose is in their chosen class
chooseseat(fclass);}
if(Integer.parseInt(seat)>45){
chooseseat(fclass);}
boardingpass(fclass);//loads up the boarding class
}
break;
case"Economy":
seat=input.next();
for(inti=15;i<30;i++){
for(intj=0;j<3;j++){
if(seat==plane[i][j])
plane[i][j]="XX";
}
if(Integer.parseInt(seat)<45){//making sure the seat they choose is in their chosen class
chooseseat(fclass);}
boardingpass(fclass);//loads up the boarding class
break;
}
}
}
privatestaticvoidboardingpass(Stringfclass){
intselect=random.nextInt(flight.length);//randomly generates the flight number
System.out.println("This is your boarding pass:");
System.out.println("");
System.out.println("|[ BOARDING PASS ]|");
System.out.println("Name : "+name);
System.out.println("From : "+ocity);
System.out.println("To : "+dcity);
System.out.println("Flight : FL"+flight[select]);
System.out.println("Seat No. : "+seat);
System.out.println("Section : "+fclass);
display();
System.out.println("");
System.out.println("Would you like to book another?");//the choice to restart the program essentially
Stringagain=input.next();
switch(again){
case"y":
details();
default:
System.out.println("Thank you for shopping with us today");
}
}
publicstaticvoiddisplay(){//purely all for displaying the seating chart