Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import hotelmanagementsystem.model.Room;
import hotelmanagementsystem.state.AvailableState;

public class RoomAvailableSpecification extends AbstractSpecification<Room> {
public class RoomAvailableSpecification implements Specification<Room> {
@Override
public boolean isSatisfiedBy(Room item) {
return item.getState() instanceof AvailableState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import hotelmanagementsystem.enums.RoomStyle;
import hotelmanagementsystem.model.Room;

public class RoomStyleSpecification extends AbstractSpecification<Room> {
public class RoomStyleSpecification implements Specification<Room> {
private final RoomStyle style;

public RoomStyleSpecification(RoomStyle style) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import hotelmanagementsystem.enums.RoomType;
import hotelmanagementsystem.model.Room;

public class RoomTypeSpecification extends AbstractSpecification<Room> {
public class RoomTypeSpecification implements Specification<Room> {
private final RoomType type;

public RoomTypeSpecification(RoomType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,15 @@

public interface Specification<T> {
boolean isSatisfiedBy(T item);
Specification<T> and(Specification<T> other);
default Specification<T> and(Specification<T> other) {
return item -> this.isSatisfiedBy(item) && other.isSatisfiedBy(item);
}

default Specification<T> or(Specification<T> other) {
return item -> this.isSatisfiedBy(item) || other.isSatisfiedBy(item);
}

default Specification<T> not() {
return item -> !this.isSatisfiedBy(item);
}
}