Question: You will be implementing the Iterator Design Pattern to view flights. Code must follow logic of the uml diagram. Don't edit the driver except to

You will be implementing the Iterator Design Pattern to view flights. Code must follow logic of the uml diagram. Don't edit the driver except to add a package iterator. Please make the files Airline.java, Flight.java, FlightIterator.java
AirlineDriver
import java.util.Iterator;
import java.util.Scanner;
public class AirlineDriver {
public AirlineDriver(){
Airline american = new Airline("American Airlines");
Scanner reader = new Scanner(System.in);
System.out.print("
Enter Origin Airport Code: ");
String fromCode = reader.nextLine();
System.out.print("Enter Destination Airport Code: ");
String toCode = reader.nextLine();
Iterator flights = american.createIterator(fromCode, toCode);
System.out.println();
if(flights == null){
return;
}
while(flights.hasNext()){
System.out.println(""+ flights.next());
}
}
public static void main(String[] args){
AirlineDriver driver = new AirlineDriver();
}
}
Airport
public enum Airport {
ATL("Hartsfield-Jackson Atlanta International Airport"),
DFW("DallasFort Worth International Airport"),
DEN("Denver International Airport"),
ORD("Chicagos OHare International Airport"),
LAX("Los Angeles International Airport"),
JFK("John F. Kennedy International Airport"),
LAS("Harry Reid International Airport in Las Vegas"),
MCO("Orlando International Airport"),
MIA("Miami International Airport"),
CLT("Charlotte Douglas International Airport"),
SEA("SeattleTacoma International Airport"),
PHX("Phoenix Sky Harbor International Airport"),
EWR("Newark Liberty International Airport"),
SFO("San Francisco International Airport"),
IAH("Houstons George Bush Intercontinental Airport"),
BOS("Bostons General Edward Lawrence Logan International Airport"),
FLL("Fort Lauderdale/Hollywood International Airport"),
MSP("MinneapolisSt. Paul International Airport"),
LGA("LaGuardia Airport in New York"),
DTW("Detroit Metro Wayne County Airport");
public final String label;
private Airport(String label){
this.label = label;
}
}
Output
Enter Origin Airport Code: CLT
Enter Destination Airport Code: SEA
CLT to SEA 10:15-15:10(4h 55m) Direct Flight
CLT to SEA 12:15-17:10(4h 55m) Direct Flight
CLT to SEA 14:15-18:10(3h 55m) Direct Flight
CLT to SEA 10:01-22:10(12h 9m)2 Transfers
CLT to SEA 06:01-14:00(7h 59m)1 Stopover
CLT to SEA 08:01-23:19(15h 18m)2 Transfers
Invalid:
Enter Origin Airport Code: sdd
Enter Destination Airport Code: dfs
Must enter valid airport codes
Flights
477B, CLT, SEA, 10:15,15:10,0
887T, CLT, ATL, 06:23,07:43,0
234B, CLT, JFK,08:45,10:25,0
344B, CLT, SEA, 12:15,17:10,0
299T, CLT, ATL, 08:43,09:03,0
843B, CLT, JFK,09:45,11:25,0
544B, CLT, SEA, 14:15,18:10,0
899T, CLT, ATL, 10:43,11:03,0
443B, CLT, JFK,11:45,13:25,0
767B, CLT, SEA, 10:01,22:10,2
394T, CLT, ATL, 08:43,13:03,1
742B, CLT, JFK,09:45,15:20,1
665B, CLT, SEA, 06:01,14:00,1
856T, CLT, ATL, 10:43,15:03,1
994B, CLT, JFK,12:45,18:20,1
656B, CLT, SEA, 08:01,23:19,2
FlightLoader
import java.io.File;
import java.io.FileNotFoundException;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Scanner;
public class FlightLoader {
//relative path to the file, based on the directory you currently have open
private static final String FILE_NAME = "iterator/flights.txt";
/**
* Returns a list of all Flights from the text file
* @return An ArrayList of Flights
*/
public static ArrayList getFlights(){
ArrayList flights = new ArrayList();
try {
File file = new File(FILE_NAME);
Scanner scanner = new Scanner(file);
//loop through to get each question
while (scanner.hasNextLine()){
String[] data = scanner.nextLine().split(",");
String flightNum = data[0].trim();
Airport from = getAirport(data[1].trim());
Airport to = getAirport(data[2].trim());
LocalTime startTime = LocalTime.parse(data[3].trim());
LocalTime endTime = LocalTime.parse(data[4].trim());
int numTransfers = Integer.parseInt(data[5].trim());
flights.add(new Flight(flightNum, from, to, startTime, endTime, numTransfers));
}
scanner.close();
} catch (FileNotFoundException e){
System.out.println("Sorry, we could not properly read the questions file");
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
return flights;
}
private static Airport getAirport(String data){
return Airport.valueOf(data.trim().toUpperCase());
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!