Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.87 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.         int n = Integer.parseInt(reader.readLine());
  13.         List<Zone> allZones = new ArrayList<>();
  14.         List<ParkingSpace> allParkingSpaces = new ArrayList<>();
  15.         for (int i = 0; i < n; i++) {
  16.             String[] input = reader.readLine().split(" ");
  17.             String name = input[0].substring(0, input[0].length() - 1);
  18.             int x = Integer.parseInt(input[1].substring(0, input[1].length() - 1));
  19.             int y = Integer.parseInt(input[2].substring(0, input[2].length() - 1));
  20.             int length = Integer.parseInt(input[3].substring(0, input[3].length() - 1));
  21.             int width = Integer.parseInt(input[4].substring(0, input[4].length() - 1));
  22.             double price = Double.parseDouble(input[5]);
  23.             allZones.add(new Zone(name, x, y, length, width, price));
  24.         }
  25.         String[] input = reader.readLine().split("; ");
  26.         String[] shopCoordinates = reader.readLine().split(", ");
  27.         int timePerBlock = Integer.parseInt(reader.readLine());
  28.         int shopX = Integer.parseInt(shopCoordinates[0]);
  29.         int shopY = Integer.parseInt(shopCoordinates[1]);
  30.         for (int i = 0; i < input.length; i++) {
  31.             int currentParkingPointX = Integer.parseInt(input[i].split(", ")[0]);
  32.             int currentParkingPointY = Integer.parseInt(input[i].split(", ")[1]);
  33.             for (Zone zone : allZones) {
  34.                 if (zone.isParkingSpaceInThisZone(currentParkingPointX, currentParkingPointY)) {
  35.                     allParkingSpaces.add(new ParkingSpace(currentParkingPointX, currentParkingPointY,
  36.                             zone.getPricePerMin(), zone.getName(), shopX, shopY, timePerBlock));
  37.                     break;
  38.                 }
  39.             }
  40.         }
  41.         ParkingSpace cheapestParking = allParkingSpaces.stream().min(Comparator.comparingDouble(ParkingSpace::getPriceToWait)).orElse(null);
  42.         allParkingSpaces.removeIf(x -> x.getPriceToWait() > cheapestParking.getPriceToWait());
  43.         ParkingSpace result = allParkingSpaces.stream().min(Comparator.comparingInt(ParkingSpace::getDistanceFromTheShop)).orElse(null);
  44.         System.out.println(result.toString());
  45.     }
  46. }
  47.  
  48. class Zone {
  49.     private String name;
  50.     private int x;
  51.     private int y;
  52.     private int length;
  53.     private int width;
  54.     private double pricePerMin;
  55.  
  56.     public Zone(String name, int x, int y, int length, int width, double pricePerMin) {
  57.         this.setX(x);
  58.         this.setY(y);
  59.         this.setLength(length);
  60.         this.setWidth(width);
  61.         this.setPricePerMin(pricePerMin);
  62.         this.setName(name);
  63.     }
  64.  
  65.     public boolean isParkingSpaceInThisZone(int x, int y) {
  66.         boolean result = false;
  67.         if ((this.getX() <= x && this.getWidth() + this.getX() - 1 >= x) && (this.getY() <= y && this.getLength() + this.getY() - 1 >= y)) {
  68.             result = true;
  69.         }
  70.         return result;
  71.     }
  72.  
  73.     public int getX() {
  74.         return x;
  75.     }
  76.  
  77.     private void setX(int x) {
  78.         this.x = x;
  79.     }
  80.  
  81.     public int getY() {
  82.         return y;
  83.     }
  84.  
  85.     private void setY(int y) {
  86.         this.y = y;
  87.     }
  88.  
  89.     public int getLength() {
  90.         return length;
  91.     }
  92.  
  93.     private void setLength(int length) {
  94.         this.length = length;
  95.     }
  96.  
  97.     public int getWidth() {
  98.         return width;
  99.     }
  100.  
  101.     private void setWidth(int width) {
  102.         this.width = width;
  103.     }
  104.  
  105.     public double getPricePerMin() {
  106.         return pricePerMin;
  107.     }
  108.  
  109.     private void setPricePerMin(double pricePerMin) {
  110.         this.pricePerMin = pricePerMin;
  111.     }
  112.  
  113.     public String getName() {
  114.         return name;
  115.     }
  116.  
  117.     private void setName(String name) {
  118.         this.name = name;
  119.     }
  120. }
  121.  
  122. class ParkingSpace {
  123.     private int x;
  124.     private int y;
  125.     private int distanceFromTheShop;
  126.     private double priceToWait;
  127.     private double pricePerMin;
  128.     private String zoneName;
  129.  
  130.     public ParkingSpace(int x, int y, double pricePerMin, String zoneName, int shopX, int shopY, int timePerBlock) {
  131.         this.setX(x);
  132.         this.setY(y);
  133.         this.setDistanceFromTheShop(shopX, shopY);
  134.         this.setPriceToWait(timePerBlock);
  135.         this.setPricePerMin(pricePerMin);
  136.         this.setZoneName(zoneName);
  137.         this.setPriceToWait(timePerBlock);
  138.     }
  139.  
  140.     public int getX() {
  141.         return x;
  142.     }
  143.  
  144.     private void setX(int x) {
  145.         this.x = x;
  146.     }
  147.  
  148.     public int getY() {
  149.         return y;
  150.     }
  151.  
  152.     private void setY(int y) {
  153.         this.y = y;
  154.     }
  155.  
  156.     public int getDistanceFromTheShop() {
  157.         return distanceFromTheShop;
  158.     }
  159.  
  160.     private void setDistanceFromTheShop(int shopX, int shopY) {
  161.         this.distanceFromTheShop = Math.abs(shopX - this.getX()) + Math.abs(shopY - this.getY()) - 1;
  162.     }
  163.  
  164.     public double getPriceToWait() {
  165.         return this.priceToWait;
  166.     }
  167.  
  168.     private void setPriceToWait(int timePerBlock) {
  169.         this.priceToWait = (Math.ceil((double)(this.getDistanceFromTheShop() * 2 * timePerBlock) / 60) * this.getPricePerMin());
  170.     }
  171.  
  172.     public double getPricePerMin() {
  173.         return pricePerMin;
  174.     }
  175.  
  176.     private void setPricePerMin(double pricePerMin) {
  177.         this.pricePerMin = pricePerMin;
  178.     }
  179.  
  180.     public String getZoneName() {
  181.         return zoneName;
  182.     }
  183.  
  184.     private void setZoneName(String zoneName) {
  185.         this.zoneName = zoneName;
  186.     }
  187.     @Override
  188.     public String toString(){
  189.        return String.format("Zone Type: %s; X: %s; Y: %s; Price: %.2f%n", this.getZoneName(), this.getX(), this.getY(), this.getPriceToWait());
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement