Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.16 KB | None | 0 0
  1. package FMS.app;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.*;
  7.  
  8. import FMS.entities.*;
  9. import FMS.provided.*;
  10. import FMS.util.*;
  11.  
  12.  
  13. public class Main {
  14.  
  15. static List<Flight> flights;
  16.  
  17.  
  18. /**
  19. * Demo application.
  20. *
  21. * <ul>
  22. * <li>creates demo flights using {@link init}</li>
  23. * <li>prints them using {@link print}</li>
  24. * <li>sorts them by flight id</li>
  25. * <li>prints them again</li>
  26. * <li>filters flights, keeping those originating from Vienna (VIE)</li>
  27. * <li>sorts the filtered flights from Vienna by departure date and time</li>
  28. * <li>prints them</li>
  29. * <li>exports the sorted flights departing from Vienna to file
  30. * departures_VIE.txt and displays the number of flights exported
  31. * </ul>
  32. *
  33. * @param args
  34. * command line arguments (not used)
  35. */
  36. public static void main(String[] args) {
  37.  
  38. flights = init();
  39. print(flights);
  40. Collections.sort(flights);
  41. print(flights);
  42. List<Flight> second = filter(flights, new OriginMatcher("VIE"));
  43. Collections.sort(second, new DepartureComparator());
  44. print(second);
  45. System.out.printf("%d",export(flights, "filenamex"));
  46.  
  47.  
  48.  
  49. }
  50.  
  51. /**
  52. * Prints all flights.
  53. *
  54. * @param flights
  55. * the flights to print
  56. * @ProgrammingProblem.Hint provided
  57. */
  58.  
  59. private static <T> java.util.List<T> filter(java.util.Collection<T> list, Matcher<T> mtch){
  60. List<T> filter = new LinkedList<T>();
  61. for(T t:list) {
  62. if(mtch.match(t))
  63. filter.add(t);
  64.  
  65. }
  66. return filter;
  67.  
  68. }
  69.  
  70. private static int export(java.util.List<Flight> flights, String filename) {
  71. int counter=0;
  72. try {
  73. BufferedWriter bw = new BufferedWriter(new FileWriter(filename+".txt"));
  74. for(Flight q:flights) {
  75. bw.write(q.toString());
  76. counter++;
  77. }
  78. bw.close();
  79. }
  80. catch(IOException e) {
  81. System.out.print("Export failed");
  82. }
  83. return counter;
  84. }
  85.  
  86. private static void print(List<Flight> flights) {
  87. System.out.println("--- Listing Flights");
  88. for (Flight f : flights)
  89. System.out.println(f + "\n");
  90. System.out.printf("--- %d flights listed\n\n", flights.size());
  91.  
  92. }
  93.  
  94. /**
  95. * creates some demo data (provided)
  96. *
  97. * @return the demo data.
  98. */
  99.  
  100.  
  101.  
  102. private static List<Flight> init() {
  103.  
  104. // ---------------- aircrafts
  105. List<Aircraft> crafts = new LinkedList<>();
  106. crafts.add(new Aircraft("A3", 3));
  107. crafts.add(new Aircraft("C4", 4));
  108. crafts.add(new Aircraft("Boeing 737", 175));
  109.  
  110. // ---------------- passengers
  111. List<Passenger> passengers = new LinkedList<>();
  112. passengers.add(new Passenger("John", "Doe", "AT00004711"));
  113. passengers.add(new Passenger("Jane", "Doe", "AT00000815"));
  114. passengers.add(new Passenger("John", "Jackson", "US00004711"));
  115. passengers.add(new Passenger("Jack", "Doe", "UK00000007"));
  116. passengers.add(new Passenger("Jack", "Johnson", "DE00004711"));
  117.  
  118. // ---------------- staff
  119. List<Staff> staff = new LinkedList<>();
  120. staff.add(new Staff("Rip", "Riley", "ISIS666", "Pilot"));
  121. staff.add(new Staff("Cheryl", "Tunt", "ISIS456", "Chef de Cabin"));
  122. staff.add(new Staff("Lana", "Kane", "ISIS001", "Stewardess"));
  123.  
  124. // ---------------- flights
  125. List<Flight> flights = new LinkedList<>();
  126. flights.add(new ScheduledFlight("OS006", "FRA", "VIE", new DateTime(2018, 06, 25, 6, 30),
  127. new DateTime(2018, 06, 25, 8, 55), 800));
  128. flights.add(new Charter("OS001", "VIE", "VIE", new DateTime(2018, 06, 23, 6, 30),
  129. new DateTime(2018, 06, 23, 8, 55)));
  130. flights.add(new Charter("OS381", "ARN", "VIE", new DateTime(2018, 05, 25, 6, 30),
  131. new DateTime(2018, 05, 25, 9, 13)));
  132. flights.add(new Charter("OS001", "VIE", "JFK", new DateTime(2018, 07, 25, 4, 30),
  133. new DateTime(2018, 07, 25, 7, 20)));
  134. flights.add(new Charter("OS502", "JFK", "VIE", new DateTime(2018, 06, 28, 18, 30),
  135. new DateTime(2018, 06, 28, 19, 55)));
  136. flights.add(new ScheduledFlight("OS007", "VIE", "CDG", new DateTime(2018, 06, 28, 21, 00),
  137. new DateTime(2018, 06, 29, 0, 05), 1000));
  138. flights.add(new ScheduledFlight("OS008", "VIE", "CDG", new DateTime(2018, 06, 1, 6, 30),
  139. new DateTime(2018, 06, 1, 8, 05), 1000));
  140.  
  141.  
  142. Flight f;
  143.  
  144. // ---------------- set up flight
  145. f = flights.get(0).setVessel(crafts.get(0));
  146. f.add(staff.get(0));
  147. f.add(staff.get(1));
  148. f.add(staff.get(2));
  149. // list passengers
  150. f.add(passengers.get(0));
  151. f.add(passengers.get(1));
  152. f.add(passengers.get(2));
  153. f.add(passengers.get(3)); //exceeds vessel capacity
  154. // board passengers
  155. f.board(passengers.get(0)); // board listed passengers
  156. f.board(passengers.get(1));
  157. f.board(passengers.get(2));
  158. f.board(passengers.get(3)); // board a passenger not listed
  159.  
  160.  
  161. // ---------------- set up flight
  162. f = flights.get(1).setVessel(crafts.get(1));
  163. f.add(passengers.get(1));
  164. f.add(passengers.get(2));
  165. f.add(passengers.get(3));
  166. f.add(new Staff(passengers.get(0), "Pilot")); //a passenger becomes a pilot
  167. f.board(passengers.get(1)); // already on board another flight
  168. f.board(passengers.get(3)); // board a passenger
  169.  
  170.  
  171.  
  172. // ---------------- set up flight
  173. f = flights.get(2).setVessel(crafts.get(2));
  174. Passenger p = new Passenger(staff.get(0)); //staff becomes a passenger
  175. f.add(passengers.get(2));
  176. f.add(passengers.get(1));
  177. f.add(passengers.get(3));
  178. f.add(p);
  179. f.board(p); //no boarding without crew
  180.  
  181.  
  182. // ---------------- set up flight
  183. f = flights.get(3);
  184. f.add(passengers.get(2));
  185. f.add(passengers.get(1));
  186. f.add(passengers.get(3));
  187.  
  188.  
  189. // ---------------- set up flight
  190. f = flights.get(4);
  191. f.add(passengers.get(2));
  192. f.add(passengers.get(1));
  193. f.add(passengers.get(3));
  194.  
  195. // ---------------- set up flight
  196. f = flights.get(5);
  197. f.add(passengers.get(2));
  198. f.add(passengers.get(1));
  199. f.add(passengers.get(3));
  200.  
  201. return flights;
  202. }
  203.  
  204.  
  205.  
  206. }
  207.  
  208. package FMS.entities;
  209.  
  210. import java.util.Set;
  211. import java.util.TreeSet;
  212.  
  213. import FMS.provided.Aircraft;
  214. import FMS.provided.DateTime;
  215. import FMS.provided.Passenger;
  216. import FMS.provided.Staff;
  217.  
  218. public abstract class Flight implements Comparable<Flight> {
  219.  
  220. private DateTime arrival;
  221. private java.util.Set<Staff> crew;
  222. private DateTime departure;
  223. private String destination;
  224. private String flightID;
  225. private String origin;
  226. private java.util.Set<Passenger> passengers;
  227. private Aircraft vessel;
  228.  
  229. public Flight(Flight f) {
  230. this.setFlightID(f.flightID);
  231. this.setArrival(new DateTime(arrival));
  232. this.setDeparture(new DateTime(departure));
  233. this.setDestination(f.destination);
  234. this.setOrigin(f.origin);
  235. }
  236.  
  237. public DateTime getArrival() {
  238. return arrival;
  239. }
  240.  
  241. public void setArrival(DateTime arrival) {
  242. this.arrival = new DateTime(arrival);
  243. }
  244.  
  245. public java.util.Set<Staff> getCrew() {
  246. return crew;
  247. }
  248.  
  249. public void setCrew(java.util.Set<Staff> crew) {
  250. this.crew = crew;
  251. }
  252.  
  253. public DateTime getDeparture() {
  254. return new DateTime(this.departure);
  255. }
  256.  
  257. public void setDeparture(DateTime departure) {
  258. this.departure = new DateTime(departure);
  259. }
  260.  
  261. public String getDestination() {
  262. return destination;
  263. }
  264.  
  265. public void setDestination(String destination) throws IllegalArgumentException {
  266. if (destination != null && destination != "")
  267. this.destination = destination;
  268. else
  269. throw new IllegalArgumentException("please enter valid destination");
  270. }
  271.  
  272. public abstract int getBonusMiles();
  273.  
  274. public int compareTo(Flight o) {
  275. return this.getFlightID().compareTo(o.getFlightID());
  276. }
  277.  
  278. public boolean add(Staff Staff) {
  279. return crew.add(Staff);
  280. // crew.remove(Staff);
  281. }
  282. public boolean board(Passenger p) {
  283. if(this.readyToBoard()) {
  284. if(this.passengers.contains(p) && p.getBoarded()== null)
  285. return true;
  286. }
  287. return false;
  288. }
  289.  
  290.  
  291. public boolean add(Passenger passenger) {
  292. if(passenger == null) {
  293. return false;
  294. }
  295. if (this.vessel.getCapactiy()-this.passengers.size() > 0)
  296. return passengers.add(passenger);
  297.  
  298. return false;
  299.  
  300. }
  301. public boolean readyToBoard() {
  302. if(this.crew.size() > 0)
  303. return true;
  304. else
  305. return false;
  306. }
  307. public boolean boardingCompleted() {
  308.  
  309. if(this.passengers.size()>0) {
  310. boolean check = true;
  311. for(Passenger p:passengers) {
  312. if (p.getBoarded()!=this) {
  313. check = false;
  314. }
  315. }
  316. return check;
  317. }
  318. return false;
  319. }
  320.  
  321. public String getFlightID() {
  322. return flightID;
  323. }
  324.  
  325. public void setFlightID(String flightID) throws IllegalArgumentException {
  326. if (flightID != null && flightID != "") {
  327. this.flightID = flightID;
  328. } else {
  329. throw new IllegalArgumentException("Please enter valid flightID");
  330. }
  331. }
  332.  
  333. public String getOrigin() {
  334. return origin;
  335. }
  336.  
  337. public void setOrigin(String origin) throws IllegalArgumentException {
  338. if (origin != null && origin != "")
  339. this.origin = origin;
  340. else
  341. throw new IllegalArgumentException("please enter valid origin");
  342. }
  343.  
  344. public java.util.Set<Passenger> getPassengers() {
  345. return passengers;
  346. }
  347.  
  348. public void setPassengers(java.util.Set<Passenger> passengers) {
  349. this.passengers = passengers;
  350. }
  351.  
  352. public Aircraft getVessel() {
  353. return vessel;
  354. }
  355.  
  356. public void setVessel(Aircraft vessel) {
  357. this.vessel = vessel;
  358. }
  359.  
  360. public Flight(DateTime arrival, DateTime departure, String destination, String flightID, String origin) {
  361. super();
  362. this.arrival = arrival;
  363. this.departure = departure;
  364. this.destination = destination;
  365. this.flightID = flightID;
  366. this.origin = origin;
  367. this.passengers = passengers;
  368. }
  369.  
  370. private final String ensureNonNullNonEmpty(String str) throws IllegalArgumentException {
  371. if (str != null && str != "")
  372. return str;
  373. else {
  374. throw new IllegalArgumentException("Illegal Argument!");
  375. }
  376. }
  377.  
  378. @Override
  379. public String toString() {
  380. return String.format(
  381. "%5s from %3.3S (%s) to %3.3S (%s)" + " with a crew of %d and %d passengers "
  382. + "<%s> boarding%scompleted \n%s\n%s",
  383. flightID, origin, departure, destination, arrival, crew == null ? 0 : crew.size(),
  384. passengers == null ? 0 : passengers.size(), vessel == null ? "no vessel" : vessel.toString(),
  385. boardingCompleted() ? " " : " not ", crew, passengers);
  386. }
  387.  
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement