Advertisement
LeatherDeer

Untitled

Nov 13th, 2022
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. class UndergroundSystem {
  2.     private final Map<Integer, Client> clientMap;
  3.     private final Map<String, Map<String, Route>> routeMap;
  4.  
  5.     public UndergroundSystem() {
  6.         clientMap = new HashMap<>();
  7.         routeMap = new HashMap<>();
  8.     }
  9.  
  10.     public void checkIn(int id, String stationName, int t) {
  11.         Client client = new Client(stationName, t);
  12.         clientMap.put(id, client);
  13.     }
  14.  
  15.     public void checkOut(int id, String stationName, int t) {
  16.         Client client = clientMap.get(id);
  17.         routeMap.putIfAbsent(client.inStation, new HashMap<>());
  18.         routeMap.get(client.inStation).putIfAbsent(stationName, new Route(0, 0));
  19.        
  20.         int travelTime = t - client.getTimeStart();
  21.         Route route = getRoute(client.inStation, stationName);
  22.         route.addTime(travelTime);
  23.     }
  24.  
  25.     public double getAverageTime(String startStation, String endStation) {
  26.         Route route = getRoute(startStation, endStation);
  27.         return route.getAverageTime();
  28.     }
  29.  
  30.     private Route getRoute(String startStation, String endStation) {
  31.         return routeMap.get(startStation).get(endStation);
  32.     }
  33.  
  34.     public static void main(String[] args) {
  35.         UndergroundSystem system = new UndergroundSystem();
  36.         system.checkIn(32, "aa", 8);
  37.         system.checkOut(32, "b", 22);
  38.     }
  39.     class Client {
  40.         private String inStation;
  41.         private int timeStart;
  42.  
  43.         public Client(String inStation, int time) {
  44.             this.inStation = inStation;
  45.             this.timeStart = time;
  46.         }
  47.         public String getInStation() {
  48.             return this.inStation;
  49.         }
  50.         public int getTimeStart() {
  51.             return this.timeStart;
  52.         }
  53.  
  54.     }
  55.  
  56.     class Route {
  57.         private int time;
  58.         private int tripCounter;
  59.  
  60.         public Route(int time, int tripCounter) {
  61.             this.time = time;
  62.             this.tripCounter = tripCounter;
  63.         }
  64.  
  65.         public void addTime(int time) {
  66.             this.time += time;
  67.             this.tripCounter++;
  68.         }
  69.         public double getAverageTime() {
  70.             return 1.0d * time / tripCounter;
  71.         }
  72.  
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement