|
|
@@ -1,6 +1,9 @@
|
|
|
package eu.fibane.parkingtoll.model;
|
|
|
|
|
|
+import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
|
+import eu.fibane.parkingtoll.exceptions.ParkingTypeDoesNotExistException;
|
|
|
+import lombok.Data;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import javax.validation.Valid;
|
|
|
@@ -9,11 +12,14 @@ import javax.validation.constraints.Null;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* ParkingLot
|
|
|
*/
|
|
|
@Validated
|
|
|
+@Data
|
|
|
public class ParkingLot {
|
|
|
@JsonProperty("id")
|
|
|
private Long id = null;
|
|
|
@@ -23,115 +29,33 @@ public class ParkingLot {
|
|
|
|
|
|
@JsonProperty("layout")
|
|
|
@Valid
|
|
|
- private List<Layout> layout = new ArrayList<Layout>();
|
|
|
+ private List<Layout> layoutList = new ArrayList<Layout>();
|
|
|
|
|
|
@JsonProperty("pricing_policy")
|
|
|
private PricingPolicy pricingPolicy = null;
|
|
|
|
|
|
- public ParkingLot id(Long id) {
|
|
|
- this.id = id;
|
|
|
- return this;
|
|
|
+ public CarSlot parkCar(CarSlot carSlot){
|
|
|
+ Layout layout = getLayoutByName(carSlot.getType());
|
|
|
+ layout.parkCar(carSlot);
|
|
|
+ return carSlot;
|
|
|
}
|
|
|
|
|
|
- @Null
|
|
|
- public Long getId() {
|
|
|
- return id;
|
|
|
+ public CarSlot removeCar(CarSlot carSlot){
|
|
|
+ Layout layout = getLayoutByName(carSlot.getType());
|
|
|
+ CarSlot oldCarSlot = layout.removeCar(carSlot);
|
|
|
+ return oldCarSlot;
|
|
|
}
|
|
|
|
|
|
- public void setId(Long id) {
|
|
|
- this.id = id;
|
|
|
- }
|
|
|
-
|
|
|
- public ParkingLot name(String name) {
|
|
|
- this.name = name;
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- @NotNull
|
|
|
- public String getName() {
|
|
|
- return name;
|
|
|
- }
|
|
|
-
|
|
|
- public void setName(String name) {
|
|
|
- this.name = name;
|
|
|
- }
|
|
|
-
|
|
|
- public ParkingLot layout(List<Layout> layout) {
|
|
|
- this.layout = layout;
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- public ParkingLot addLayoutItem(Layout layoutItem) {
|
|
|
- this.layout.add(layoutItem);
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- @NotNull @Valid
|
|
|
- public List<Layout> getLayout() {
|
|
|
- return layout;
|
|
|
- }
|
|
|
|
|
|
- public void setLayout(List<Layout> layout) {
|
|
|
- this.layout = layout;
|
|
|
+ private Layout getLayoutByName(String name){
|
|
|
+ Optional<Layout> layout = layoutList.stream().filter(a -> a.getName().equals(name)).findFirst();
|
|
|
+ return layout.orElseThrow(() -> new ParkingTypeDoesNotExistException("This type of slot does not exist in this parking lot"));
|
|
|
}
|
|
|
|
|
|
- public ParkingLot pricingPolicy(PricingPolicy pricingPolicy) {
|
|
|
- this.pricingPolicy = pricingPolicy;
|
|
|
- return this;
|
|
|
+ @JsonIgnore
|
|
|
+ public List<String> getSlotTypes(){
|
|
|
+ return layoutList.stream().map(Layout::getName).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
- @NotNull @Valid
|
|
|
- public PricingPolicy getPricingPolicy() {
|
|
|
- return pricingPolicy;
|
|
|
- }
|
|
|
-
|
|
|
- public void setPricingPolicy(PricingPolicy pricingPolicy) {
|
|
|
- this.pricingPolicy = pricingPolicy;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean equals(Object o) {
|
|
|
- if (this == o) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- if (o == null || getClass() != o.getClass()) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- ParkingLot parkingLot = (ParkingLot) o;
|
|
|
- return Objects.equals(this.id, parkingLot.id) &&
|
|
|
- Objects.equals(this.name, parkingLot.name) &&
|
|
|
- Objects.equals(this.layout, parkingLot.layout) &&
|
|
|
- Objects.equals(this.pricingPolicy, parkingLot.pricingPolicy);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int hashCode() {
|
|
|
- return Objects.hash(id, name, layout, pricingPolicy);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String toString() {
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- sb.append("class ParkingLot {\n");
|
|
|
-
|
|
|
- sb.append(" id: ").append(toIndentedString(id)).append("\n");
|
|
|
- sb.append(" name: ").append(toIndentedString(name)).append("\n");
|
|
|
- sb.append(" layout: ").append(toIndentedString(layout)).append("\n");
|
|
|
- sb.append(" pricingPolicy: ").append(toIndentedString(pricingPolicy)).append("\n");
|
|
|
- sb.append("}");
|
|
|
- return sb.toString();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Convert the given object to string with each line indented by 4 spaces
|
|
|
- * (except the first line).
|
|
|
- */
|
|
|
- private String toIndentedString(Object o) {
|
|
|
- if (o == null) {
|
|
|
- return "null";
|
|
|
- }
|
|
|
- return o.toString().replace("\n", "\n ");
|
|
|
- }
|
|
|
}
|
|
|
|