In: Computer Science
public class Flight extends java.lang.Object
This class represents a single flight within the travel agency system.
Constructor Summary
Constructor and Description |
---|
Flight(java.lang.String airline, int flightNum,
java.lang.String from, java.lang.String to, java.util.Calendar
leavesAt, java.util.Calendar arrives, double price)
Creates a new flight leg in the system. |
Method Summary
Modifier and Type | Method and Description |
---|---|
double | getPrice()
Retrieves the price of this flight. |
java.lang.String | toString()
Retrieves a formatted string summarizing this Flight. |
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, waitConstructor Detail
Flight
public Flight(java.lang.String airline, int flightNum, java.lang.String from, java.lang.String to, java.util.Calendar leavesAt, java.util.Calendar arrives, double price)
Creates a new flight leg in the system.
Parameters:
airline - The two letter airline code (e.g, "DL", "AA")
flightNum - The unique flight number on a given day
from - The three letter airport code for the departure airport (e.g, "OMA")
to - The three letter airport code for the arrival airport (e.g., "CDG")
leavesAt - The departure time
arrives - The arrival time
price - The price for this flight in US Dollars.
/**************************Flight.java************************/
import java.util.Calendar;
public class Flight {
private String airline;
private int flightNum;
private String from;
private String to;
private Calendar leavesAt;
private Calendar arrives;
private double price;
public Flight(String airline, int flightNum, String
from, String to, Calendar leavesAt, Calendar arrives,
double price)
{
this.airline = airline;
this.flightNum = flightNum;
this.from = from;
this.to = to;
this.leavesAt = leavesAt;
this.arrives = arrives;
this.price = price;
}
public double getPrice() {
return price;
}
public String getAirline() {
return airline;
}
public int getFlightNum() {
return flightNum;
}
public String getFrom() {
return from;
}
public String getTo() {
return to;
}
public Calendar getLeavesAt() {
return leavesAt;
}
public Calendar getArrives() {
return arrives;
}
@Override
public String toString() {
return "Airline: " + airline +
"\nFlight Number: " + flightNum + "\nFrom: " + from + "\nTo: " +
to
+ "\nLeaves At: " +
leavesAt.getTime().toString() + "\nArrives At: " +
arrives.getTime().toString() + "\nPrice: $" + price + "\n";
}
}
/************************TestFlight.java**********************./
import java.util.Calendar;
public class TestFlight {
public static void main(String[] args) {
Calendar leaveAt =
Calendar.getInstance();
leaveAt.set(Calendar.YEAR,
2020);
leaveAt.set(Calendar.MONTH,
10);
leaveAt.set(Calendar.DAY_OF_MONTH,
26);
leaveAt.set(Calendar.HOUR_OF_DAY,
5);
leaveAt.set(Calendar.MINUTE,
34);
Calendar arriveAt =
Calendar.getInstance();
arriveAt.set(Calendar.YEAR,
2020);
arriveAt.set(Calendar.MONTH,
10);
arriveAt.set(Calendar.DAY_OF_MONTH,
31);
arriveAt.set(Calendar.HOUR_OF_DAY,
7);
arriveAt.set(Calendar.MINUTE,
12);
Flight f1 = new Flight("DL", 5,
"OMA", "CDG", leaveAt, arriveAt, 234);
System.out.println("Flight
information");
System.out.println(f1.toString());
}
}
/****************output************/
Flight information
Airline: DL
Flight Number: 5
From: OMA
To: CDG
Leaves At: Thu Nov 26 05:34:17 IST 2020
Arrives At: Tue Dec 01 07:12:17 IST 2020
Price: $234.0
Please let me know if you have any doubt or modify the answer, Thanks :)