Question: this is the objective i was given as well as the code i am running, posted way at the bottom. I am not getting the

this is the objective i was given as well as the code i am running, posted way at the bottom. I am not getting the output that is asked which is to get all the reservations 815. If i input the whole text from the reservation text file I am getting all reservations when i just need the 815 ones. Not sure what I did wrong in my code if anyone can help me out please

this is the objective i was given as well as the codei am running, posted way at the bottom. I am not gettingthe output that is asked which is to get all the reservations

These are the text flies that were given:

flight-schedule.txt

866,commercial,SAT,LAS,13:05,14:00 815,commercial,SAT,LAS,08:00,09:05 267,private,SAT,SLC,05:50,09:50 896,commercial,SAT,JFK,07:00,11:50 

Reservation.txt:

815,Reyes,Hugo,22 815,Reyes,Carmen,23 815,Reyes,Diego,21 815,Reyes,David,20 896,Goodspeed,Horace,9 866,Littleton,Claire,2 866,Locke,John,101 866,Ford,James,4 267,Hume,Desmond,1 267,Pace,Charlie,3 815,Kwon,Jin-Soo,2 815,Shephard,Jack,132 896,Dawson,Michael,24 896,Burke,Juliet,35 815,Straume,Miles,47 267,Kwon,Sun-Hwa,6 267,Jarrah,Sayid,8 815,Linus,Benjamin,118 896,Linus,Roger,118 866,Alpert,Richard,120 866,Rutherford,Shannon,15 896,Nadler,Rose,57 896,Carlyle,Boon,37 896,Nadler,Bernard,58 866,Rousseau,Alexandra,13 866,Friendly,Tom,6 866,Shephard,Christian,14 896,Hawking,Eloise,47 866,Widmore,Charles,103 866,Chang,Pierre,115 896,Reyes,Carmen,22 815,Smith,Elizabeth,67 866,Fernandez,Nikki,89 896,Pace,Liam,135 815,Widmore,Penelope,92 815,Rom,Ethan,99 866,Keamy,Martin,111 896,Chandler,Cindy,88 896,Arzt,Leslie,42 

_________________________________

Flight.java

__________________________________

