Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. import java.util.HashMap;
  2.  
  3. class UndergroundSystem {
  4. HashMap<Integer, Checkin> checkins = new HashMap<>();
  5. HashMap<String, HashMap<String, Stats>> statsLookup = new HashMap<>();
  6.  
  7. public void checkIn(int id, String stationName, int t) {
  8. checkins.put(id, new Checkin(stationName, t));
  9. }
  10.  
  11. public void checkOut(int id, String stationName, int t) {
  12. Checkin checkin = checkins.get(id);
  13. statsLookup.put(checkin.stationIn, statsLookup.getOrDefault(checkin.stationIn, new HashMap<>()));
  14. HashMap<String, Stats> inner = statsLookup.get(checkin.stationIn);
  15. inner.put(stationName, inner.getOrDefault(stationName, new Stats()));
  16. inner.get(stationName).samples++;
  17. inner.get(stationName).totalTime += t - checkin.timeIn;
  18. }
  19.  
  20. public double getAverageTime(String startStation, String endStation) {
  21. return statsLookup.get(startStation).get(endStation).getAverage();
  22. }
  23.  
  24. static class Stats {
  25. int totalTime = 0;
  26. int samples = 0;
  27.  
  28. public double getAverage() {
  29. return totalTime / (samples + 0.0);
  30. }
  31. }
  32.  
  33. static class Checkin {
  34. String stationIn;
  35. int timeIn;
  36.  
  37. public Checkin(String s, int t) {
  38. this.stationIn = s;
  39. this.timeIn = t;
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement