Advertisement
dzocesrce

[NP] Parking Garage

Apr 28th, 2025 (edited)
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.75 KB | None | 0 0
  1. import java.time.Duration;
  2. import java.time.LocalDateTime;
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5. import java.time.LocalDateTime;
  6. import java.util.Comparator;
  7. import java.util.List;
  8. import java.time.LocalDateTime;
  9. import java.util.*;
  10. import java.util.stream.Collectors;
  11.  
  12. class Parking {
  13.     int capacity;
  14.     Map<String, Car> currenctParkedOnSpots;
  15.     Map<String, List<Car>> parkingHistoryOnSpots;
  16.  
  17.     public Parking(int capacity) {
  18.         this.capacity = capacity;
  19.         this.currenctParkedOnSpots = new HashMap<>();
  20.         this.parkingHistoryOnSpots = new HashMap<>();
  21.     }
  22.  
  23.     public void update(String registration, String spot, LocalDateTime timestamp, boolean entrance) {
  24.         if(entrance){
  25.             currenctParkedOnSpots.putIfAbsent(spot,new Car(registration,spot,new ArrayList<>()));
  26.             currenctParkedOnSpots.get(spot).parkingTimes.add(timestamp);
  27.         }
  28.         else{
  29.             Car leavingCar =currenctParkedOnSpots.get(spot);
  30.             currenctParkedOnSpots.remove(spot);
  31.             parkingHistoryOnSpots.putIfAbsent(spot,new ArrayList<>());
  32.             parkingHistoryOnSpots.get(spot).add(leavingCar);
  33.             parkingHistoryOnSpots.get(spot).get(parkingHistoryOnSpots.get(spot).size()-1).parkingTimes.add(timestamp);
  34.         }
  35.  
  36.     }
  37.  
  38.     public void currentState() {
  39.         System.out.println(String.format("Capacity filled: %.2f%%",
  40.                 100*(double)Math.abs(currenctParkedOnSpots.size())/capacity));
  41.         currenctParkedOnSpots.values().stream().sorted(Comparator.reverseOrder()).forEach(i-> System.out.println(i));
  42.     }
  43.  
  44.     public void history() {
  45.         parkingHistoryOnSpots.values().stream().flatMap(list->list.stream()).sorted()
  46.                 .forEach(i-> System.out.println(i));
  47.     }
  48.  
  49.     public Map<String,Double> spotOccupancy(LocalDateTime start, LocalDateTime end) {
  50.         Map<String, Long> minutesBySpot = parkingHistoryOnSpots.values().stream()
  51.                 .flatMap(List::stream)
  52.                 .collect(Collectors.groupingBy(
  53.                         car -> car.spot,
  54.                         Collectors.summingLong(car -> car.getTotalDurationInGivenTime(start, end))
  55.                 ));
  56.         long totalTime =  DateUtil.durationBetween(start,end);
  57.         return minutesBySpot.entrySet().stream()
  58.                 .collect(Collectors.toMap(
  59.                         entry -> entry.getKey(),
  60.                         entry -> 100.0* (entry.getValue() * 1.0 / totalTime),
  61.                         (e1, e2) -> e1,
  62.                         TreeMap::new
  63.                 ));
  64.     }
  65.  
  66.     public Map<String,Long> carStatistics() {
  67.         return parkingHistoryOnSpots.values().stream().flatMap(list->list.stream()).collect(Collectors.groupingBy(
  68.                 i->i.registrationPlate,
  69.                 TreeMap::new,
  70.                 Collectors.counting()
  71.         ));
  72.  
  73.     }
  74. }
  75.  
  76. class Car implements Comparable<Car>{
  77.     String registrationPlate;
  78.     String spot;
  79.     List<LocalDateTime> parkingTimes;
  80.  
  81.     public Car(String registrationPlate, String spot, List<LocalDateTime> parkingTimes) {
  82.         this.registrationPlate = registrationPlate;
  83.         this.spot= spot;
  84.         this.parkingTimes = parkingTimes;
  85.     }
  86.  
  87.     public long getTotalDurationInGivenTime(LocalDateTime start, LocalDateTime end){
  88.  
  89.         long timeParked =  DateUtil.durationBetween((parkingTimes.get(0).compareTo(start)>0 ? parkingTimes.get(0) : start),
  90.                 parkingTimes.get(1).compareTo(end)<0 ? parkingTimes.get(1) : end);
  91.         if(timeParked<0 || DateUtil.durationBetween(start, end)<=timeParked)
  92.             return 0;
  93.         return timeParked;
  94.     }
  95.    
  96.         public LocalDateTime getParkingTime() {
  97.         return parkingTimes.get(0);
  98.     }
  99.  
  100.     public int getDurationParked(){
  101.         if(parkingTimes.size() > 1) {
  102.             return (int) DateUtil.durationBetween(parkingTimes.get(0), parkingTimes.get(1));
  103.         }
  104.         else{
  105.             return 0;
  106.         }
  107.     }
  108.  
  109.     @Override
  110.     public String toString() {
  111.         if(parkingTimes.size()==1)
  112.             return String.format("Registration number: %s Spot: %s Start timestamp: %s",
  113.                     registrationPlate,spot,parkingTimes.get(0));
  114.         else{
  115.             return String.format("Registration number: %s Spot: %s Start timestamp: %s End timestamp: %s Duration in minutes: %d",
  116.                     registrationPlate,spot,parkingTimes.get(0),parkingTimes.get(1),
  117.                     (int)DateUtil.durationBetween(parkingTimes.get(0),parkingTimes.get(1)));
  118.         }
  119.     }
  120.  
  121.     @Override
  122.     public int compareTo(Car o) {
  123.         return Comparator.comparing(Car::getDurationParked).reversed()
  124.                 .thenComparing(Car::getParkingTime)
  125.                 .compare(this,o);
  126.     }
  127. }
  128. class DateUtil {
  129.     public static long durationBetween(LocalDateTime start, LocalDateTime end) {
  130.         return Duration.between(start, end).toMinutes();
  131.     }
  132. }
  133.  
  134. public class ParkingTesting {
  135.  
  136.     public static <K, V extends Comparable<V>> void printMapSortedByValue(Map<K, V> map) {
  137.         map.entrySet().stream()
  138.                 .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
  139.                 .forEach(entry -> System.out.println(String.format("%s -> %s", entry.getKey().toString(), entry.getValue().toString())));
  140.  
  141.     }
  142.  
  143.     public static void main(String[] args) {
  144.         Scanner sc = new Scanner(System.in);
  145.         int capacity = Integer.parseInt(sc.nextLine());
  146.  
  147.         Parking parking = new Parking(capacity);
  148.  
  149.         while (sc.hasNextLine()) {
  150.             String line = sc.nextLine();
  151.             String[] parts = line.split("\\s+");
  152.             if (parts[0].equals("update")) {
  153.                 String registration = parts[1];
  154.                 String spot = parts[2];
  155.                 LocalDateTime timestamp = LocalDateTime.parse(parts[3]);
  156.                 boolean entrance = Boolean.parseBoolean(parts[4]);
  157.                 parking.update(registration, spot, timestamp, entrance);
  158.             } else if (parts[0].equals("currentState")) {
  159.                 System.out.println("PARKING CURRENT STATE");
  160.                 parking.currentState();
  161.             } else if (parts[0].equals("history")) {
  162.                 System.out.println("PARKING HISTORY");
  163.                 parking.history();
  164.             } else if (parts[0].equals("carStatistics")) {
  165.                 System.out.println("CAR STATISTICS");
  166.                 printMapSortedByValue(parking.carStatistics());
  167.             } else if (parts[0].equals("spotOccupancy")) {
  168.                 LocalDateTime start = LocalDateTime.parse(parts[1]);
  169.                 LocalDateTime end = LocalDateTime.parse(parts[2]);
  170.                 printMapSortedByValue(parking.spotOccupancy(start, end));
  171.             }
  172.         }
  173.     }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement