Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FMS.app;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.*;
- import FMS.entities.*;
- import FMS.provided.*;
- import FMS.util.*;
- public class Main {
- static List<Flight> flights;
- /**
- * Demo application.
- *
- * <ul>
- * <li>creates demo flights using {@link init}</li>
- * <li>prints them using {@link print}</li>
- * <li>sorts them by flight id</li>
- * <li>prints them again</li>
- * <li>filters flights, keeping those originating from Vienna (VIE)</li>
- * <li>sorts the filtered flights from Vienna by departure date and time</li>
- * <li>prints them</li>
- * <li>exports the sorted flights departing from Vienna to file
- * departures_VIE.txt and displays the number of flights exported
- * </ul>
- *
- * @param args
- * command line arguments (not used)
- */
- public static void main(String[] args) {
- flights = init();
- print(flights);
- Collections.sort(flights);
- print(flights);
- List<Flight> second = filter(flights, new OriginMatcher("VIE"));
- Collections.sort(second, new DepartureComparator());
- print(second);
- System.out.printf("%d",export(flights, "filenamex"));
- }
- /**
- * Prints all flights.
- *
- * @param flights
- * the flights to print
- * @ProgrammingProblem.Hint provided
- */
- private static <T> java.util.List<T> filter(java.util.Collection<T> list, Matcher<T> mtch){
- List<T> filter = new LinkedList<T>();
- for(T t:list) {
- if(mtch.match(t))
- filter.add(t);
- }
- return filter;
- }
- private static int export(java.util.List<Flight> flights, String filename) {
- int counter=0;
- try {
- BufferedWriter bw = new BufferedWriter(new FileWriter(filename+".txt"));
- for(Flight q:flights) {
- bw.write(q.toString());
- counter++;
- }
- bw.close();
- }
- catch(IOException e) {
- System.out.print("Export failed");
- }
- return counter;
- }
- private static void print(List<Flight> flights) {
- System.out.println("--- Listing Flights");
- for (Flight f : flights)
- System.out.println(f + "\n");
- System.out.printf("--- %d flights listed\n\n", flights.size());
- }
- /**
- * creates some demo data (provided)
- *
- * @return the demo data.
- */
- private static List<Flight> init() {
- // ---------------- aircrafts
- List<Aircraft> crafts = new LinkedList<>();
- crafts.add(new Aircraft("A3", 3));
- crafts.add(new Aircraft("C4", 4));
- crafts.add(new Aircraft("Boeing 737", 175));
- // ---------------- passengers
- List<Passenger> passengers = new LinkedList<>();
- passengers.add(new Passenger("John", "Doe", "AT00004711"));
- passengers.add(new Passenger("Jane", "Doe", "AT00000815"));
- passengers.add(new Passenger("John", "Jackson", "US00004711"));
- passengers.add(new Passenger("Jack", "Doe", "UK00000007"));
- passengers.add(new Passenger("Jack", "Johnson", "DE00004711"));
- // ---------------- staff
- List<Staff> staff = new LinkedList<>();
- staff.add(new Staff("Rip", "Riley", "ISIS666", "Pilot"));
- staff.add(new Staff("Cheryl", "Tunt", "ISIS456", "Chef de Cabin"));
- staff.add(new Staff("Lana", "Kane", "ISIS001", "Stewardess"));
- // ---------------- flights
- List<Flight> flights = new LinkedList<>();
- flights.add(new ScheduledFlight("OS006", "FRA", "VIE", new DateTime(2018, 06, 25, 6, 30),
- new DateTime(2018, 06, 25, 8, 55), 800));
- flights.add(new Charter("OS001", "VIE", "VIE", new DateTime(2018, 06, 23, 6, 30),
- new DateTime(2018, 06, 23, 8, 55)));
- flights.add(new Charter("OS381", "ARN", "VIE", new DateTime(2018, 05, 25, 6, 30),
- new DateTime(2018, 05, 25, 9, 13)));
- flights.add(new Charter("OS001", "VIE", "JFK", new DateTime(2018, 07, 25, 4, 30),
- new DateTime(2018, 07, 25, 7, 20)));
- flights.add(new Charter("OS502", "JFK", "VIE", new DateTime(2018, 06, 28, 18, 30),
- new DateTime(2018, 06, 28, 19, 55)));
- flights.add(new ScheduledFlight("OS007", "VIE", "CDG", new DateTime(2018, 06, 28, 21, 00),
- new DateTime(2018, 06, 29, 0, 05), 1000));
- flights.add(new ScheduledFlight("OS008", "VIE", "CDG", new DateTime(2018, 06, 1, 6, 30),
- new DateTime(2018, 06, 1, 8, 05), 1000));
- Flight f;
- // ---------------- set up flight
- f = flights.get(0).setVessel(crafts.get(0));
- f.add(staff.get(0));
- f.add(staff.get(1));
- f.add(staff.get(2));
- // list passengers
- f.add(passengers.get(0));
- f.add(passengers.get(1));
- f.add(passengers.get(2));
- f.add(passengers.get(3)); //exceeds vessel capacity
- // board passengers
- f.board(passengers.get(0)); // board listed passengers
- f.board(passengers.get(1));
- f.board(passengers.get(2));
- f.board(passengers.get(3)); // board a passenger not listed
- // ---------------- set up flight
- f = flights.get(1).setVessel(crafts.get(1));
- f.add(passengers.get(1));
- f.add(passengers.get(2));
- f.add(passengers.get(3));
- f.add(new Staff(passengers.get(0), "Pilot")); //a passenger becomes a pilot
- f.board(passengers.get(1)); // already on board another flight
- f.board(passengers.get(3)); // board a passenger
- // ---------------- set up flight
- f = flights.get(2).setVessel(crafts.get(2));
- Passenger p = new Passenger(staff.get(0)); //staff becomes a passenger
- f.add(passengers.get(2));
- f.add(passengers.get(1));
- f.add(passengers.get(3));
- f.add(p);
- f.board(p); //no boarding without crew
- // ---------------- set up flight
- f = flights.get(3);
- f.add(passengers.get(2));
- f.add(passengers.get(1));
- f.add(passengers.get(3));
- // ---------------- set up flight
- f = flights.get(4);
- f.add(passengers.get(2));
- f.add(passengers.get(1));
- f.add(passengers.get(3));
- // ---------------- set up flight
- f = flights.get(5);
- f.add(passengers.get(2));
- f.add(passengers.get(1));
- f.add(passengers.get(3));
- return flights;
- }
- }
- package FMS.entities;
- import java.util.Set;
- import java.util.TreeSet;
- import FMS.provided.Aircraft;
- import FMS.provided.DateTime;
- import FMS.provided.Passenger;
- import FMS.provided.Staff;
- public abstract class Flight implements Comparable<Flight> {
- private DateTime arrival;
- private java.util.Set<Staff> crew;
- private DateTime departure;
- private String destination;
- private String flightID;
- private String origin;
- private java.util.Set<Passenger> passengers;
- private Aircraft vessel;
- public Flight(Flight f) {
- this.setFlightID(f.flightID);
- this.setArrival(new DateTime(arrival));
- this.setDeparture(new DateTime(departure));
- this.setDestination(f.destination);
- this.setOrigin(f.origin);
- }
- public DateTime getArrival() {
- return arrival;
- }
- public void setArrival(DateTime arrival) {
- this.arrival = new DateTime(arrival);
- }
- public java.util.Set<Staff> getCrew() {
- return crew;
- }
- public void setCrew(java.util.Set<Staff> crew) {
- this.crew = crew;
- }
- public DateTime getDeparture() {
- return new DateTime(this.departure);
- }
- public void setDeparture(DateTime departure) {
- this.departure = new DateTime(departure);
- }
- public String getDestination() {
- return destination;
- }
- public void setDestination(String destination) throws IllegalArgumentException {
- if (destination != null && destination != "")
- this.destination = destination;
- else
- throw new IllegalArgumentException("please enter valid destination");
- }
- public abstract int getBonusMiles();
- public int compareTo(Flight o) {
- return this.getFlightID().compareTo(o.getFlightID());
- }
- public boolean add(Staff Staff) {
- return crew.add(Staff);
- // crew.remove(Staff);
- }
- public boolean board(Passenger p) {
- if(this.readyToBoard()) {
- if(this.passengers.contains(p) && p.getBoarded()== null)
- return true;
- }
- return false;
- }
- public boolean add(Passenger passenger) {
- if(passenger == null) {
- return false;
- }
- if (this.vessel.getCapactiy()-this.passengers.size() > 0)
- return passengers.add(passenger);
- return false;
- }
- public boolean readyToBoard() {
- if(this.crew.size() > 0)
- return true;
- else
- return false;
- }
- public boolean boardingCompleted() {
- if(this.passengers.size()>0) {
- boolean check = true;
- for(Passenger p:passengers) {
- if (p.getBoarded()!=this) {
- check = false;
- }
- }
- return check;
- }
- return false;
- }
- public String getFlightID() {
- return flightID;
- }
- public void setFlightID(String flightID) throws IllegalArgumentException {
- if (flightID != null && flightID != "") {
- this.flightID = flightID;
- } else {
- throw new IllegalArgumentException("Please enter valid flightID");
- }
- }
- public String getOrigin() {
- return origin;
- }
- public void setOrigin(String origin) throws IllegalArgumentException {
- if (origin != null && origin != "")
- this.origin = origin;
- else
- throw new IllegalArgumentException("please enter valid origin");
- }
- public java.util.Set<Passenger> getPassengers() {
- return passengers;
- }
- public void setPassengers(java.util.Set<Passenger> passengers) {
- this.passengers = passengers;
- }
- public Aircraft getVessel() {
- return vessel;
- }
- public void setVessel(Aircraft vessel) {
- this.vessel = vessel;
- }
- public Flight(DateTime arrival, DateTime departure, String destination, String flightID, String origin) {
- super();
- this.arrival = arrival;
- this.departure = departure;
- this.destination = destination;
- this.flightID = flightID;
- this.origin = origin;
- this.passengers = passengers;
- }
- private final String ensureNonNullNonEmpty(String str) throws IllegalArgumentException {
- if (str != null && str != "")
- return str;
- else {
- throw new IllegalArgumentException("Illegal Argument!");
- }
- }
- @Override
- public String toString() {
- return String.format(
- "%5s from %3.3S (%s) to %3.3S (%s)" + " with a crew of %d and %d passengers "
- + "<%s> boarding%scompleted \n%s\n%s",
- flightID, origin, departure, destination, arrival, crew == null ? 0 : crew.size(),
- passengers == null ? 0 : passengers.size(), vessel == null ? "no vessel" : vessel.toString(),
- boardingCompleted() ? " " : " not ", crew, passengers);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement