Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.time.LocalTime;
- import java.util.Comparator;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.atomic.AtomicInteger;
- 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;
- }
- public String getCode() {
- return code;
- }
- public int getPassengers() {
- return passengers;
- }
- @Override
- public String toString() {
- return "Airport{" +
- "name='" + name + '\'' +
- ", country='" + country + '\'' +
- ", code='" + code + '\'' +
- ", passengers=" + passengers +
- '}';
- }
- }
- class AirportGroup {
- Map<String,Airport> airports;
- Map<String,List<Flight>> flights;
- public AirportGroup() {
- airports= new HashMap<>();
- flights= new HashMap<>();
- }
- public void addAirport(String name, String country, String code, int passengers) {
- airports.putIfAbsent(code,new Airport(name,country,code,passengers));
- flights.putIfAbsent(code,new ArrayList<>());
- }
- public void addFlights(String from, String to, int time, int duration) {
- flights.get(from).add(new Flight(from,to,time,duration));
- }
- public void showFlightsFromAirport(String from) {
- AtomicInteger atomicInteger = new AtomicInteger(1);
- System.out.println(String.format("%s (%s)\n%s\n%d", airports.get(from).getName(),
- airports.get(from).getCode(),
- airports.get(from).getCountry(),
- airports.get(from).getPassengers()));
- flights.get(from).stream().sorted().forEach(i-> System.out.println(String.format("%d. %s",atomicInteger.getAndIncrement(),
- i)));
- }
- public void showDirectFlightsFromTo(String from, String to) {
- if(flights.get(from).stream().filter(i->i.getTo().equals(to)).count()==0){
- System.out.println(String.format("No flights from %s to %s",from,to));
- return ;
- }
- flights.get(from).stream().sorted().filter(i->i.getTo().equals(to)).forEach(i-> System.out.println(String.format("%s", i)));
- }
- public void showDirectFlightsTo(String to) {
- flights.values().stream().flatMap(list->list.stream()).filter(i->i.getTo().equals(to)).sorted().forEach(i-> System.out.println(String.format("%s", i)));
- }
- }
- 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;
- }
- public String getActualTime(){
- LocalTime localTime = LocalTime.ofSecondOfDay(duration);
- String cutTime;
- cutTime = localTime.toString().substring(3).replace(":","h").concat("m");
- if(duration%60==0){
- cutTime=cutTime.replace("m","h00m");
- }
- if(cutTime.startsWith("0"))
- return cutTime.substring(1);
- return cutTime;
- }
- public String getDepartureAndArrivalTime(){
- LocalTime departureTime = LocalTime.ofSecondOfDay(time);
- LocalTime arrivalTime = LocalTime.ofSecondOfDay(time+duration);
- String deprartureTimeCut = departureTime.toString().substring(3);
- String arrivalTimeCut = arrivalTime.toString().substring(3);
- if(time%60==0){
- deprartureTimeCut=deprartureTimeCut.concat(":00");
- }
- if((time+duration)%60==0){
- arrivalTimeCut=arrivalTimeCut.concat(":00");
- }
- if(Integer.parseInt(arrivalTime.toString().substring(3,5))>23){
- arrivalTime = LocalTime.ofSecondOfDay(time+duration-24*60);
- arrivalTimeCut = arrivalTime.toString().substring(3)+" +1d";
- }
- return String.format("%s-%s",deprartureTimeCut,arrivalTimeCut);
- }
- @Override
- public String toString() {
- return String.format("%s-%s %s %s",from,to,getDepartureAndArrivalTime(),getActualTime());
- }
- @Override
- public int compareTo(Flight o) {
- return Comparator.comparing(Flight::getTo)
- .thenComparing(Flight::getTime)
- .thenComparing(Flight::getFrom)
- .compare(this,o);
- }
- }
- public class AirportsTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- AirportGroup airports = new AirportGroup();
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement