SHARE
TWEET

Untitled

a guest Jun 27th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.PrintWriter;
  6. import java.util.*;
  7. import java.util.stream.IntStream;
  8.  
  9. public class ParkingLotManagement {
  10.     public static void main(String[] args) {
  11.         ParkingLot myParkingLot = new ParkingLot("Beverli");
  12.         Scanner jin = new Scanner(System.in);
  13.         System.out.println("Enter vehicle information that is entering the parking.\n" +
  14.                 "This includes the vehicle ID, hours of stay, whether the vehicle is a truck(T) or a car(C)," +
  15.                 "and the floor you wish to park in ! Write END to finish");
  16.         while (true) {
  17.             try {
  18.                 String line = jin.nextLine();
  19.                 line = line.trim();
  20.                 if (line.equals(""))
  21.                     continue;
  22.                 if (line.equals("END"))
  23.                     break;
  24.                 String[] parts = line.split("\\s+");
  25.                 if (parts.length != 4)
  26.                     continue;
  27.  
  28.                 String vehicleID = parts[0];
  29.                 int hourStay = Integer.parseInt(parts[1]);
  30.                 String vehicleType = parts[2];
  31.                 int floor = Integer.parseInt(parts[3]);
  32.                 if (vehicleType.equals("C"))
  33.                     myParkingLot.parkVehicle(floor, new Car(vehicleID, hourStay));
  34.                 else
  35.                     myParkingLot.parkVehicle(floor, new Truck(vehicleID, hourStay));
  36.             } catch (Exception e) {
  37.             }
  38.         }
  39.         System.out.println(myParkingLot);
  40.  
  41.         Vehicle testTruck = new Truck("SomeTruck", 2);
  42.         Vehicle testCar = new Car("SomeCar", 1);
  43.         myParkingLot.parkVehicle(2, new Truck("SomeTruck", 5));
  44.         myParkingLot.parkVehicle(2, new Car("SomeCar", 4));
  45.         myParkingLot.parkVehicle(1, new Car("SomeCar2", 2));
  46.         System.out.println(myParkingLot);
  47.         myParkingLot.leaveParking(testCar);
  48.         myParkingLot.leaveParking(testTruck);
  49.         myParkingLot.leaveParking(new Truck("NonExistent truck", 1));
  50.         System.out.println(myParkingLot);
  51.     }
  52. }
  53.  
  54. class FileSaver{
  55.     public static File saveFile=new File("Parking Backup!");
  56. }
  57.  
  58. //EXCEPTIONS
  59. class InvalidFloorNumber extends Exception {
  60.     private int floor;
  61.  
  62.     public InvalidFloorNumber(int floor) {
  63.         super("There is no floor with number " + floor + " on the parking!");
  64.         this.floor = floor;
  65.     }
  66.  
  67.     public int getFloor() {
  68.         return floor;
  69.     }
  70. }
  71.  
  72. class VehicleNotOnParking extends Exception {
  73.     private Vehicle vehicle;
  74.  
  75.     public VehicleNotOnParking(Vehicle vehicle) {
  76.         super("Vehicle " + vehicle + " is not in the parking!");
  77.         this.vehicle = vehicle;
  78.     }
  79.  
  80.     public Vehicle getVehicle() {
  81.         return vehicle;
  82.     }
  83. }
  84.  
  85. class TruckException extends Exception {
  86.     private Vehicle vehicle;
  87.  
  88.     public TruckException(Vehicle vehicle) {
  89.         super("Vehicle " + vehicle + " is a truck and can only be parked on floor 1!");
  90.         this.vehicle = vehicle;
  91.     }
  92.  
  93.     public Vehicle getVehicle() {
  94.         return vehicle;
  95.     }
  96. }
  97.  
  98. class NoMoreSpotsOnFloor extends Exception {
  99.     private int floorNumber;
  100.  
  101.     public NoMoreSpotsOnFloor(int floorNumber) {
  102.         super("No more spots on floor " + floorNumber);
  103.         this.floorNumber = floorNumber;
  104.     }
  105.  
  106.     public int getFloorNumber() {
  107.         return floorNumber;
  108.     }
  109. }
  110. //EXCEPTIONS
  111.  
  112. abstract class Vehicle {
  113.     private String vehicleID;
  114.     private int hourStay;
  115.  
  116.     Vehicle(String vehicleID, int initialHourStay) {
  117.         this.vehicleID = vehicleID;
  118.         hourStay = initialHourStay;
  119.     }
  120.  
  121.     Vehicle() {
  122.     }
  123.  
  124.     public String getVehicleID() {
  125.         return vehicleID;
  126.     }
  127.  
  128.     public int getHourStay() {
  129.         return hourStay;
  130.     }
  131.  
  132.     public void updateHourStay(int addedTime) {
  133.         this.hourStay += addedTime;
  134.     }
  135.  
  136.     abstract Double pricePerHour();
  137.  
  138.     abstract String getType();
  139.  
  140.     abstract boolean canPark(int floor);
  141.  
  142.     Double totalPay() {
  143.         return pricePerHour() * hourStay;
  144.     }
  145.  
  146.     @Override
  147.     public String toString() {
  148.         return ((getType().equals("C")) ? "Car " : "Truck ") + vehicleID;
  149.     }
  150.  
  151.     @Override
  152.     public boolean equals(Object o) {
  153.         if (this == o) return true;
  154.         if (o == null || getClass() != o.getClass()) return false;
  155.         Vehicle vehicle = (Vehicle) o;
  156.         return vehicleID.equals(vehicle.vehicleID);
  157.     }
  158.  
  159.     @Override
  160.     public int hashCode() {
  161.         return Objects.hash(vehicleID);
  162.     }
  163. }
  164.  
  165. class Car extends Vehicle {
  166.  
  167.     public Car(String vehicleID, int initialHourStay) {
  168.         super(vehicleID, initialHourStay);
  169.     }
  170.  
  171.     public Car() {
  172.     }
  173.  
  174.     @Override
  175.     Double pricePerHour() {
  176.         return 1.0;
  177.     }
  178.  
  179.     @Override
  180.     String getType() {
  181.         return "C";
  182.     }
  183.  
  184.     @Override
  185.     boolean canPark(int floor) {
  186.         return true;
  187.     }
  188. }
  189.  
  190. class Truck extends Vehicle {
  191.  
  192.     public Truck(String vehicleID, int initialHourStay) {
  193.         super(vehicleID, initialHourStay);
  194.     }
  195.  
  196.     public Truck() {
  197.     }
  198.  
  199.     @Override
  200.     Double pricePerHour() {
  201.         return 5.0;
  202.     }
  203.  
  204.     @Override
  205.     String getType() {
  206.         return "T";
  207.     }
  208.  
  209.     @Override
  210.     boolean canPark(int floor) {
  211.         return floor == 1;
  212.     }
  213. }
  214.  
  215. class Floor {
  216.     private List<Vehicle> vehicles;
  217.     private static int numberOfVehicles = 40;
  218.     private int floorNumber;
  219.  
  220.     public Floor(int floorNumber) {
  221.         this.floorNumber = floorNumber;
  222.         vehicles = new ArrayList<>(numberOfVehicles);
  223.     }
  224.  
  225.     public void parkVehicle(Vehicle vehicle) throws NoMoreSpotsOnFloor {
  226.         if (vehicles.size() != numberOfVehicles) {
  227.             vehicles.add(vehicle);
  228.             System.out.println("Vehicle : " + vehicle + " is parked on floor "+floorNumber);
  229.         } else
  230.             throw new NoMoreSpotsOnFloor(floorNumber);
  231.     }
  232.  
  233.     public void leaveVehicle(Vehicle vehicle) {
  234.         vehicles.remove(vehicle);
  235.         System.out.println("Vehicle : " + vehicle + " is leaving the floor "
  236.                 + floorNumber + "!");
  237.     }
  238.  
  239.     public boolean vehicleOnFloor(Vehicle vehicle) {
  240.         return vehicles.contains(vehicle);
  241.     }
  242.  
  243.     public int spotsLeft() {
  244.         return numberOfVehicles - spotsTaken();
  245.     }
  246.  
  247.     public int spotsTaken() {
  248.         return vehicles.size();
  249.     }
  250.  
  251.     public List<Vehicle> getVehicles() {
  252.         return vehicles;
  253.     }
  254.  
  255.     public int getFloorNumber() {
  256.         return floorNumber;
  257.     }
  258.  
  259.     @Override
  260.     public String toString() {
  261.         StringBuilder stringBuilder = new StringBuilder();
  262.         stringBuilder.append("Parking floor : ").append(floorNumber).append("\n");
  263.         vehicles
  264.                 .forEach(vehicle -> stringBuilder.append(vehicle).append("\n"));
  265.  
  266.         stringBuilder.append("Free spots: ").append(spotsLeft()).append("/")
  267.                 .append(numberOfVehicles);
  268.         stringBuilder.append("\n");
  269.         return stringBuilder.toString();
  270.     }
  271.  
  272.     @Override
  273.     public boolean equals(Object o) {
  274.         if (this == o) return true;
  275.         if (o == null || getClass() != o.getClass()) return false;
  276.         Floor floor = (Floor) o;
  277.         return floorNumber == floor.floorNumber &&
  278.                 vehicles.equals(floor.vehicles);
  279.     }
  280.  
  281.     @Override
  282.     public int hashCode() {
  283.         return Objects.hash(vehicles, floorNumber);
  284.     }
  285. }
  286.  
  287. class ParkingLot {
  288.     private List<Floor> floors;
  289.     private static int numberOfFloors = 3;
  290.     private static int saveInteral=60;
  291.     private String parkingName;
  292.     private Double dailyProfit;
  293.  
  294.  
  295.     public ParkingLot(String parkingName) {
  296.         this.parkingName = parkingName;
  297.         floors = new ArrayList<>(numberOfFloors);
  298.         IntStream.range(0, numberOfFloors)
  299.                 .forEach(index -> floors.add(new Floor(index + 1)));
  300.         dailyProfit=0.0;
  301.     }
  302.  
  303.     public void parkVehicle(int floor, Vehicle vehicle) {
  304.         try {
  305.             if (floor >= 1 && floor <= numberOfFloors) {
  306.                 if (vehicle.canPark(floor)) {
  307.                     floors.get(floor - 1).parkVehicle(vehicle);
  308.                     dailyProfit += vehicle.totalPay();
  309.                 }
  310.                 else
  311.                     throw new TruckException(vehicle);
  312.             } else
  313.                 throw new InvalidFloorNumber(floor);
  314.         } catch (NoMoreSpotsOnFloor | TruckException
  315.                 | InvalidFloorNumber noMoreSpotsOnFloor) {
  316.             System.out.println(noMoreSpotsOnFloor.getMessage());
  317.         }
  318.     }
  319.  
  320.     public void leaveParking(Vehicle vehicle) {
  321.         try {
  322.             Floor neededFloor
  323.                     = floors
  324.                     .stream()
  325.                     .filter(floor -> floor.vehicleOnFloor(vehicle))
  326.                     .findAny()
  327.                     .orElse(null);
  328.             if (neededFloor != null)
  329.                 neededFloor.leaveVehicle(vehicle);
  330.             else
  331.                 throw new VehicleNotOnParking(vehicle);
  332.         } catch (VehicleNotOnParking vehicleNotOnParking) {
  333.             System.out.println(vehicleNotOnParking.getMessage());
  334.         }
  335.     }
  336.  
  337.     public int getFreeSpotsInParking() {
  338.         return
  339.                 floors
  340.                         .stream()
  341.                         .mapToInt(Floor::spotsLeft)
  342.                         .sum();
  343.     }
  344.  
  345.     public List<Floor> getFloors() {
  346.         return floors;
  347.     }
  348.  
  349.     public static int getNumberOfFloors() {
  350.         return numberOfFloors;
  351.     }
  352.  
  353.     public String getParkingName() {
  354.         return parkingName;
  355.     }
  356.  
  357.     public Double dailyTotal() {
  358.         return dailyProfit;
  359.     }
  360.  
  361.     @Override
  362.     public String toString() {
  363.         StringBuilder stringBuilder = new StringBuilder();
  364.         stringBuilder.append(parkingName).append(" :\n\n");
  365.         floors
  366.                 .forEach(stringBuilder::append);
  367.  
  368.         stringBuilder.append("Total free spots: ").append(getFreeSpotsInParking()).append("\n");
  369.         stringBuilder.append("Daily profit: ").append(dailyTotal()).append("\n");
  370.  
  371.         return stringBuilder.toString();
  372.     }
  373.  
  374.     @Override
  375.     public boolean equals(Object o) {
  376.         if (this == o) return true;
  377.         if (o == null || getClass() != o.getClass()) return false;
  378.         ParkingLot that = (ParkingLot) o;
  379.         return floors.equals(that.floors) &&
  380.                 parkingName.equals(that.parkingName);
  381.     }
  382.  
  383.     @Override
  384.     public int hashCode() {
  385.         return Objects.hash(floors, parkingName);
  386.     }
  387.  
  388.     public void periodicSave(){
  389.         //simulate 60 seconds
  390.         saveParking();
  391.     }
  392.  
  393.     public void saveParking(){
  394.         try (PrintWriter printWriter = new PrintWriter(new FileOutputStream(FileSaver.saveFile))) {
  395.             printWriter.write(this.toString());
  396.             printWriter.flush();
  397.         } catch (FileNotFoundException e) {
  398.             System.out.println(e.getMessage());
  399.         }
  400.     }
  401. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top