Advertisement
Tsuki11

Untitled

Jun 28th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. package parking;
  2.  
  3. public class Car {
  4.  
  5. private String manufacturer;
  6. private String model;
  7. private int year;
  8.  
  9. public void setManufacturer(String manufacturer) {
  10. this.manufacturer = manufacturer;
  11. }
  12.  
  13. public void setModel(String model) {
  14. this.model = model;
  15. }
  16.  
  17. public void setYear(int year) {
  18. this.year = year;
  19. }
  20.  
  21. public Car(String manufacturer, String model, int year) {
  22. this.setManufacturer(manufacturer);
  23. this.setModel(model);
  24. this.setYear(year);
  25. }
  26.  
  27. public String getManufacturer() {
  28. return this.manufacturer;
  29. }
  30.  
  31. public String getModel() {
  32. return this.model;
  33. }
  34.  
  35. public int getYear() {
  36. return this.year;
  37. }
  38.  
  39. @Override
  40. public String toString (){
  41. return String.format("%s %s (%d)",this.getManufacturer(),this.getModel(),this.getYear());
  42. // TODO: 28/06/2020 принт без интервал, интервал/ трим да си проверя
  43. }
  44. }
  45.  
  46.  
  47.  
  48. package parking;
  49.  
  50. import java.util.ArrayList;
  51. import java.util.Collection;
  52. import java.util.Comparator;
  53.  
  54. public class Parking {
  55. private String type;
  56. private int capacity;
  57. private Collection<Car> data;
  58.  
  59. public Parking(String type, int capacity) {
  60. this.type = type;
  61. this.capacity = capacity;
  62. data = new ArrayList<>();
  63. }
  64.  
  65. public void add(Car car) {
  66. if (this.data.size() < this.capacity) {
  67. this.data.add(car);
  68. }
  69. }
  70.  
  71. public boolean remove(String manufacturer, String model) {
  72. boolean hasCar = this.data.stream().anyMatch(e -> e.getManufacturer().equals(manufacturer) && e.getModel().equals(model));
  73. if (hasCar) {
  74. Car car = this.data.stream().filter(e -> e.getManufacturer().equals(manufacturer) && e.getModel().equals(model))
  75. .findFirst().orElse(null);
  76. this.data.remove(car);
  77. return true;
  78. } else {
  79. return false;
  80. }
  81. }
  82.  
  83. public Car getLatestCar() {
  84. return this.data.stream().max(Comparator.comparingInt(Car::getYear)).orElse(null);
  85. }
  86.  
  87. public Car getCar(String manufacturer, String model) {
  88. return this.data.stream().filter(e -> e.getManufacturer().equals(manufacturer) && e.getModel().equals(model))
  89. .findFirst().orElse(null);
  90. }
  91.  
  92. public int getCount(){
  93. return this.data.size();
  94. }
  95.  
  96. public String getStatistics() {
  97. StringBuilder perPrint = new StringBuilder();
  98. perPrint.append(String.format("The cars are parked in %s:", this.type));
  99. perPrint.append(System.lineSeparator());
  100. this.data.forEach(e -> perPrint.append(e.toString()).append(System.lineSeparator()));
  101. return perPrint.toString().trim();
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement