Advertisement
Martina312

Untitled

Aug 23rd, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. import java.time.LocalTime;
  2. import java.util.Comparator;
  3. import java.util.HashMap;
  4. import java.util.Scanner;
  5. import java.util.TreeSet;
  6.  
  7. class Flight {
  8. private String from;
  9. private String to;
  10. private int time;
  11. private int duration;
  12.  
  13. public Flight(String from, String to, int time, int duration) {
  14. this.from = from;
  15. this.to = to;
  16. this.time = time;
  17. this.duration = duration;
  18. }
  19.  
  20. public String getDestinationCode() {
  21. return to;
  22. }
  23.  
  24. public int getTime() {
  25. return time;
  26. }
  27.  
  28. public String getFrom() {
  29. return from;
  30. }
  31.  
  32. @Override
  33. public String toString() {
  34. StringBuilder sb = new StringBuilder();
  35. LocalTime departure = LocalTime.of(0, 0).plusMinutes(time);
  36. LocalTime arrival = departure.plusMinutes(duration);
  37.  
  38. sb.append(from).append("-").append(to)
  39. .append(" ").append(departure)
  40. .append("-").append(arrival)
  41. .append(" ").append(durationInDays());
  42.  
  43. return sb.toString();
  44.  
  45. }
  46.  
  47. private String durationInDays() {
  48. if (time + duration > 24 * 60) {
  49. int days = (time + duration) / (24*60);
  50. int tmp = duration % (24*60);
  51. int hours = tmp / 60;
  52. int minutes = tmp % 60;
  53. return String.format("%+dd %dh%02dm", days, hours, minutes);
  54. } else {
  55. int hours = duration / 60;
  56. int minutes = duration % 60;
  57. return String.format("%dh%02dm", hours, minutes);
  58. }
  59. }
  60. }
  61.  
  62. class Airport {
  63. private String name;
  64. private String country;
  65. private String code;
  66. private int passengers;
  67. private TreeSet<Flight> flightsFrom;
  68. private TreeSet<Flight> flightsTo;
  69.  
  70.  
  71. public Airport(String name, String country, String code, int passengers) {
  72. this.name = name;
  73. this.country = country;
  74. this.code = code;
  75. this.passengers = passengers;
  76. flightsFrom = new TreeSet<>(Comparator.comparing(Flight::getDestinationCode).thenComparing(Flight::getTime));
  77. flightsTo = new TreeSet<>(Comparator.comparing(Flight::getTime).thenComparing(Flight::getFrom));
  78. }
  79.  
  80. public void addFlightFrom(Flight f) {
  81. flightsFrom.add(f);
  82. }
  83.  
  84. public void addFlightTo(Flight f) {
  85. flightsTo.add(f);
  86. }
  87.  
  88. @Override
  89. public String toString() {
  90. StringBuilder sb = new StringBuilder();
  91.  
  92. sb.append(name).append(" (").append(code).append(")\n");
  93. sb.append(country).append("\n");
  94. sb.append(passengers);
  95.  
  96. int i = 1;
  97. for (Flight f : flightsFrom) {
  98. sb.append("\n").append(i).append(". ").append(f.toString());
  99. i++;
  100. }
  101.  
  102. return sb.toString();
  103. }
  104.  
  105. public void showDirectFlightsTo(String to) {
  106. boolean noFlights = flightsFrom
  107. .stream()
  108. .noneMatch(f -> f.getDestinationCode().equals(to));
  109.  
  110. if (noFlights)
  111. System.out.println("No flights from " + code + " to " + to);
  112.  
  113. flightsFrom
  114. .stream()
  115. .filter(f -> f.getDestinationCode().equals(to))
  116. .forEach(System.out::println);
  117. }
  118.  
  119. public void showIncomingDirectFlights() {
  120. flightsTo.forEach(System.out::println);
  121. }
  122. }
  123.  
  124. class Airports {
  125. private HashMap<String, Airport> airports;
  126.  
  127. public Airports() {
  128. this.airports = new HashMap<>();
  129. }
  130.  
  131. public void addAirport(String name, String country, String code, int passengers) {
  132. Airport a = new Airport(name, country, code, passengers);
  133. airports.putIfAbsent(code, a);
  134. }
  135.  
  136. public void addFlights(String from, String to, int time, int duration) {
  137. Flight f = new Flight(from, to, time, duration);
  138. airports.get(from).addFlightFrom(f);
  139. airports.get(to).addFlightTo(f);
  140. }
  141.  
  142. public void showFlightsFromAirport(String code) {
  143. Airport a = airports.get(code);
  144. System.out.println(a);
  145. }
  146.  
  147. public void showDirectFlightsFromTo(String from, String to) {
  148. Airport a = airports.get(from);
  149. a.showDirectFlightsTo(to);
  150. }
  151.  
  152. public void showDirectFlightsTo(String to) {
  153. Airport a = airports.get(to);
  154. a.showIncomingDirectFlights();
  155. }
  156. }
  157.  
  158. public class AirportsTest {
  159. public static void main(String[] args) {
  160. Scanner scanner = new Scanner(System.in);
  161. Airports airports = new Airports();
  162. int n = scanner.nextInt();
  163. scanner.nextLine();
  164. String[] codes = new String[n];
  165. for (int i = 0; i < n; ++i) {
  166. String al = scanner.nextLine();
  167. String[] parts = al.split(";");
  168. airports.addAirport(parts[0], parts[1], parts[2], Integer.parseInt(parts[3]));
  169. codes[i] = parts[2];
  170. }
  171. int nn = scanner.nextInt();
  172. scanner.nextLine();
  173. for (int i = 0; i < nn; ++i) {
  174. String fl = scanner.nextLine();
  175. String[] parts = fl.split(";");
  176. airports.addFlights(parts[0], parts[1], Integer.parseInt(parts[2]), Integer.parseInt(parts[3]));
  177. }
  178. int f = scanner.nextInt();
  179. int t = scanner.nextInt();
  180. String from = codes[f];
  181. String to = codes[t];
  182. System.out.printf("===== FLIGHTS FROM %S =====\n", from);
  183. airports.showFlightsFromAirport(from);
  184. System.out.printf("===== DIRECT FLIGHTS FROM %S TO %S =====\n", from, to);
  185. airports.showDirectFlightsFromTo(from, to);
  186. t += 5;
  187. t = t % n;
  188. to = codes[t];
  189. System.out.printf("===== DIRECT FLIGHTS TO %S =====\n", to);
  190. airports.showDirectFlightsTo(to);
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement