A320
Flight Trip Reservation System
Consider
the following trip related objects details:
An
abstract base Person object has the following attributes:
· First name
· Last name
· Gender (M/F)
· Date of birth
A
Passenger object, who is a Person, has the following extra attributes:
· Passport number
· Nationality
· Has a valid entrance VISA
· Has flight seat preference which could
be Window, Aisle, or None
The
A320 flight has rows of seats. The abstract base Seat object has the following
attributes:
· The row number (from 1-31)
· The column number (from A to F)
· The seat number which is consists of
column number and row number (e.g. A12)
· The Passenger details if the seat is
reserved.
The flight seats are two types, first class seats and economy seats.
The First Class Seat has the following extra attributes:
· Food preferences (either Caviar or
Steak)
· Drink preferences (either Liquorice or
Carob)
While
the Economy Seat has the following extra attributes:
· Food preferences (either Chicken or
Fish)
· Drink preferences (either Cola or
Juice)
Finally,
we have a flight Trip that has the following attributes:
· A ragged array of seats.
· Flight number.
· From airport name.
· To airport name
· The trip distance
· The departure time
· The arrival time
Task1:
Class Implementations
Implement
the classes according to the mentioned attributes and relationship between
classes.
For
the constructor and behaviours:
· Setters and getters to the class
attributes as necessary.
· Constructors using the instance
attributes as necessary.
Task2:
A320 Flight Trip Reservation System
Create
a driver class and in the main method, loop to show the following menu:
0)
To add a new trip. Use ArrayList to add the trips.
1)
To reserve a new empty seat suggested be the system (first class or economy)
for a new
passenger
into a specific trip taking into consideration the passenger’s seat preference.
2)
To display the passenger information for a specific seat (if the seat is not
empty) in a specific
trip.
3)
To search for a passenger by first or last name in a specific trip
4)
To print a list of the first class seat’s passengers sorted by seat numbers in
a specific trip.
5)
To print a list of the economy seat’s passengers sorted by seat numbers in a
specific trip.
6)
To change a passenger seat in a specific trip.
7)
To cancel a passenger reservation in a specific trip.
8)
To exit execution
Finally,
adequately implement the previous options (from 0 to 8).
--------------------------
CODE Answer)
package pp;
import java.lang.reflect.Array;
import java.nio.channels.ShutdownChannelGroupException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Scanner;
abstract class Person
{
protected String firstName;
protected String lastName;
protected char gender;
protected String bday;
public void
setFirstName(String fname)
{
this.firstName=fname;
}
public String
getFirstName()
{
return this.firstName;
}
public void setLastName(String
lname)
{
this.lastName=lname;
}
public String getLastName()
{
return this.lastName;
}
public void setGender(char
c)
{
this.gender=c;
}
public char getGender(char
c)
{
return this.gender;
}
public void setBday(String
bday)
{
this.bday = bday;
}
public String getBday()
{
return this.bday;
}
}
class Passenger extends Person
{
private long passportNo;
private String nationality;
private long visaNo;
private String seatPref;
public Passenger(String
fname, String lname, char g, String bday, long pno, String nat, long vno,
String seatp)
{
this.firstName=fname;
this.lastName=lname;
this.gender=g;
this.bday=bday;
this.passportNo=pno;
this.nationality=nat;
this.visaNo=vno;
this.seatPref=seatp;
}
public void
setPreference(String s)
{
this.seatPref=s;
}
public String
getPreference()
{
return this.seatPref;
}
public void
setPassportNo(long pn)
{
this.passportNo=pn;
}
public long getPassportNo()
{
return this.passportNo;
}
public void
setNationality(String n)
{
this.nationality=n;
}
public String
getNationality()
{
return
this.nationality;
}
public void setVisaNo(long
vn)
{
this.visaNo=vn;
}
public long getVisaNo()
{
return this.visaNo;
}
public String getDetails()
{
return "Name:
"+this.firstName+"\nLast Name: "+this.lastName+"\nGender:
"+this.gender+"\nBirthday(DD-MM-YYYY): "+this.bday+
"\nPassport No: "+this.passportNo+"\nNationality:
"+this.nationality+"\nVisa No: "+this.visaNo+"\nSeat Pref:
"+ this.seatPref+"\n";
}
}
abstract class Seat
{
protected Passenger
passenger;
protected String seatno;
protected int row;
protected char col;
protected boolean
researved;
public Seat()
{
this.researved = false;
this.passenger=null;
}
public void setRow(int i)
{
this.row=i;
}
public int getRow()
{
return this.row;
}
public void setCol(char i)
{
this.col=i;
}
public char getCol()
{
return this.col;
}
public void setSeatNo(int
i, char col)
{
this.seatno =
col+Integer.toString(i);
}
public String getSeatNo()
{
return this.seatno;
}
public void
setPass(Passenger i)
{
this.passenger=i;
}
public Passenger getPass()
{
return this.passenger;
}
public void
setReservation(boolean i)
{
this.researved=i;
}
public boolean
getReservation()
{
return this.researved;
}
}
class EconomySeat extends Seat
{
public String
foodPreference;
public String
drinkPreference;
public EconomySeat()
{
super();
}
public void setFP(String
fp)
{
this.foodPreference=fp;
}
public String getFP()
{
return
this.foodPreference;
}
public void setDP(String
dp)
{
this.drinkPreference=dp;
}
public String getDP()
{
return
this.drinkPreference;
}
}
class FirstClassSeat extends Seat
{
private String
foodPreference;
private String drinkPreference;
public FirstClassSeat()
{
super();
}
public void setFP(String
fp)
{
this.foodPreference=fp;
}
public String getFP()
{
return
this.foodPreference;
}
public void setDP(String
dp)
{
this.drinkPreference=dp;
}
public String getDP()
{
return
this.drinkPreference;
}
}
class FlightTrip
{
private Seat[][] seats;
private String flightNo;
private String toAirport;
private int distance;
private String
departureTime;
private String arrivalTime;
public FlightTrip(String
flightno,String des,int dis,String dtime,String atime)
{
this.arrivalTime =
atime;
this.departureTime =
dtime;
this.distance = dis;
this.flightNo =
flightno;
this.toAirport=des;
this.seats = new
Seat[31][];
this.seats[0] = new
FirstClassSeat[4];
this.seats[1] = new
FirstClassSeat[4];
for(int
i=2;i<31;i++)
{
if(i==4||i==5||i==13)
this.seats[i] =
new EconomySeat[0];
else
this.seats[i] =
new EconomySeat[6];
}
char name[]=
{'A','C','D','F'};
for(int i=0;i<2;i++)
{
for(int
j=0;j<=3;j++)
{
this.seats[i][j]=new FirstClassSeat();
this.seats[i][j].setRow(i);
this.seats[i][j].setCol(name[j]);
this.seats[i][j].setSeatNo(i,name[j]);
}
}
for(int i=2;i<31;i++)
{
if(i==4||i==5||i==13)
continue;
char col = 'A';
for(int
j=0;j<=5;j++)
{
this.seats[i][j]=new EconomySeat();
this.seats[i][j].setRow(i);
this.seats[i][j].setCol(col);
this.seats[i][j].setSeatNo(i,col);
col++;
}
}
}
public String getFlightNo()
{
return this.flightNo;
}
public void
setFlightNo(String flightno)
{
this.flightNo =
flightno;
}
public Seat[][] getSeats()
{
return this.seats;
}
public String
getArrivalTime()
{
return
this.arrivalTime;
}
public void setArrivalTime(String
atime)
{
this.arrivalTime =
atime;
}
public String
getDepartureTime()
{
return
this.departureTime;
}
public void
setDepartureTime(String dtime)
{
this.departureTime =
dtime;
}
public String
getDestination()
{
return this.toAirport;
}
public void
setDestination(String des)
{
this.toAirport = des;
}
public int getDistance()
{
return this.distance;
}
public void setDistance(int
dis)
{
this.distance = dis;
}
public String getDetails()
{
return
this.getFlightNo()+" "+this.getDestination()+"
"+this.getArrivalTime()+" "+this.getDepartureTime();
}
}
public class Test {
public static
ArrayList<FlightTrip> trip =new ArrayList<>();
public static Scanner myObj
= new Scanner(System.in);;
public static int
ShowTrips()
{
System.out.println("Please Select Trip\n");
System.out.println(" FlightNo Destination Departure
Arrival\n");
int i=0;
for(FlightTrip t: trip)
{
System.out.println(Integer.toString(i)+")
"+t.getDetails()+"\n");
i++;
}
int ans =
myObj.nextInt();
return ans;
}
public static Passenger
newPassenger()
{
System.out.println("Enter First Name");
String fname =
myObj.next();
System.out.println("Enter Last Name");
String lname =
myObj.next();
System.out.println("Enter Gender.(M/F)");
char c =
myObj.next().charAt(0);
System.out.println("Enter Birth Date.(DD-MM-YYYY)");
String d =myObj.next();
System.out.println("Enter Nationality.");
String nat =
myObj.next();
System.out.println("Enter
Passport Number.");
long pno
=myObj.nextLong();
System.out.println("Enter Visa Number.");
long vno
=myObj.nextLong();
System.out.println("Enter Seat Preference.\nChoose Option\n1.
Window\n2. Aisle\n3. None");
int sp =
myObj.nextInt();
String
seatp="None";
if(sp==1)
seatp="Window";
else if(sp==2)
seatp="Aisle";
return new
Passenger(fname, lname, c, d, pno, nat, vno, seatp);
}
public static void
printWindowSeatFirst(Seat s[][])
{
System.out.println("Empty Window Seats in First class.");
for(int i=0;i<2;i++)
{
if(s[i][0].getReservation()==false)
System.out.println(s[i][0].getSeatNo());
if(s[i][3].getReservation()==false)
System.out.println(s[i][3].getSeatNo());
}
}
public static void
printWindowSeatEco(Seat s[][])
{
System.out.println("Empty Window Seats in Economy class.");
for(int
i=2;i<31;i++)
{
if(i==4||i==5||i==13)
continue;
if(s[i][0].getReservation()==false)
System.out.println(s[i][0].getSeatNo());
if(s[i][5].getReservation()==false)
System.out.println(s[i][5].getSeatNo());
}
}
public static void
printAisleSeatFirst(Seat s[][])
{
System.out.println("Empty Asile Seats in First class.");
for(int i=0;i<2;i++)
{
if(s[i][1].getReservation()==false)
System.out.println(s[i][1].getSeatNo());
if(s[i][2].getReservation()==false)
System.out.println(s[i][2].getSeatNo());
}
}
public static int
getseatcol(char c)
{
if(c=='A')
return 0;
else if(c=='C')
return 1;
else if(c=='D')
return 2;
else
return 3;
}
public static void
printAisleSeatEco(Seat s[][])
{
System.out.println("Empty Asile Seats in Economy class.");
for(int
i=2;i<31;i++)
{
if(i==4||i==5||i==13)
continue;
if(s[i][2].getReservation()==false)
System.out.println(s[i][2].getSeatNo());
if(s[i][3].getReservation()==false)
System.out.println(s[i][3].getSeatNo());
}
}
public static void
printMiddleSeat(Seat s[][])
{
System.out.println("Empty Middle Seats.");
for(int
i=2;i<31;i++)
{
if(i==4||i==5||i==13)
continue;
if(s[i][1].getReservation()==false)
System.out.println(s[i][1].getSeatNo());
if(s[i][4].getReservation()==false)
System.out.println(s[i][4].getSeatNo());
}
}
public static void
newTrip()
{
System.out.println("Enter Flight No");
String flightno =
myObj.next();
System.out.println("Enter Destination");
String des =
myObj.next();
System.out.println("Enter Distance");
int dis = myObj.nextInt();
System.out.println("Enter Departure Time(HH::MM)");
String dtime =
myObj.next();
System.out.println("Enter Arrival Time No(HH::MM)");
String atime =
myObj.next();
FlightTrip t = new
FlightTrip(flightno,des,dis,dtime,atime);
trip.add(t);
System.out.println("Trip Added successfully!");
}
public static void
resSeat(Passenger f,int ans)
{
Seat s[][] =
trip.get(ans).getSeats();
Passenger p=f;
if(f==null)
{
p= newPassenger();
}
else
System.out.println("Please choose seat:");
if(p.getPreference()=="Aisle")
{
printAisleSeatFirst(s);
printAisleSeatEco(s);
printWindowSeatFirst(s);
printWindowSeatEco(s);
printMiddleSeat(s);
}
else
{
printWindowSeatFirst(s);
printWindowSeatEco(s);
printAisleSeatFirst(s);
printAisleSeatEco(s);
printMiddleSeat(s);
}
System.out.println("Enter Seat Numer to reserve.(RowCol)");
String seat =
myObj.next();
int col =
seat.charAt(0)-65;
int row =
Integer.parseInt(seat.substring(1));
if(row<2)
{
col =
getseatcol(seat.charAt(0));
}
String fp;
String dp;
if(row<2)
{
System.out.println("Choose Food Prefernce\n1. Caviar\n2. Steak
");
int fpoption =
myObj.nextInt();
if(fpoption==1)
fp="Caviar";
else
fp="Steak";
System.out.println("Choose Drink Prefernce\n1. Liquorice\n2. Carob
");
int dpoption =
myObj.nextInt();
if(dpoption==1)
dp="Liquorice";
else
dp="Carob";
}
else
{
System.out.println("Choose Food Prefernce\n1. Chicken\n2. Fish
");
int fpoption =
myObj.nextInt();
if(fpoption==1)
fp="Chicken";
else
fp="Fish";
System.out.println("Choose Drink Prefernce\n1. Cola\n2. Juice
");
int dpoption =
myObj.nextInt();
if(dpoption==1)
dp="Cola";
else
dp="Juice";
}
s[row][col].setReservation(true);
s[row][col].setPass(p);
System.out.println("Reservation Successful!");
}
public static void
passDetail(int ans)
{
System.out.println("Enter Seat Numer.(RowCol)");
String seat =
myObj.next();
int col =
seat.charAt(0)-65;
int row =
Integer.parseInt(seat.substring(1));
if(row<2)
col =
getseatcol(seat.charAt(0));
Seat s[][] =
trip.get(ans).getSeats();
if(s[row][col].getReservation()==true)
{
System.out.println(s[row][col].getPass().getDetails());
}
else
{
System.out.println("Seat is empty.");
}
}
public static String
searchPass(int ans)
{
String pattern ;
System.out.println("Enter 1 for searching by first name or 2 for
searching by last name");
int option =
myObj.nextInt();
if(option==1)
{
System.out.println("Enter First Name");
}
else
{
System.out.println("Enter Last Name");
}
pattern =myObj.next();
Seat s[][] =
trip.get(ans).getSeats();
for(int i=0;i<2;i++)
{
for(int
j=0;j<4;j++)
{
if(s[i][j].getReservation())
{
if(option==1)
{
if(s[i][j].getPass().getFirstName().equals(pattern))
{
return s[i][j].getPass().getDetails();
}
}
else
{
if(s[i][j].getPass().getLastName().equals(pattern))
{
return
s[i][j].getPass().getDetails();
}
}
}
}
}
for(int
i=2;i<31;i++)
{
for(int
j=0;j<6;j++)
{
if(s[i][j].getReservation())
{
if(option==1)
{
if(s[i][j].getPass().getFirstName().equals(pattern))
{
return s[i][j].getPass().getDetails();
}
}
else
{
if(s[i][j].getPass().getLastName().equals(pattern))
{
return
s[i][j].getPass().getDetails();
}
}
}
}
}
return "No
passenger found";
}
public static void
firstClassPass(int ans)
{
Seat s[][] = trip.get(ans).getSeats();
for(int i=0;i<2;i++)
{
for(int
j=0;j<4;j++)
{
if(s[i][j].getReservation())
System.out.println(s[i][j].getSeatNo()+" "+
s[i][j].getPass().getDetails());
}
}
}
public static void
ecoClassPass(int ans)
{
Seat s[][] =
trip.get(ans).getSeats();
for(int
i=2;i<31;i++)
{
for(int
j=0;j<6;j++)
{
if(s[i][j].getReservation())
System.out.println(s[i][j].getSeatNo()+"
"+ s[i][j].getPass().getDetails());
}
}
}
public static void
changeSeat(int ans)
{
System.out.println("Enter Current Seat No.(RowCol)");
String seat =
myObj.next();
int col = seat.charAt(0)-65;
int row =
Integer.parseInt(seat.substring(1));
if(row<2)
col =
getseatcol(seat.charAt(0));
Seat s[][] =
trip.get(ans).getSeats();
s[row][col].setReservation(false);
Passenger p=
s[row][col].getPass();
s[row][col].setPass(null);
System.out.println("Cancellation Successful!");
resSeat(p,ans);
}
public static void
cancelRes(int ans)
{
System.out.println("Enter
Seat Numer to cancel.(RowCol)");
String seat =
myObj.next();
int col =
seat.charAt(0)-65;
int row =
Integer.parseInt(seat.substring(1));
if(row<2)
col =
getseatcol(seat.charAt(0));
Seat s[][] =
trip.get(ans).getSeats();
s[row][col].setReservation(false);
s[row][col].setPass(null);
System.out.println("Cancellation Successful!");
}
public static void
main(String[] args)
{
int flag=0;
while(true)
{
System.out.println("Please Choose the option from below
list:\n\n0). Add a new Trip.\n1). Researve a seat.\n"
+ "2).
Display Passenger Information.\n3). Search a passenger by First Name/Last
Name.\n"
+ "4).
Print a list of the first class seat’s passengers.\n5). Print a list of the
economy seat’s passengers.\n"
+ "6).
Change a passenger seat.\n7). Cancel a passenger reservation.\n8). Exit");
int ans = myObj.nextInt();
switch(ans)
{
case 0:
{
newTrip();
break;}
case 1:
{
int ans2 =
ShowTrips();
resSeat(null,ans2);
break;
}
case 2:
{
int ans2 =
ShowTrips();
passDetail(ans2);
break;
}
case 3:
{
int ans2 =
ShowTrips();
System.out.println(searchPass(ans2));
break;
}
case 4:
{
int ans2 =
ShowTrips();
firstClassPass(ans2);
break;
}
case 5:
{
int ans2 =
ShowTrips();
ecoClassPass(ans2);
break;
}
case 6:
{
int ans2 =
ShowTrips();
changeSeat(ans2);
break;
}
case 7:
{
int ans2 =
ShowTrips();
cancelRes(ans2);
break;
}
case 8: return;
}
}
}
}
No comments:
Post a Comment