Advertisement
dzocesrce

[NP] Airports

Apr 25th, 2025
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.22 KB | None | 0 0
  1. import java.util.*;
  2. import java.time.LocalTime;
  3. import java.util.Comparator;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.concurrent.atomic.AtomicInteger;
  9. class Airport {
  10.  
  11.     String name;
  12.     String country;
  13.     String code;
  14.     int passengers;
  15.  
  16.     public Airport(String name, String country, String code, int passengers) {
  17.         this.name = name;
  18.         this.country = country;
  19.         this.code = code;
  20.         this.passengers = passengers;
  21.     }
  22.  
  23.     public String getName() {
  24.         return name;
  25.     }
  26.  
  27.     public String getCountry() {
  28.         return country;
  29.     }
  30.  
  31.     public String getCode() {
  32.         return code;
  33.     }
  34.  
  35.     public int getPassengers() {
  36.         return passengers;
  37.     }
  38.  
  39.     @Override
  40.     public String toString() {
  41.         return "Airport{" +
  42.                 "name='" + name + '\'' +
  43.                 ", country='" + country + '\'' +
  44.                 ", code='" + code + '\'' +
  45.                 ", passengers=" + passengers +
  46.                 '}';
  47.     }
  48. }
  49. class AirportGroup {
  50.  
  51.     Map<String,Airport> airports;
  52.     Map<String,List<Flight>> flights;
  53.     public AirportGroup() {
  54.         airports= new HashMap<>();
  55.         flights= new HashMap<>();
  56.     }
  57.  
  58.     public void addAirport(String name, String country, String code, int passengers) {
  59.         airports.putIfAbsent(code,new Airport(name,country,code,passengers));
  60.         flights.putIfAbsent(code,new ArrayList<>());
  61.     }
  62.  
  63.     public void addFlights(String from, String to, int time, int duration) {
  64.         flights.get(from).add(new Flight(from,to,time,duration));
  65.  
  66.     }
  67.  
  68.     public void showFlightsFromAirport(String from) {
  69.         AtomicInteger atomicInteger = new AtomicInteger(1);
  70.         System.out.println(String.format("%s (%s)\n%s\n%d", airports.get(from).getName(),
  71.                 airports.get(from).getCode(),
  72.                 airports.get(from).getCountry(),
  73.                 airports.get(from).getPassengers()));
  74.         flights.get(from).stream().sorted().forEach(i-> System.out.println(String.format("%d. %s",atomicInteger.getAndIncrement(),
  75.                 i)));
  76.     }
  77.  
  78.     public void showDirectFlightsFromTo(String from, String to) {
  79.         if(flights.get(from).stream().filter(i->i.getTo().equals(to)).count()==0){
  80.             System.out.println(String.format("No flights from %s to %s",from,to));
  81.             return ;
  82.         }
  83.         flights.get(from).stream().sorted().filter(i->i.getTo().equals(to)).forEach(i-> System.out.println(String.format("%s", i)));
  84.     }
  85.  
  86.     public void showDirectFlightsTo(String to) {
  87.         flights.values().stream().flatMap(list->list.stream()).filter(i->i.getTo().equals(to)).sorted().forEach(i-> System.out.println(String.format("%s", i)));
  88.     }
  89. }
  90. class Flight implements Comparable<Flight> {
  91.     String from;
  92.     String to;
  93.     int time;
  94.     int duration;
  95.  
  96.     public Flight(String from, String to, int time, int duration) {
  97.         this.from = from;
  98.         this.to = to;
  99.         this.time = time;
  100.         this.duration = duration;
  101.     }
  102.  
  103.     public String getFrom() {
  104.         return from;
  105.     }
  106.  
  107.     public String getTo() {
  108.         return to;
  109.     }
  110.  
  111.     public int getTime() {
  112.         return time;
  113.     }
  114.  
  115.     public int getDuration() {
  116.         return duration;
  117.     }
  118.  
  119.     public String getActualTime(){
  120.         LocalTime localTime = LocalTime.ofSecondOfDay(duration);
  121.         String cutTime;
  122.         cutTime = localTime.toString().substring(3).replace(":","h").concat("m");
  123.         if(duration%60==0){
  124.             cutTime=cutTime.replace("m","h00m");
  125.         }
  126.         if(cutTime.startsWith("0"))
  127.             return cutTime.substring(1);
  128.         return cutTime;
  129.  
  130.     }
  131.  
  132.     public String getDepartureAndArrivalTime(){
  133.         LocalTime departureTime = LocalTime.ofSecondOfDay(time);
  134.         LocalTime arrivalTime = LocalTime.ofSecondOfDay(time+duration);
  135.         String deprartureTimeCut = departureTime.toString().substring(3);
  136.         String arrivalTimeCut = arrivalTime.toString().substring(3);
  137.         if(time%60==0){
  138.             deprartureTimeCut=deprartureTimeCut.concat(":00");
  139.         }
  140.         if((time+duration)%60==0){
  141.             arrivalTimeCut=arrivalTimeCut.concat(":00");
  142.         }
  143.         if(Integer.parseInt(arrivalTime.toString().substring(3,5))>23){
  144.             arrivalTime = LocalTime.ofSecondOfDay(time+duration-24*60);
  145.             arrivalTimeCut = arrivalTime.toString().substring(3)+" +1d";
  146.         }
  147.         return String.format("%s-%s",deprartureTimeCut,arrivalTimeCut);
  148.  
  149.     }
  150.  
  151.     @Override
  152.     public String toString() {
  153.         return String.format("%s-%s %s %s",from,to,getDepartureAndArrivalTime(),getActualTime());
  154.     }
  155.  
  156.     @Override
  157.     public int compareTo(Flight o) {
  158.         return Comparator.comparing(Flight::getTo)
  159.                 .thenComparing(Flight::getTime)
  160.                 .thenComparing(Flight::getFrom)
  161.                 .compare(this,o);
  162.     }
  163. }
  164. public class AirportsTest {
  165.   public static void main(String[] args) {
  166.     Scanner scanner = new Scanner(System.in);
  167.     AirportGroup airports = new AirportGroup();
  168.     int n = scanner.nextInt();
  169.     scanner.nextLine();
  170.     String[] codes = new String[n];
  171.     for (int i = 0; i < n; ++i) {
  172.       String al = scanner.nextLine();
  173.       String[] parts = al.split(";");
  174.       airports.addAirport(parts[0], parts[1], parts[2], Integer.parseInt(parts[3]));
  175.       codes[i] = parts[2];
  176.     }
  177.     int nn = scanner.nextInt();
  178.     scanner.nextLine();
  179.     for (int i = 0; i < nn; ++i) {
  180.       String fl = scanner.nextLine();
  181.       String[] parts = fl.split(";");
  182.       airports.addFlights(parts[0], parts[1], Integer.parseInt(parts[2]), Integer.parseInt(parts[3]));
  183.     }
  184.     int f = scanner.nextInt();
  185.     int t = scanner.nextInt();
  186.     String from = codes[f];
  187.     String to = codes[t];
  188.     System.out.printf("===== FLIGHTS FROM %S =====\n", from);
  189.     airports.showFlightsFromAirport(from);
  190.     System.out.printf("===== DIRECT FLIGHTS FROM %S TO %S =====\n", from, to);
  191.     airports.showDirectFlightsFromTo(from, to);
  192.     t += 5;
  193.     t = t % n;
  194.     to = codes[t];
  195.     System.out.printf("===== DIRECT FLIGHTS TO %S =====\n", to);
  196.     airports.showDirectFlightsTo(to);
  197.   }
  198. }
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement