masterm1nd99

Untitled

Jun 13th, 2020
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. class Airport
  5. {
  6. String name;
  7. String country;
  8. String code;
  9. int passengers;
  10.  
  11. public Airport(String name, String country, String code, int passengers) {
  12. this.name = name;
  13. this.country = country;
  14. this.code = code;
  15. this.passengers = passengers;
  16. }
  17.  
  18. public String getName() {
  19. return name;
  20. }
  21.  
  22. public String getCountry() {
  23. return country;
  24. }
  25.  
  26. @Override
  27. public int hashCode() {
  28. return Objects.hash(name, country, code, passengers);
  29. }
  30.  
  31. public String getCode() {
  32. return code;
  33. }
  34.  
  35. public int getPassengers() {
  36. return passengers;
  37. }
  38.  
  39. @Override
  40. public boolean equals(Object o) {
  41. if (this == o) return true;
  42. if (o == null || getClass() != o.getClass()) return false;
  43. Airport airport = (Airport) o;
  44. return passengers == airport.passengers &&
  45. Objects.equals(name, airport.name) &&
  46. Objects.equals(country, airport.country) &&
  47. Objects.equals(code, airport.code);
  48. }
  49.  
  50. @Override
  51. public String toString() {
  52. return this.name + " " + this.code;
  53. }
  54. }
  55. class Flight implements Comparable<Flight>
  56. {
  57. String from;
  58. String to;
  59. int time;
  60. int duration;
  61.  
  62. public Flight(String from, String to, int time, int duration) {
  63. this.from = from;
  64. this.to = to;
  65. this.time = time;
  66. this.duration = duration;
  67. }
  68.  
  69. public String getFrom() {
  70. return from;
  71. }
  72.  
  73. public String getTo() {
  74. return to;
  75. }
  76.  
  77. public int getTime() {
  78. return time;
  79. }
  80.  
  81. public int getDuration() {
  82. return duration;
  83. }
  84.  
  85. @Override
  86. public int compareTo(Flight o) {
  87. if(this.to.compareTo(o.to) > 0)
  88. return 1;
  89. else return 0;
  90. }
  91. public String getSahat() {
  92. boolean flag = false;
  93. int Ahour = this.time / 60;
  94. int Aminutes = this.time % 60;
  95. int Bhour = (this.time + duration) / 60;
  96. int BMinutes = (this.time + duration) % 60;
  97. int day=0;
  98. if (Bhour > 23) {
  99. day = (Bhour / 24);
  100. Bhour -= day * 24;
  101. flag = true;
  102. }
  103. int durationH = this.duration/60;
  104. int durationM = this.duration%60;
  105. if (flag == false) {
  106. return String.format("%02d:%02d-%02d:%02d %dh%dm",Ahour,Aminutes,Bhour,BMinutes,
  107. durationH,durationM);
  108. }
  109. else
  110. {
  111. return String.format("%02d:%02d-%02d:%02d +%dd %dh%dm",Ahour,Aminutes,Bhour,BMinutes,day
  112. ,durationH,durationM);
  113. }
  114. }
  115.  
  116. @Override
  117. public boolean equals(Object o) {
  118. if (this == o) return true;
  119. if (o == null || getClass() != o.getClass()) return false;
  120. Flight flight = (Flight) o;
  121. return time == flight.time &&
  122. duration == flight.duration &&
  123. Objects.equals(from, flight.from) &&
  124. Objects.equals(to, flight.to);
  125. }
  126.  
  127. @Override
  128. public int hashCode() {
  129. return Objects.hash(from, to, time, duration);
  130. }
  131.  
  132. @Override
  133. public String toString()
  134. {
  135. return String.format("%s-%s %s",this.from,this.to,this.getSahat());
  136. }
  137. }
  138. class Airports
  139. {
  140. Map<Airport,TreeSet<Flight>> mapa;
  141. public Airports()
  142. {
  143. mapa = new HashMap<Airport,TreeSet<Flight>>();
  144. }
  145. public void addAirport(String name, String country, String code, int passengers)
  146. {
  147. Airport a = new Airport(name,country,code,passengers);
  148. boolean flag = true;
  149. if(mapa.containsKey(a))
  150. {
  151. flag = false;
  152. }
  153. if(flag==true)
  154. mapa.computeIfAbsent(a, i-> new TreeSet<Flight>());
  155.  
  156. }
  157. public void addFlights(String from, String to, int time, int duration)
  158. {
  159. Flight f = new Flight(from,to,time,duration);
  160. Airport air = mapa.keySet().stream()
  161. .filter(i->i.code.equals(from)).findAny().orElse(null);
  162. if(air != null)
  163. mapa.get(air).add(f);
  164. }
  165. public void showFlightsFromAirport(String code)
  166. {
  167. Airport air = mapa.keySet().stream().filter(i -> i.code.equals(code)).findAny().orElse(null);
  168. if (air !=null) {
  169. System.out.println(air.name + " (" + air.code + ")\n" + air.country + "\n" + air.passengers);
  170. mapa.get(air).stream().forEach(i -> System.out.println(i.toString()));
  171. }
  172.  
  173.  
  174. }
  175. public void showDirectFlightsFromTo(String from, String to)
  176. {
  177. Airport air = mapa.keySet().stream()
  178. .filter(i->i.code.equals(from)).findFirst().get();
  179. mapa.get(air).stream().filter(i-> i.to.equals(to))
  180. .forEach(i->System.out.println(i.toString()));
  181. }
  182. public void showDirectFlightsTo(String to)
  183. {
  184. mapa.values().stream().flatMap(i ->i.stream()).filter(i-> i.to.equals(to))
  185. .forEach(i-> System.out.println(i.toString()));
  186. }
  187. }
  188. public class AirportsTest {
  189. public static void main(String[] args) {
  190. Scanner scanner = new Scanner(System.in);
  191. Airports airports = new Airports();
  192. int n = scanner.nextInt();
  193. scanner.nextLine();
  194. String[] codes = new String[n];
  195. for (int i = 0; i < n; ++i) {
  196. String al = scanner.nextLine();
  197. String[] parts = al.split(";");
  198. airports.addAirport(parts[0], parts[1], parts[2], Integer.parseInt(parts[3]));
  199. codes[i] = parts[2];
  200. }
  201. int nn = scanner.nextInt();
  202. scanner.nextLine();
  203. for (int i = 0; i < nn; ++i) {
  204. String fl = scanner.nextLine();
  205. String[] parts = fl.split(";");
  206. airports.addFlights(parts[0], parts[1], Integer.parseInt(parts[2]), Integer.parseInt(parts[3]));
  207. }
  208. int f = scanner.nextInt();
  209. int t = scanner.nextInt();
  210. String from = codes[f];
  211. String to = codes[t];
  212. System.out.printf("===== FLIGHTS FROM %S =====\n", from);
  213. airports.showFlightsFromAirport(from);
  214. System.out.printf("===== DIRECT FLIGHTS FROM %S TO %S =====\n", from, to);
  215. airports.showDirectFlightsFromTo(from, to);
  216. t += 5;
  217. t = t % n;
  218. to = codes[t];
  219. System.out.printf("===== DIRECT FLIGHTS TO %S =====\n", to);
  220. airports.showDirectFlightsTo(to);
  221. }
  222. }
  223.  
  224. // vashiot kod ovde
Add Comment
Please, Sign In to add comment