Advertisement
martinkotevski

NP - Aerodromi

Dec 28th, 2016
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.61 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class AirportsTest {
  4.   public static void main(String[] args) {
  5.     Scanner scanner = new Scanner(System.in);
  6.     Airports airports = new Airports();
  7.     int n = scanner.nextInt();
  8.     scanner.nextLine();
  9.     String[] codes = new String[n];
  10.     for (int i = 0; i < n; ++i) {
  11.       String al = scanner.nextLine();
  12.       String[] parts = al.split(";");
  13.       airports.addAirport(parts[0], parts[1], parts[2], Integer.parseInt(parts[3]));
  14.       codes[i] = parts[2];
  15.     }
  16.     int nn = scanner.nextInt();
  17.     scanner.nextLine();
  18.     for (int i = 0; i < nn; ++i) {
  19.       String fl = scanner.nextLine();
  20.       String[] parts = fl.split(";");
  21.       airports.addFlights(parts[0], parts[1], Integer.parseInt(parts[2]), Integer.parseInt(parts[3]));
  22.     }
  23.     int f = scanner.nextInt();
  24.     int t = scanner.nextInt();
  25.     String from = codes[f];
  26.     String to = codes[t];
  27.     System.out.printf("===== FLIGHTS FROM %S =====\n", from);
  28.     airports.showFlightsFromAirport(from);
  29.     System.out.printf("===== DIRECT FLIGHTS FROM %S TO %S =====\n", from, to);
  30.     airports.showDirectFlightsFromTo(from, to);
  31.     t += 5;
  32.     t = t % n;
  33.     to = codes[t];
  34.     System.out.printf("===== DIRECT FLIGHTS TO %S =====\n", to);
  35.     airports.showDirectFlightsTo(to);
  36.   }
  37. }
  38.  
  39. // vashiot kod ovde
  40. class Airports {
  41.     private Map<String, Airport> airports;
  42.  
  43.  
  44.     public Airports() {
  45.         this.airports = new HashMap<>();
  46.     }
  47.  
  48.     public void addAirport(String name, String country, String code, int passengersYearly) {
  49.         Airport a = new Airport(name, country, code, passengersYearly);
  50.         airports.put(a.code, a);
  51.     }
  52.  
  53.     public void addFlights(String from, String to, int time, int duration) {
  54.         Flight f = new Flight(from, to, time, duration);
  55.         airports.get(from).addDepartingFlight(f);
  56.         airports.get(to).addArrivingFlight(f);
  57.     }
  58.  
  59.     public void showDirectFlightsFromTo(String from, String to) {
  60.         Airport a = this.airports.get(from);
  61.         a.showDirectFlightsTo(to);
  62.     }
  63.  
  64.     public void showDirectFlightsTo(String to) {
  65.         this.airports.get(to).showAllArrivingFlights();
  66.     }
  67.  
  68.     public void showFlightsFromAirport(String code) {
  69.         Airport a = this.airports.get(code);
  70.         a.showAllDepartingFlights();
  71.     }
  72. }
  73.  
  74. class Airport {
  75.     String name;
  76.     String country;
  77.     String code;
  78.     int passengersYearly;
  79.     Map<String, TreeSet<Flight>> departingFlights;
  80.     TreeSet<Flight> arrivingFlights;
  81.  
  82.     public Airport(String name, String country, String code, int passengersYearly) {
  83.         this.name = name;
  84.         this.country = country;
  85.         this.code = code;
  86.         this.passengersYearly = passengersYearly;
  87.         departingFlights = new TreeMap<>();
  88.         arrivingFlights = new TreeSet<>(Comparator.comparing(Flight::getTime).thenComparing(Flight::getFrom));
  89.     }
  90.  
  91.     public void showDirectFlightsTo(String code) {
  92.         TreeSet<Flight> res = departingFlights.get(code);
  93.         if (res == null)
  94.             System.out.println("No flights from " + this.code +  " to " + code);
  95.         else
  96.             res.forEach(System.out::println);
  97.     }
  98.  
  99.     public void showAllArrivingFlights() {
  100.         this.arrivingFlights.forEach(System.out::println);
  101.     }
  102.  
  103.     public void showAllDepartingFlights() {
  104.         List<Flight> ls = new ArrayList<>();
  105.         System.out.println(this.name + " (" + this.code + ")");
  106.         System.out.println(this.country);
  107.         System.out.println(this.passengersYearly);
  108.  
  109.         for (Map.Entry<String, TreeSet<Flight>> entry : this.departingFlights.entrySet()) {
  110.             entry.getValue().forEach(each -> ls.add(each));
  111.         }
  112.         int index = 1;
  113.         for (Flight f : ls)
  114.             System.out.println(index++ + ". " + f);
  115.     }
  116.  
  117.     public void addDepartingFlight(Flight flight) {
  118.         this.departingFlights.computeIfPresent(flight.to, (key, set) -> {
  119.             set.add(flight);
  120.             return set;
  121.         });
  122.         TreeSet<Flight> s = this.departingFlights.computeIfAbsent(flight.to,
  123.                 key -> new TreeSet<Flight>(Comparator.comparing(Flight::getTime)));
  124.         s.add(flight);
  125.     }
  126.  
  127.     public void addArrivingFlight(Flight flight) {
  128.         this.arrivingFlights.add(flight);
  129.     }
  130.  
  131. }
  132.  
  133. class Flight {
  134.     String from;
  135.     String to;
  136.     int time;
  137.     int duration;
  138.  
  139.     public Flight(String from, String to, int time, int duration) {
  140.         this.from = from;
  141.         this.to = to;
  142.         this.time = time;
  143.         this.duration = duration;
  144.     }
  145.  
  146.     public int getTime() {
  147.         return time;
  148.     }
  149.    
  150.     public String getFrom() {
  151.         return from;
  152.     }
  153.  
  154.     @Override
  155.     public String toString() {
  156.         String H;
  157.         int h = duration / 60;
  158.         String M;
  159.         int m = duration % 60;
  160.         if (m < 10) M = "0" + m;
  161.         else M = "" + m;
  162.         String dur = "" + h + "h" + M + "m";
  163.         String startTime = convert(this.time);
  164.         String endTime = convert(this.time + this.duration);
  165.         int a = Integer.parseInt(startTime.substring(0, 2));
  166.         int b = Integer.parseInt(endTime.substring(0, 2));
  167.         if (a > b)
  168.             dur = "+1d " + dur;
  169.         return String.format("%s-%s %s-%s %s", from, to, startTime, endTime, dur);
  170.     }
  171.  
  172.     private String convert(int min) {
  173.         int h = (min / 60) % 24;
  174.         String H;
  175.         if (h < 10) H = "0" + h;
  176.         else H = "" + h;
  177.         //if (h == 24) H = "00";
  178.  
  179.         int m = min % 60;
  180.         String M;
  181.         if (m < 10) M = "0" + m;
  182.         else M = "" + m;
  183.         return H + ":" + M;
  184.     }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement