Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package softUniParking;
  2.  
  3. public class Car {
  4. private String make;
  5. private String model;
  6. private int horsePower;
  7. private String registrationNumber;
  8.  
  9. public Car(String make, String model, int horsePower, String registrationNumber) {
  10. this.make = make;
  11. this.model = model;
  12. this.horsePower = horsePower;
  13. this.registrationNumber = registrationNumber;
  14. }
  15.  
  16. public String getMake() {
  17. return this.make;
  18. }
  19. public String getModel() {
  20. return this.model;
  21. }
  22. public int getHorsePower() {
  23. return this.horsePower;
  24. }
  25. public String getRegistrationNumber() {
  26. return this.registrationNumber;
  27. }
  28.  
  29.  
  30.  
  31. @Override
  32. public String toString() {
  33. return String.format(
  34. "Make: %s%n" +
  35. "Model: %s%n" +
  36. "HorsePower: %d%n" +
  37. "RegistrationNumber: %s"
  38. ,this.make,this.model,this.horsePower,this.registrationNumber);
  39. }
  40. }
  41. package softUniParking;
  42.  
  43. import java.util.HashMap;
  44. import java.util.List;
  45. import java.util.Map;
  46.  
  47. public class Parking {
  48. private Map<String,Car> cars;
  49. private int capacity;
  50.  
  51. public Parking(int capacity) {
  52. this.cars = new HashMap<>();
  53. this.capacity = capacity;
  54. }
  55. public String addCar(Car car){
  56. if (this.cars.containsKey(car.getRegistrationNumber())){
  57. return "Car with that registration number, already exists!";
  58. }else if (this.cars.size() == this.capacity){
  59. return "Parking is full!";
  60. }
  61. this.cars.putIfAbsent(car.getRegistrationNumber(),car);
  62. return String.format("Successfully added new car %s %s",car.getMake(),car.getRegistrationNumber());
  63. }
  64. public String removeCar(String registrationNumber){
  65. if (!this.cars.containsKey(registrationNumber)){
  66. return "Car with that registration number, doesn't exists!";
  67. }
  68. this.cars.remove(registrationNumber);
  69. return String.format("Successfully removed %s",registrationNumber);
  70. }
  71. public String getCar(String registrationNumber){
  72.  
  73. return this.cars.get(registrationNumber).toString();
  74. }
  75. public void removeSetOfRegistrationNumber(List<String> registrationNumbers){
  76. for (String registrationNumber : registrationNumbers) {
  77. this.removeCar(registrationNumber);
  78. }
  79. }
  80.  
  81. public int getCount(){
  82. return cars.size();
  83. }
  84.  
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement