SHARE
TWEET

softUniParking

elvirynwa Jun 21st, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package softUniParking;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         Parking parking = new Parking(3);
  12.         Car audi = new Car("Audi", "A8",2000,"03-230-GA");
  13.         Car passat = new Car("Passat", "5",2000,"04-150-FA");
  14.         Car bmw = new Car("BMW", "M3",1800,"04-150-FA");
  15.         Car mercedes = new Car("Mercedes", "D",2500,"04-000-CD");
  16.  
  17.         System.out.println(audi.toString());
  18.  
  19.         System.out.println(parking.addCar(passat));
  20.         System.out.println(parking.addCar(audi));
  21.         System.out.println(parking.addCar(bmw));
  22.         System.out.println(parking.addCar(mercedes));
  23.  
  24.         System.out.println(parking.removeCar("03-230-GA"));
  25.         System.out.println(parking.removeCar("invalid"));
  26.  
  27.         System.out.println(parking.getCar("03-230-GA".toString()));
  28.  
  29.         List<String> regNums = new ArrayList<>();
  30.  
  31.         regNums.add("03-230-GA");
  32.         regNums.add("04-150-FA");
  33.         regNums.add("invalid");
  34.  
  35.         parking.removeSetOfRegistrationNumber(regNums);
  36.         System.out.println(parking.getCount());
  37.     }
  38. }
  39.  
  40. package softUniParking;
  41.  
  42. public class Car {
  43.     private String make;
  44.     private String model;
  45.     private int horsePower;
  46.     private String registrationNumber;
  47.  
  48.     public Car(String make, String model, int horsePower, String registrationNumber) {
  49.         this.make = make;
  50.         this.model = model;
  51.         this.horsePower = horsePower;
  52.         this.registrationNumber = registrationNumber;
  53.     }
  54.  
  55.     public String getMake() {
  56.         return this.make;
  57.     }
  58.  
  59.     public String getModel() {
  60.         return this.model;
  61.     }
  62.  
  63.     public int getHorsePower() {
  64.         return this.horsePower;
  65.     }
  66.  
  67.     public String getRegistrationNumber() {
  68.         return this.registrationNumber;
  69.     }
  70.  
  71.     @Override
  72.     public String toString() {
  73.         return String.format("Make: %s%n" +
  74.                         "Model: %s%n" +
  75.                         "HorsePower: %d%n" +
  76.                         "RegistrationNumber: %s",
  77.                 this.getMake(),
  78.                 this.getModel(),
  79.                 this.getHorsePower(),
  80.                 this.getRegistrationNumber()
  81.         );
  82.     }
  83. }
  84.  
  85.  
  86. package softUniParking;
  87.  
  88. import java.util.LinkedHashMap;
  89. import java.util.List;
  90. import java.util.Map;
  91.  
  92. public class Parking {
  93.     private Map<String, Car> cars;
  94.     private int capacity;
  95.  
  96.     public Parking(int capacity) {
  97.         this.cars = new LinkedHashMap<>();
  98.         this.capacity = capacity;
  99.     }
  100.  
  101.     public String addCar(Car car) {
  102.         if (cars.size() >= capacity) {
  103.             return "Parking is full!";
  104.         } else if (!cars.containsKey(car.getRegistrationNumber())) {
  105.             cars.put(car.getRegistrationNumber(), car);
  106.             return String.format("Successfully added new car %s %s", car.getMake(), car.getRegistrationNumber());
  107.         } else {
  108.             return "Car with that registration number, already exists!";
  109.         }
  110.     }
  111.  
  112.     public String removeCar(String registrationNumber) {
  113.         if (!this.cars.containsKey(registrationNumber)) {
  114.             return "Car with that registration number, doesn't exists!";
  115.         } else {
  116.             return String.format("Successfully removed %s", registrationNumber);
  117.         }
  118.     }
  119.  
  120.     public Car getCar(String registrationNumber) {
  121.         return cars.get(registrationNumber);
  122.     }
  123.  
  124.     public void removeSetOfRegistrationNumber(List<String> registrationNumbers) {
  125.  
  126.         for (String regNum : registrationNumbers) {
  127.             if (this.cars.containsKey(regNum)) {
  128.                 this.cars.remove(regNum);
  129.             }
  130.         }
  131.     }
  132.  
  133.     public int getCount() {
  134.         return this.cars.size();
  135.     }
  136. }
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