Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Airport implements Comparable<Airport>
- {
- private String name;
- private String country;
- private String code;
- private int passengers;
- public int compareTo(Airport rhs)
- {
- return name.compareTo(rhs.name);
- }
- public Airport(String n, String cou, String cod, int p)
- {
- name = n;
- country = cou;
- code = cod;
- passengers = p;
- }
- public String toString()
- {
- return name + " (" + code + ")\n" + country + "\n" + passengers;
- }
- }
- class Flight implements Comparable<Flight>
- {
- private String from;
- private String to;
- private int time;
- private int duration;
- public String getTo()
- {
- return to;
- }
- public int compareTo(Flight rhs)
- {
- if(to.compareTo(rhs.to) != 0)
- return to.compareTo(rhs.to);
- if(time != rhs.time)
- return time - rhs.time;
- return from.compareTo(rhs.from);
- }
- public Flight(String f, String t, int ti, int d)
- {
- from = f;
- to = t;
- time = ti;
- duration = d;
- }
- public String toString()
- {
- String result = from + "-" + to + " " + String.format("%02d:%02d-%02d:%02d",time/60,time%60,(time+duration)%1440/60,(time+duration)%60);
- if(time+duration > 1440)
- result += " +1d ";
- else
- result += " ";
- result += String.format("%dh%02dm",duration/60,duration%60);
- return result;
- }
- }
- class Airports
- {
- private HashMap<String, Airport> airports;
- private HashMap<String, TreeSet<Flight>> flightsFrom;
- private HashMap<String, TreeSet<Flight>> flightsTo;
- public Airports()
- {
- airports = new HashMap<String, Airport>();
- flightsFrom = new HashMap<String, TreeSet<Flight>>();
- flightsTo = new HashMap<String, TreeSet<Flight>>();
- }
- public void addAirport(String name, String country, String code, int passengers)
- {
- airports.put(code, new Airport(name, country, code, passengers));
- }
- public void addFlights(String from, String to, int time, int duration)
- {
- Flight flight = new Flight(from, to, time, duration);
- TreeSet<Flight> ts = flightsFrom.get(from);
- if(ts == null)
- ts = new TreeSet<Flight>();
- ts.add(flight);
- flightsFrom.put(from, ts);
- TreeSet<Flight> ts2 = flightsTo.get(to);
- if(ts2 == null)
- ts2 = new TreeSet<Flight>();
- ts2.add(flight);
- flightsTo.put(to,ts2);
- }
- public void showFlightsFromAirport(String code)
- {
- Airport current = airports.get(code);
- System.out.println(current);
- TreeSet<Flight> ts = flightsFrom.get(code);
- if(ts == null)
- {
- System.out.println("No flights from " + code);
- return;
- }
- int counter = 1;
- for(Flight f : ts)
- System.out.println(counter++ + ". " + f);
- }
- public void showDirectFlightsTo(String to)
- {
- TreeSet<Flight> ts = flightsTo.get(to);
- if(ts == null)
- {
- System.out.println("No flights to " + to);
- return;
- }
- for(Flight f : ts)
- System.out.println(f);
- }
- public void showDirectFlightsFromTo(String from, String to)
- {
- TreeSet<Flight> ts = flightsFrom.get(from);
- if(ts == null)
- {
- System.out.println("No flights from " + from + " to " + to);
- return;
- }
- boolean flag = false;
- for(Flight f : ts)
- if(f.getTo().equals(to))
- {
- System.out.println(f);
- flag = true;
- }
- if(!flag)
- System.out.println("No flights from " + from + " to " + to);
- }
- }
- public class AirportsTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Airports airports = new Airports();
- int n = scanner.nextInt();
- scanner.nextLine();
- String[] codes = new String[n];
- for (int i = 0; i < n; ++i) {
- String al = scanner.nextLine();
- String[] parts = al.split(";");
- airports.addAirport(parts[0], parts[1], parts[2], Integer.parseInt(parts[3]));
- codes[i] = parts[2];
- }
- int nn = scanner.nextInt();
- scanner.nextLine();
- for (int i = 0; i < nn; ++i) {
- String fl = scanner.nextLine();
- String[] parts = fl.split(";");
- airports.addFlights(parts[0], parts[1], Integer.parseInt(parts[2]), Integer.parseInt(parts[3]));
- }
- int f = scanner.nextInt();
- int t = scanner.nextInt();
- String from = codes[f];
- String to = codes[t];
- System.out.printf("===== FLIGHTS FROM %S =====\n", from);
- airports.showFlightsFromAirport(from);
- System.out.printf("===== DIRECT FLIGHTS FROM %S TO %S =====\n", from, to);
- airports.showDirectFlightsFromTo(from, to);
- t += 5;
- t = t % n;
- to = codes[t];
- System.out.printf("===== DIRECT FLIGHTS TO %S =====\n", to);
- airports.showDirectFlightsTo(to);
- }
- }
- // vashiot kod ovde
Add Comment
Please, Sign In to add comment