Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package softUniParking;
- public class Car {
- private String make;
- private String model;
- private int horsePower;
- private String registrationNumber;
- public Car(String make, String model, int horsePower, String registrationNumber) {
- this.make = make;
- this.model = model;
- this.horsePower = horsePower;
- this.registrationNumber = registrationNumber;
- }
- public String getMake() {
- return this.make;
- }
- public String getModel() {
- return this.model;
- }
- public int getHorsePower() {
- return this.horsePower;
- }
- public String getRegistrationNumber() {
- return this.registrationNumber;
- }
- @Override
- public String toString() {
- return String.format(
- "Make: %s%n" +
- "Model: %s%n" +
- "HorsePower: %d%n" +
- "RegistrationNumber: %s"
- ,this.make,this.model,this.horsePower,this.registrationNumber);
- }
- }
- package softUniParking;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class Parking {
- private Map<String,Car> cars;
- private int capacity;
- public Parking(int capacity) {
- this.cars = new HashMap<>();
- this.capacity = capacity;
- }
- public String addCar(Car car){
- if (this.cars.containsKey(car.getRegistrationNumber())){
- return "Car with that registration number, already exists!";
- }else if (this.cars.size() == this.capacity){
- return "Parking is full!";
- }
- this.cars.putIfAbsent(car.getRegistrationNumber(),car);
- return String.format("Successfully added new car %s %s",car.getMake(),car.getRegistrationNumber());
- }
- public String removeCar(String registrationNumber){
- if (!this.cars.containsKey(registrationNumber)){
- return "Car with that registration number, doesn't exists!";
- }
- this.cars.remove(registrationNumber);
- return String.format("Successfully removed %s",registrationNumber);
- }
- public String getCar(String registrationNumber){
- return this.cars.get(registrationNumber).toString();
- }
- public void removeSetOfRegistrationNumber(List<String> registrationNumbers){
- for (String registrationNumber : registrationNumbers) {
- this.removeCar(registrationNumber);
- }
- }
- public int getCount(){
- return cars.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement