Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- class Airport
- {
- String name;
- String country;
- String code;
- int passengers;
- public Airport(String name, String country, String code, int passengers) {
- this.name = name;
- this.country = country;
- this.code = code;
- this.passengers = passengers;
- }
- public String getName() {
- return name;
- }
- public String getCountry() {
- return country;
- }
- @Override
- public int hashCode() {
- return Objects.hash(name, country, code, passengers);
- }
- public String getCode() {
- return code;
- }
- public int getPassengers() {
- return passengers;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Airport airport = (Airport) o;
- return passengers == airport.passengers &&
- Objects.equals(name, airport.name) &&
- Objects.equals(country, airport.country) &&
- Objects.equals(code, airport.code);
- }
- @Override
- public String toString() {
- return this.name + " " + this.code;
- }
- }
- class Flight implements Comparable<Flight>
- {
- String from;
- String to;
- int time;
- int duration;
- public Flight(String from, String to, int time, int duration) {
- this.from = from;
- this.to = to;
- this.time = time;
- this.duration = duration;
- }
- public String getFrom() {
- return from;
- }
- public String getTo() {
- return to;
- }
- public int getTime() {
- return time;
- }
- public int getDuration() {
- return duration;
- }
- @Override
- public int compareTo(Flight o) {
- if(this.to.compareTo(o.to) > 0)
- return 1;
- else return 0;
- }
- public String getSahat() {
- boolean flag = false;
- int Ahour = this.time / 60;
- int Aminutes = this.time % 60;
- int Bhour = (this.time + duration) / 60;
- int BMinutes = (this.time + duration) % 60;
- int day=0;
- if (Bhour > 23) {
- day = (Bhour / 24);
- Bhour -= day * 24;
- flag = true;
- }
- int durationH = this.duration/60;
- int durationM = this.duration%60;
- if (flag == false) {
- return String.format("%02d:%02d-%02d:%02d %dh%dm",Ahour,Aminutes,Bhour,BMinutes,
- durationH,durationM);
- }
- else
- {
- return String.format("%02d:%02d-%02d:%02d +%dd %dh%dm",Ahour,Aminutes,Bhour,BMinutes,day
- ,durationH,durationM);
- }
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Flight flight = (Flight) o;
- return time == flight.time &&
- duration == flight.duration &&
- Objects.equals(from, flight.from) &&
- Objects.equals(to, flight.to);
- }
- @Override
- public int hashCode() {
- return Objects.hash(from, to, time, duration);
- }
- @Override
- public String toString()
- {
- return String.format("%s-%s %s",this.from,this.to,this.getSahat());
- }
- }
- class Airports
- {
- Map<Airport,TreeSet<Flight>> mapa;
- public Airports()
- {
- mapa = new HashMap<Airport,TreeSet<Flight>>();
- }
- public void addAirport(String name, String country, String code, int passengers)
- {
- Airport a = new Airport(name,country,code,passengers);
- boolean flag = true;
- if(mapa.containsKey(a))
- {
- flag = false;
- }
- if(flag==true)
- mapa.computeIfAbsent(a, i-> new TreeSet<Flight>());
- }
- public void addFlights(String from, String to, int time, int duration)
- {
- Flight f = new Flight(from,to,time,duration);
- Airport air = mapa.keySet().stream()
- .filter(i->i.code.equals(from)).findAny().orElse(null);
- if(air != null)
- mapa.get(air).add(f);
- }
- public void showFlightsFromAirport(String code)
- {
- Airport air = mapa.keySet().stream().filter(i -> i.code.equals(code)).findAny().orElse(null);
- if (air !=null) {
- System.out.println(air.name + " (" + air.code + ")\n" + air.country + "\n" + air.passengers);
- mapa.get(air).stream().forEach(i -> System.out.println(i.toString()));
- }
- }
- public void showDirectFlightsFromTo(String from, String to)
- {
- Airport air = mapa.keySet().stream()
- .filter(i->i.code.equals(from)).findFirst().get();
- mapa.get(air).stream().filter(i-> i.to.equals(to))
- .forEach(i->System.out.println(i.toString()));
- }
- public void showDirectFlightsTo(String to)
- {
- mapa.values().stream().flatMap(i ->i.stream()).filter(i-> i.to.equals(to))
- .forEach(i-> System.out.println(i.toString()));
- }
- }
- 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