desislava_topuzakova

03. Parking - class Parking

Jun 10th, 2021
835
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package parking;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.List;
  6.  
  7. public class Parking {
  8.  
  9.     //fields
  10.     private List<Car> data;
  11.     private String type;
  12.     private int capacity;
  13.  
  14.     //constructor
  15.     public Parking(String type, int capacity) {
  16.         this.type = type;
  17.         this.capacity = capacity;
  18.         this.data = new ArrayList<>();
  19.     }
  20.  
  21.     //add -> adds an entity to the data if there is an empty cell for the car.
  22.     public void add(Car car) {
  23.         if(this.data.size() < this.capacity) {
  24.             this.data.add(car);
  25.         }
  26.     }
  27.  
  28.     //remove -> removes the car by given manufacturer and model, if such exists, and returns boolean.
  29.     public boolean remove(String manufacturer, String model) {
  30.         //true -> ако има кола със собственик: manufacturer и модел: model и премахваме колата
  31.         //false -> ако няма кола със собственик: manufacturer и модел: model
  32.         for (Car car : this.data) {
  33.             if(car.getManufacturer().equals(manufacturer) && car.getModel().equals(model)) {
  34.                 return this.data.remove(car);
  35.             }
  36.         }
  37.  
  38.         return false;
  39.     }
  40.  
  41.     //getLatestCar -> returns the latest car (by year) or null if have no cars.
  42.  
  43.     public Car getLatestCar() {
  44.         if(this.data.size() == 0) {
  45.             return null;
  46.         } else {
  47.             return this.data.stream().max(Comparator.comparingInt(Car::getYear)).get();
  48.         }
  49.     }
  50.  
  51.     //getCar -> returns the car with the given manufacturer and model or null if there is no such car.
  52.     public Car getCar(String manufacturer, String model) {
  53.         if(this.data.size() == 0) {
  54.             return null;
  55.         } else {
  56.             for (Car car : this.data) {
  57.                 if(car.getManufacturer().equals(manufacturer) && car.getModel().equals(model)) {
  58.                     return car;
  59.                 }
  60.             }
  61.             return null;
  62.         }
  63.     }
  64.  
  65.     //getCount -> returns the number of cars
  66.     public int getCount() {
  67.         return this.data.size();
  68.     }
  69.  
  70.     //getStatistics – returns a String in the following format
  71.     public String getStatistics() {
  72.         StringBuilder builder = new StringBuilder();
  73.         builder.append("The cars are parked in ").append(this.type).append(":").append(System.lineSeparator());
  74.         for (Car car : this.data) {
  75.             builder.append(car.toString()).append(System.lineSeparator());
  76.         }
  77.         return builder.toString();
  78.     }
  79.  
  80.  
  81.  
  82. }
  83.  
RAW Paste Data