Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.Duration;
- import java.time.LocalDateTime;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.time.LocalDateTime;
- import java.util.Comparator;
- import java.util.List;
- import java.time.LocalDateTime;
- import java.util.*;
- import java.util.stream.Collectors;
- class Parking {
- int capacity;
- Map<String, Car> currenctParkedOnSpots;
- Map<String, List<Car>> parkingHistoryOnSpots;
- public Parking(int capacity) {
- this.capacity = capacity;
- this.currenctParkedOnSpots = new HashMap<>();
- this.parkingHistoryOnSpots = new HashMap<>();
- }
- public void update(String registration, String spot, LocalDateTime timestamp, boolean entrance) {
- if(entrance){
- currenctParkedOnSpots.putIfAbsent(spot,new Car(registration,spot,new ArrayList<>()));
- currenctParkedOnSpots.get(spot).parkingTimes.add(timestamp);
- }
- else{
- Car leavingCar =currenctParkedOnSpots.get(spot);
- currenctParkedOnSpots.remove(spot);
- parkingHistoryOnSpots.putIfAbsent(spot,new ArrayList<>());
- parkingHistoryOnSpots.get(spot).add(leavingCar);
- parkingHistoryOnSpots.get(spot).get(parkingHistoryOnSpots.get(spot).size()-1).parkingTimes.add(timestamp);
- }
- }
- public void currentState() {
- System.out.println(String.format("Capacity filled: %.2f%%",
- 100*(double)Math.abs(currenctParkedOnSpots.size())/capacity));
- currenctParkedOnSpots.values().stream().sorted(Comparator.reverseOrder()).forEach(i-> System.out.println(i));
- }
- public void history() {
- parkingHistoryOnSpots.values().stream().flatMap(list->list.stream()).sorted()
- .forEach(i-> System.out.println(i));
- }
- public Map<String,Double> spotOccupancy(LocalDateTime start, LocalDateTime end) {
- Map<String, Long> minutesBySpot = parkingHistoryOnSpots.values().stream()
- .flatMap(List::stream)
- .collect(Collectors.groupingBy(
- car -> car.spot,
- Collectors.summingLong(car -> car.getTotalDurationInGivenTime(start, end))
- ));
- long totalTime = DateUtil.durationBetween(start,end);
- return minutesBySpot.entrySet().stream()
- .collect(Collectors.toMap(
- entry -> entry.getKey(),
- entry -> 100.0* (entry.getValue() * 1.0 / totalTime),
- (e1, e2) -> e1,
- TreeMap::new
- ));
- }
- public Map<String,Long> carStatistics() {
- return parkingHistoryOnSpots.values().stream().flatMap(list->list.stream()).collect(Collectors.groupingBy(
- i->i.registrationPlate,
- TreeMap::new,
- Collectors.counting()
- ));
- }
- }
- class Car implements Comparable<Car>{
- String registrationPlate;
- String spot;
- List<LocalDateTime> parkingTimes;
- public Car(String registrationPlate, String spot, List<LocalDateTime> parkingTimes) {
- this.registrationPlate = registrationPlate;
- this.spot= spot;
- this.parkingTimes = parkingTimes;
- }
- public long getTotalDurationInGivenTime(LocalDateTime start, LocalDateTime end){
- long timeParked = DateUtil.durationBetween((parkingTimes.get(0).compareTo(start)>0 ? parkingTimes.get(0) : start),
- parkingTimes.get(1).compareTo(end)<0 ? parkingTimes.get(1) : end);
- if(timeParked<0 || DateUtil.durationBetween(start, end)<=timeParked)
- return 0;
- return timeParked;
- }
- public LocalDateTime getParkingTime() {
- return parkingTimes.get(0);
- }
- public int getDurationParked(){
- if(parkingTimes.size() > 1) {
- return (int) DateUtil.durationBetween(parkingTimes.get(0), parkingTimes.get(1));
- }
- else{
- return 0;
- }
- }
- @Override
- public String toString() {
- if(parkingTimes.size()==1)
- return String.format("Registration number: %s Spot: %s Start timestamp: %s",
- registrationPlate,spot,parkingTimes.get(0));
- else{
- return String.format("Registration number: %s Spot: %s Start timestamp: %s End timestamp: %s Duration in minutes: %d",
- registrationPlate,spot,parkingTimes.get(0),parkingTimes.get(1),
- (int)DateUtil.durationBetween(parkingTimes.get(0),parkingTimes.get(1)));
- }
- }
- @Override
- public int compareTo(Car o) {
- return Comparator.comparing(Car::getDurationParked).reversed()
- .thenComparing(Car::getParkingTime)
- .compare(this,o);
- }
- }
- class DateUtil {
- public static long durationBetween(LocalDateTime start, LocalDateTime end) {
- return Duration.between(start, end).toMinutes();
- }
- }
- public class ParkingTesting {
- public static <K, V extends Comparable<V>> void printMapSortedByValue(Map<K, V> map) {
- map.entrySet().stream()
- .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
- .forEach(entry -> System.out.println(String.format("%s -> %s", entry.getKey().toString(), entry.getValue().toString())));
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int capacity = Integer.parseInt(sc.nextLine());
- Parking parking = new Parking(capacity);
- while (sc.hasNextLine()) {
- String line = sc.nextLine();
- String[] parts = line.split("\\s+");
- if (parts[0].equals("update")) {
- String registration = parts[1];
- String spot = parts[2];
- LocalDateTime timestamp = LocalDateTime.parse(parts[3]);
- boolean entrance = Boolean.parseBoolean(parts[4]);
- parking.update(registration, spot, timestamp, entrance);
- } else if (parts[0].equals("currentState")) {
- System.out.println("PARKING CURRENT STATE");
- parking.currentState();
- } else if (parts[0].equals("history")) {
- System.out.println("PARKING HISTORY");
- parking.history();
- } else if (parts[0].equals("carStatistics")) {
- System.out.println("CAR STATISTICS");
- printMapSortedByValue(parking.carStatistics());
- } else if (parts[0].equals("spotOccupancy")) {
- LocalDateTime start = LocalDateTime.parse(parts[1]);
- LocalDateTime end = LocalDateTime.parse(parts[2]);
- printMapSortedByValue(parking.spotOccupancy(start, end));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement