Introduction
Reservation systems all revolve around one question: does a new stay overlap an existing one? Modelling stays as half-open date ranges makes the answer a single expression, and java.time.LocalDate takes care of calendars, leap years and weekdays.
Problem statement
Objectives
- Use LocalDate, ChronoUnit and DayOfWeek
- Implement interval overlap correctly with half-open ranges
- Combine enums, records and streams in a service class
- Handle parsing errors from java.time
Features
- Overlap-free room assignment
- Weekend surcharge per night
- Availability search and cancellation
- Daily occupancy and revenue listing
Concepts demonstrated
- Date arithmetic
- Interval overlap
- Service classes
- Validation
- Switch expressions on enums
System architecture
Hotel owns the rooms (a TreeMap by number) and a list of Booking records. A booking covers [checkIn, checkOut), so two stays overlap exactly when in < otherOut && otherIn < out; this lets one guest check in on the day another checks out. available filters rooms of a type with no overlapping booking; book takes the first. Booking.cost walks each night and applies the weekend rate. Main parses commands and converts DateTimeParseException into friendly errors.
Folder structure
Complete source code
src/Hotel.java
What each file does
- src/Hotel.java
- Room types, the Room and Booking records, overlap logic, pricing and the reservation service.
- src/Main.java
- Sets up the rooms and runs the command loop.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
book Asha suite 2026-12-24 2026-12-27 book Ravi suite 2026-12-26 2026-12-28 book Ravi suite 2026-12-27 2026-12-29 book Meera double 2026-12-23 2026-12-26 book John double 2026-12-24 2026-12-25 book Lina double 2026-12-24 2026-12-26 free double 2026-12-24 2026-12-25 free single 2026-12-24 2026-12-25 book Omar penthouse 2026-12-24 2026-12-25 book Omar single 2026-12-25 2026-12-24 book Omar single 2026-12-30 2027-02-10 book Omar single 2026-02-30 2026-03-02 cancel 4 free double 2026-12-24 2026-12-25 day 2026-12-24 day 2026-12-27 bookings quit
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - Asha's suite from 24 to 27 December 2026 covers Thursday, Friday and Saturday nights: 9,000 + 10,800 + 10,800 = ₹30,600.
- Ravi can't book 26–28 December but can book 27–29 December, starting the day Asha leaves.
- 2026-02-30 is rejected as an invalid date.
Common errors
Back-to-back stays are reported as clashes
Treat the check-out day as exclusive: compare with isBefore, not !isAfter.
DateTimeParseException crashes the program
Catch it around LocalDate.parse and report the expected format.
Possible improvements
- Add guests' contact details and a search by name
- Support seasonal prices from a table
- Persist bookings to a file
Additional challenges
- 1Allow moving a booking to another room.
- 2Report monthly occupancy for a whole year.
- 3Offer the cheapest room type available when the requested one is full.
Built and tested it yourself? Mark it to update your dashboard.