diff --git a/solutions/java/src/hotelmanagementsystem/HotelManagerFacade.java b/solutions/java/src/hotelmanagementsystem/HotelManagerFacade.java index 243bea9a..81fb00b1 100644 --- a/solutions/java/src/hotelmanagementsystem/HotelManagerFacade.java +++ b/solutions/java/src/hotelmanagementsystem/HotelManagerFacade.java @@ -15,6 +15,7 @@ import hotelmanagementsystem.specification.Specification; import java.time.LocalDate; +import java.time.temporal.ChronoUnit; import java.util.List; import java.util.Optional; @@ -44,7 +45,7 @@ public Booking bookRoom(Guest guest, RoomType type, RoomStyle style, LocalDate s Booking booking = bookingService.createBooking(guest, room, start, end); // 3. Use Decorator pattern to calculate total cost with amenities - Bookable bookable = new RoomBooking(room); + Bookable bookable = new RoomBooking(room, (int) ChronoUnit.DAYS.between(start, end)); for (String amenity : amenities) { if ("breakfast".equalsIgnoreCase(amenity)) { bookable = new BreakfastDecorator(bookable); diff --git a/solutions/java/src/hotelmanagementsystem/decorator/RoomBooking.java b/solutions/java/src/hotelmanagementsystem/decorator/RoomBooking.java index 4931b345..639d3d66 100644 --- a/solutions/java/src/hotelmanagementsystem/decorator/RoomBooking.java +++ b/solutions/java/src/hotelmanagementsystem/decorator/RoomBooking.java @@ -4,14 +4,16 @@ public class RoomBooking implements Bookable { private final Room room; + private final int numberOfNights; - public RoomBooking(Room room) { + public RoomBooking(Room room, int numberOfNights) { this.room = room; + this.numberOfNights = numberOfNights; } @Override public double getCost() { - return room.getPrice(); + return room.getPrice() * numberOfNights; } @Override diff --git a/solutions/java/src/hotelmanagementsystem/specification/AbstractSpecification.java b/solutions/java/src/hotelmanagementsystem/specification/AbstractSpecification.java deleted file mode 100644 index d74e2bdb..00000000 --- a/solutions/java/src/hotelmanagementsystem/specification/AbstractSpecification.java +++ /dev/null @@ -1,7 +0,0 @@ -package hotelmanagementsystem.specification; - -public abstract class AbstractSpecification implements Specification { - public Specification and(Specification other) { - return new AndSpecification<>(this, other); - } -} diff --git a/solutions/java/src/hotelmanagementsystem/specification/AndSpecification.java b/solutions/java/src/hotelmanagementsystem/specification/AndSpecification.java deleted file mode 100644 index ea94e3ab..00000000 --- a/solutions/java/src/hotelmanagementsystem/specification/AndSpecification.java +++ /dev/null @@ -1,16 +0,0 @@ -package hotelmanagementsystem.specification; - -public class AndSpecification extends AbstractSpecification { - private final Specification spec1; - private final Specification spec2; - - public AndSpecification(Specification spec1, Specification spec2) { - this.spec1 = spec1; - this.spec2 = spec2; - } - - @Override - public boolean isSatisfiedBy(T item) { - return spec1.isSatisfiedBy(item) && spec2.isSatisfiedBy(item); - } -} diff --git a/solutions/java/src/hotelmanagementsystem/specification/RoomAvailableSpecification.java b/solutions/java/src/hotelmanagementsystem/specification/RoomAvailableSpecification.java index cae66e9a..e5d51eb7 100644 --- a/solutions/java/src/hotelmanagementsystem/specification/RoomAvailableSpecification.java +++ b/solutions/java/src/hotelmanagementsystem/specification/RoomAvailableSpecification.java @@ -3,7 +3,7 @@ import hotelmanagementsystem.model.Room; import hotelmanagementsystem.state.AvailableState; -public class RoomAvailableSpecification extends AbstractSpecification { +public class RoomAvailableSpecification implements Specification { @Override public boolean isSatisfiedBy(Room item) { return item.getState() instanceof AvailableState; diff --git a/solutions/java/src/hotelmanagementsystem/specification/RoomStyleSpecification.java b/solutions/java/src/hotelmanagementsystem/specification/RoomStyleSpecification.java index 33067baa..9622579c 100644 --- a/solutions/java/src/hotelmanagementsystem/specification/RoomStyleSpecification.java +++ b/solutions/java/src/hotelmanagementsystem/specification/RoomStyleSpecification.java @@ -3,7 +3,7 @@ import hotelmanagementsystem.enums.RoomStyle; import hotelmanagementsystem.model.Room; -public class RoomStyleSpecification extends AbstractSpecification { +public class RoomStyleSpecification implements Specification { private final RoomStyle style; public RoomStyleSpecification(RoomStyle style) { diff --git a/solutions/java/src/hotelmanagementsystem/specification/RoomTypeSpecification.java b/solutions/java/src/hotelmanagementsystem/specification/RoomTypeSpecification.java index 1f2fba2a..1bf0b0f8 100644 --- a/solutions/java/src/hotelmanagementsystem/specification/RoomTypeSpecification.java +++ b/solutions/java/src/hotelmanagementsystem/specification/RoomTypeSpecification.java @@ -3,7 +3,7 @@ import hotelmanagementsystem.enums.RoomType; import hotelmanagementsystem.model.Room; -public class RoomTypeSpecification extends AbstractSpecification { +public class RoomTypeSpecification implements Specification { private final RoomType type; public RoomTypeSpecification(RoomType type) { diff --git a/solutions/java/src/hotelmanagementsystem/specification/Specification.java b/solutions/java/src/hotelmanagementsystem/specification/Specification.java index 65a41788..4a3c8409 100644 --- a/solutions/java/src/hotelmanagementsystem/specification/Specification.java +++ b/solutions/java/src/hotelmanagementsystem/specification/Specification.java @@ -2,5 +2,15 @@ public interface Specification { boolean isSatisfiedBy(T item); - Specification and(Specification other); + default Specification and(Specification other) { + return item -> this.isSatisfiedBy(item) && other.isSatisfiedBy(item); + } + + default Specification or(Specification other) { + return item -> this.isSatisfiedBy(item) || other.isSatisfiedBy(item); + } + + default Specification not() { + return item -> !this.isSatisfiedBy(item); + } }