add1ctus

Аеродроми

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