import java.util.Scanner; /** * Flight.java
* Implements a class named Flight that represents an airline flight. * It contains instance data that represent the name of the airline, the flight number, * the flights origin and destination cities. * A static method readFlight() is used to get values from the keyboard.
* In addition to the getters and setters, the class provides a String representation of * Flight object.
* The main shows the use of a driver/testing program * @author CS230 Staff (TM) * @version Sept 23, 2022 * */ public class Flight { //Instance variables private String airline; private int flightNum; // could be String since we don't do arithmetic with these nums private String origin; private String destination; /** * Creates Flight objects, given their characteristics * * @param airline The name of the airline * @param flNum The flight number * @param from The city of origin * @param to The city of destination * * */ public Flight(String airline, int flNum, String from, String to) { this.airline = airline; // here "this" is needed, //since instance and input vars have the same name flightNum = flNum; origin = from; destination = to; } /** * returns this flight's for airline * * @return The airline name * */ public String getAirline() { return airline; } /** * Sets this flight's airline * * @param airline The updated name of the airline for this flight * */ public void setAirline(String airline) { this.airline = airline; } /** * Getter for flightNum of this flight * * @return The flight number **/ public int getFlightNumber() { return flightNum; } /** * Setter for flightNum of this flight * @param flNum The number for this flight **/ public void setFlightNumber(int flNum) { flightNum = flNum; } /** * returns the origin of this flight * * @return The origin city of the flight * */ public String getOrigin() { return origin; } /** * Sets the origin of this flight to the input one * * @param fromCity The origin city of the flight * */ public void setOrigin(String fromCity) { origin = fromCity; } /** * Gets the destination of this flight * * @return The destination city of this flight **/ public String getDestination() { return destination; } /** * Sets the destination of this flight * * @param toCity The destination city of this flight **/ public void setDestination(String toCity) { destination = toCity; } /** * Checks whether the destination of the first flight is the same as origin of the second. * It returns true if this is the case, false otherwise * *@param f1 the first flight *@param f2 the second flight *@return true if the first flight's destination is the same as the origin of the second. **/ public static boolean stopOver(Flight f1, Flight f2){ return f1.destination.equals(f2.origin); // using the equals() on Strings } /** * Returns a String representation of this flight * * @return The state of this flight, as a string * */ public String toString(){ String s = airline + flightNum + " from " + origin + " to " + destination + " "; return s; } /** * Reads from the keyboard the properties of a Flight. * Returns the Flight object. Used in main(). * Notice that this is a "static" method, because it is to be used by the static main(). * @param s the Scanner object to be used for reading user input * @return The flight as entered by the user **/ public static Flight readFlight(Scanner s){ System.out.println ("Enter the name of the Airline (then hit return):"); String a = s.nextLine(); System.out.println ("Enter the Flight number:(then hit return)"); int b = s.nextInt(); String nl = s.nextLine(); // to clear the input stream System.out.println ("Enter the name of departing city:"); String c = s.nextLine(); System.out.println ("Enter the name of destination city:"); String d = s.nextLine(); //System.out.println ("Flight "+a+b+ " connects "+c+" to "+d); return new Flight(a,b,c,d); } /** * Driver, used to test this class. **/ public static void main (String[] args) { //Scanner scan = new Scanner(System.in); //f1 = readFlight(scan); //scan.close(); Flight f1 = new Flight("AA", 123, "BOS", "LAX"); System.out.println("Testing manual entry\nNew Flight f1 entered:"+f1); Flight f2 = new Flight("DL", 55, "LAX", "SFO"); System.out.println("Testing constructor\nNew Flight f2 created:"+f2); Flight f3 = new Flight("OA", 1234, "SFO", "ATH"); System.out.println("Testing constructor\nNew Flight f3 created:"+f3); System.out.println("Testing getAirline(f1):(AA)->"+f1.getAirline()); System.out.println("Testing getFlightNumber(f1):(123)->"+f1.getFlightNumber()); System.out.println("Testing getOrigin(f1):(BOS)->"+f1.getOrigin()); System.out.println("Testing getDestination(f1):(LAX)->"+f1.getDestination()); System.out.println("Flights "+f1+" and "+f2+" share stopover city (TRUE)->: "+stopOver(f1,f2)); System.out.println("Flights "+f2+" and "+f1+" share stopover city (FALS)->: "+stopOver(f2,f1)); System.out.println("Flights "+f2+" and "+f3+" share stopover city (TRUE->): "+stopOver(f2,f3)); System.out.println("Testing setDestination(f1):BOS"); f1.setDestination("BOS"); System.out.println("Testing setOrigin(f1):SFO"); f1.setOrigin("SFO"); System.out.println("Testing changes to Flight f1:"+f1); System.out.println("Flights "+f1+" and "+f2+" share stopover city (FALSE)->: "+stopOver(f1,f2)); System.out.println("Flights "+f2+" and "+f1+" share stopover city (TRUE)->: "+stopOver(f2,f1)); } }