/** * This class represents a Flight. */ import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.Collection; public class Flight implements Comparable {

// Instance variables private int flightNum; private String origin; private String destination; private LocalTime departureTime; private LocalTime arrivalTime; private Collection reservations;

/** * Parameterized constructor * * @param flightNum * - flight number * @param origin * - origin of the flight * @param destination * - destination of the flight * @param departureTime * - departure time of the flight * @param arrivalTime * - arrival time of the flight */ public Flight(int flightNum, String origin, String destination, String departureTime, String arrivalTime) { this.flightNum = flightNum; this.origin = origin; this.destination = destination; this.departureTime = LocalTime.parse(departureTime, DateTimeFormatter.ofPattern("H:m")); this.arrivalTime = LocalTime.parse(arrivalTime, DateTimeFormatter.ofPattern("H:m")); this.reservations = new ArrayList(); }

/** * Returns the flightNum */ public int getFlightNum() { return flightNum; }

/** * Returns the origin */ public String getOrigin() { return origin; }

/** * Returns the destination */ public String getDestination() { return destination; }

/** * Returns the departureTime */ public LocalTime getDepartureTime() { return departureTime; }

/** * Returns the arrivalTime */ public LocalTime getArrivalTime() { return arrivalTime; }

/** * Returns the reservations for this flight */ public Collection getReservations() { return reservations; }

/** * Sets the flight number * * @param flightNum * the flightNum to set */ public void setFlightNum(int flightNum) { this.flightNum = flightNum; }

/** * Sets the origin * * @param origin * the origin to set */ public void setOrigin(String origin) { this.origin = origin; }

/** * Sets the destination * * @param destination * the destination to set */ public void setDestination(String destination) { this.destination = destination; }

/** * Sets the departure time * * @param departureTime * the departureTime to set */ public void setDepartureTime(LocalTime departureTime) { this.departureTime = departureTime; }

/** * Sets the departure time * * @param arrivalTime * the arrivalTime to set */ public void setArrivalTime(LocalTime arrivalTime) { this.arrivalTime = arrivalTime; }

/** * Sets the reservations * * @param reservations * the reservations to set */ public void setReservations(Collection reservations) { this.reservations = reservations; }

/** * Adds a new reservation to this flight * * @param newReservation * - reservation to be added */ public void addReservation(Reservation newReservation) { reservations.add(newReservation); }

/** * Checks whether this flight is commercial or not * * @return - true if flight is commercial, false otherwise */ public boolean isCommercialFlight() { return (this instanceof CommercialFlight); }

/** * Returns the flight information. */ @Override public String toString() { return (String.format("%-10s", (isCommercialFlight() ? "Commercial" : "Private")) + " Flight " + flightNum + " from " + origin + " to " + destination + " departs: " + departureTime + " arrives: " + arrivalTime + ". Seats reserved: " + reservations.size()); }

/** * Compares this flight time with flight f2 based on their departure time */ @Override public int compareTo(Flight f2) { if (this.departureTime.getHour() == f2.getDepartureTime().getHour()) return (this.departureTime.getMinute() - f2.getDepartureTime().getMinute()); else return (this.departureTime.getHour() - f2.getDepartureTime().getHour()); }

/** * Takes a String parameter and returns a type of Flight object populated * from the data string */ public static Flight parseFlightInformation(String flightInfo) throws IllegalArgumentException, NumberFormatException { // Split flightInfo string String[] flightData = flightInfo.split(",");

// Check if there are sufficient arguments if (flightData.length == 6) { try { // Check whether flight is commercial or private if (flightData[1].equalsIgnoreCase("commercial")) return new CommercialFlight(Integer.parseInt(flightData[0]), flightData[2], flightData[3], flightData[4], flightData[5]); else if (flightData[1].equalsIgnoreCase("private")) return new PrivateFlight(Integer.parseInt(flightData[0]), flightData[2], flightData[3], flightData[4], flightData[5]); } catch (DateTimeParseException dtpe) { throw new IllegalArgumentException( "Invalid time at: " + flightInfo); } } throw new IllegalArgumentException("Insufficient data: " + flightInfo); } }

_________________________________________

PrivateFlight.java

__________________________________________

/** * This class represents a Private Flight. */ public class PrivateFlight extends Flight {

/** * Parameterized constructor * * @param flightNum * - flight number * @param origin * - origin of the flight * @param destination * - destination of the flight * @param departureTime * - departure time of the flight * @param arrivalTime * - arrival time of the flight */ public PrivateFlight(int flightNum, String origin, String destination, String departureTime, String arrivalTime) { super(flightNum, origin, destination, departureTime, arrivalTime); } }

_______________________

CommercialFlight.java

_________________________

/** * This class represents a Commercial Flight. */ public class CommercialFlight extends Flight {

/** * Parameterized constructor * * @param flightNum * - flight number * @param origin * - origin of the flight * @param destination * - destination of the flight * @param departureTime * - departure time of the flight * @param arrivalTime * - arrival time of the flight */ public CommercialFlight(int flightNum, String origin, String destination, String departureTime, String arrivalTime) { super(flightNum, origin, destination, departureTime, arrivalTime); } }

_________________________________________

Reservation.java

___________________________________________

/** * This class represents a Reservation. */ public class Reservation implements Comparable {

// Instance variables private int reservationNum; private int flightNum; private String firstName; private String lastName; private int seatNum;

/** * Parameterized constructor * * @param reservationNum * - reservation number * @param flightNum * - flight number * @param lastName * - last name of passengeer * @param firstName * - first name of passengeer * @param seatNum * - seat number of passengeer */ public Reservation(int reservationNum, int flightNum, String lastName, String firstName, int seatNum) { this.reservationNum = reservationNum; this.flightNum = flightNum; this.firstName = firstName; this.lastName = lastName; this.seatNum = seatNum; }

/** * Returns the flightNum */ public int getFlightNum() { return flightNum; }

/** * Returns the firstName */ public String getFirstName() { return firstName; }

/** * Returns the lastName */ public String getLastName() { return lastName; }

/** * Returns the reservationNum */ public int getReservationNum() { return reservationNum; }

/** * Returns the seatNum */ public int getSeatNum() { return seatNum; }

/** * Sets the flight number * * @param flightNum * the flightNum to set */ public void setFlightNum(int flightNum) { this.flightNum = flightNum; }

/** * Sets the first name of the passenger * * @param firstName * the firstName to set */ public void setFirstName(String firstName) { this.firstName = firstName; }

/** * Sets the last name of the passenger * * @param lastName * the lastName to set */ public void setLastName(String lastName) { this.lastName = lastName; }

/** * Sets the seat number of the passenger * * @param seatNum * the seatNum to set */ public void setSeatNum(int seatNum) { this.seatNum = seatNum; }

/** * Sets the reservation number * * @param reservationNum * the reservationNum to set */ public void setReservationNum(int reservationNum) { this.reservationNum = reservationNum; }

/** * Compares the seat number of this reservation with another */ @Override public int compareTo(Reservation r2) { return (this.seatNum - r2.seatNum); }

/** * Takes a String parameter and returns a Reservation object populated from * the data string */ public static Reservation parseReservationInformation(String info) throws IllegalArgumentException, NumberFormatException { // Split reservation string String[] data = info.split(",");

// Check if there are sufficient argments if (data.length == 4) { // Create and return the object return new Reservation(Integer.parseInt(data[0] + data[3]), Integer.parseInt(data[0]), data[1], data[2], Integer.parseInt(data[3])); } else throw new IllegalArgumentException("Insufficient data: " + info); }

/** * Returns the reservation */ @Override public String toString() { return "Flight " + flightNum + " (" + seatNum + ") " + lastName + ", " + firstName; } }

_____________________________________

Lab3.java

____________________________________

import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner;

public class Lab3 {

private static final String FLIGHT_INFO = "flight-schedule.txt"; private static final String RESERVATIONS = "reservations.txt";

public static void main(String[] args) { // Create ArrayList to hold flight info ArrayList flights = new ArrayList();

// Create scanner to read from the file Scanner file = null;

// Read flight info from file try { file = new Scanner(new File(FLIGHT_INFO));

// Read file while (file.hasNext()) { // Read a line String data = file.nextLine().trim();

// Check if data is not empty if (!data.isEmpty()) {

try { // Get flight object Flight flight = Flight.parseFlightInformation(data);

// Add flight to the list flights.add(flight); } catch (Exception e) { System.out.println(e.getMessage()); } } } } catch (FileNotFoundException fnfe) { System.out.println("File not found: " + FLIGHT_INFO); System.exit(1); } finally { // Close file if (file != null) file.close(); }

// Read reservation info from file try { file = new Scanner(new File(RESERVATIONS));

// Read file while (file.hasNext()) { // Read a line String data = file.nextLine().trim();

// Check if data is not empty if (!data.isEmpty()) { try { // Get flight object Reservation res = Reservation.parseReservationInformation(data);

// Get flight number from reservation int flightNum = res.getFlightNum();

// Check for flight number in flights list for (Flight f : flights) { if (f.getFlightNum() == flightNum) { // Add reservation to the flight f.addReservation(res); break; } } } catch (Exception e) { System.out.println(e.getMessage()); } } } } catch (FileNotFoundException fnfe) { System.out.println("File not found: " + RESERVATIONS); System.exit(1); } finally { // Close file if (file != null) file.close(); }

// Sort flight list Collections.sort(flights);

// Display flight information System.out.println(String.format("%91s", " ").replaceAll(" ", "-")); for (Flight f : flights) System.out.println(f); System.out.println(String.format("%91s", " ").replaceAll(" ", "-"));

// Display reservations for (Flight f : flights) { // Sort reservations Collections.sort((ArrayList)f.getReservations());

for (Reservation r : f.getReservations()) System.out.println(r); } } }

Objectives: Polymorphism ArrayList Strings, Comparable UML diagrams Task: Create a basic flight reservation program The local airport would like to display flight information for all flights that occur each day. Flight information includes the data about each flight (its destination, origin, arrival time, etc) as well as the reservations made for that flight. They keep this information in two [very disorganized] text files-your program will sort out the data and display it to the user Getting Started Your application will consist of 5 Java classes: Lab3.java . Flight.java CommercialFlight.java PrivateFlight.java Reservation.java It will read in data from two text files: fight-schedule.txt and reservations.txt. You will place these files at the top of your project in Eclipse that is, not within the source folder or any other folder. If this is done correctly, within your code, you will need only refer to the file name, rather than giving a full path to the location of each file Lab3.java This class will run your program (Hint: it must have a main method). The main method will have an ArrayList of flight- it will populate the data for these flights and their reservations from the text file (see below for additional details). When run, your program will read in the data from the given text files, and print exactly as follows

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 Databases Questions!