Design a hotel management system. Guests search for rooms by type and dates, book, check in, use services (food, laundry) and check out with a bill.
A hotel has **rooms** of different types (single, double, suite) with prices.
Guests **search** and **book** a room type for a date range.
At **check-in** a specific room is assigned; at **check-out** the bill is produced.
Extra **services** (room service, laundry) are added to the bill during the stay.
A booking can be **cancelled** with a rule-based refund.
Stage 1: Rooms, guests and bookings
Name the things.
A hotel books a kind of room first and a specific room later. `Booking` holds a `RoomType`, and `room` stays empty until check-in assigns a number. Reserving by type lets the hotel move guests between equal rooms until they arrive. Two steps (type then room) cost a little complexity and gain flexibility.
Stage 2: The hotel: search, book, assign
Check availability for a type and dates.
Who counts how many rooms of a type are free on given nights? `Hotel.available` counts rooms of that type minus bookings that overlap, and `book` refuses when the count is zero. `checkIn` picks a free room of the booked type. The count-and-book step is locked per room type, so two guests cannot take the last room. Counting overlapping bookings on every search is fine for one hotel.
Stage 3: Booking states
Each action is allowed only at the right time.
A guest should not check out before checking in or cancel after leaving. `BookingState` with `ReservedState` (can check in or cancel) and `CheckedInState` (can check out). Each refuses the actions that do not fit. Allowed actions come from the object's state, not from flag checks (**State**). Cancelled and checked-out states are omitted for space and follow the same shape.
Stage 4: Bills and refunds
Itemise charges and make the refund rule swappable.
Services and cancellations need clear money rules. A `Bill` holds itemised `Charge`s and totals them. A `RefundPolicy` decides what to give back; `FlexibleRefund` refunds in full up to N days before arrival. Itemised charges make the bill auditable, and refund rules change without touching booking (**Strategy**). Store the amount on each charge when it is added, so later price changes do not rewrite old bills.