Skip to content
Java project #10Intermediate

Hotel Reservation System

Book rooms for date ranges without double-booking, with weekend pricing, cancellations, availability searches and occupancy reports built on java.time.

Estimated time
~5 hours to build
Files
5 files · 333 lines
Technologies
Java 21, java.time (LocalDate, ChronoUnit, DayOfWeek), Enums with data, Records, Streams

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

Model a hotel with single, double and suite rooms. Users can search for free rooms of a type between two dates, book the lowest-numbered free room, cancel bookings, see who is staying on a date and the occupancy percentage, and list all bookings with revenue. Friday and Saturday nights cost 20% more. Reject unknown room types, check-out dates on or before check-in, stays over 30 nights and invalid dates.

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

TextProject tree
hotel-reservation-system/
├── README.md
├── src/Hotel.java
├── src/Main.java
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

src/Hotel.java

83 lines
Javasrc/Hotel.java
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
/** Rooms, bookings and the rules that connect them: no overlapping stays in the same room. */
public class Hotel {
public enum RoomType {
SINGLE(2_500), DOUBLE(4_000), SUITE(9_000);
final long nightlyRupees;
RoomType(long nightlyRupees) { this.nightlyRupees = nightlyRupees; }
}
public record Room(int number, RoomType type) { }
public record Booking(int id, String guest, Room room, LocalDate checkIn, LocalDate checkOut) {
long nights() { return ChronoUnit.DAYS.between(checkIn, checkOut); }
// Weekend nights (Friday and Saturday) cost 20% more.
long cost() {
long total = 0;
for (LocalDate d = checkIn; d.isBefore(checkOut); d = d.plusDays(1)) {
boolean weekend = switch (d.getDayOfWeek()) { case FRIDAY, SATURDAY -> true; default -> false; };
total += weekend ? room.type().nightlyRupees * 12 / 10 : room.type().nightlyRupees;
}
return total;
}
// Half-open ranges [checkIn, checkOut): a guest can check in on the day another checks out.
boolean overlaps(LocalDate in, LocalDate out) { return in.isBefore(checkOut) && checkIn.isBefore(out); }
}
private final Map<Integer, Room> rooms = new TreeMap<>();
private final List<Booking> bookings = new ArrayList<>();
private int nextId = 1;
public void addRoom(int number, RoomType type) { rooms.put(number, new Room(number, type)); }
public List<Room> available(RoomType type, LocalDate in, LocalDate out) {
validate(in, out);
return rooms.values().stream()
.filter(r -> r.type() == type)
.filter(r -> bookings.stream().noneMatch(b -> b.room().equals(r) && b.overlaps(in, out)))
.toList();
}
/** Books the lowest-numbered free room of the requested type. */
public Booking book(String guest, RoomType type, LocalDate in, LocalDate out) {
List<Room> free = available(type, in, out);
if (free.isEmpty()) throw new IllegalStateException("no " + type + " room is free from " + in + " to " + out);
Booking b = new Booking(nextId++, guest, free.get(0), in, out);
bookings.add(b);
return b;
}
public Booking cancel(int id) {
Booking b = find(id).orElseThrow(() -> new IllegalArgumentException("no booking #" + id));
bookings.remove(b);
return b;
}
public Optional<Booking> find(int id) { return bookings.stream().filter(b -> b.id() == id).findFirst(); }
public List<Booking> onDate(LocalDate day) {
return bookings.stream().filter(b -> !day.isBefore(b.checkIn()) && day.isBefore(b.checkOut()))
.sorted(Comparator.comparingInt(b -> b.room().number())).toList();
}
public double occupancy(LocalDate day) { return rooms.isEmpty() ? 0 : 100.0 * onDate(day).size() / rooms.size(); }
public List<Booking> all() { return bookings.stream().sorted(Comparator.comparing(Booking::checkIn).thenComparingInt(Booking::id)).toList(); }
private static void validate(LocalDate in, LocalDate out) {
if (!out.isAfter(in)) throw new IllegalArgumentException("check-out must be after check-in");
if (ChronoUnit.DAYS.between(in, out) > 30) throw new IllegalArgumentException("stays are limited to 30 nights");
}
}

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

Terminal
javac -d out $(find src -name "*.java")
# Windows PowerShell: javac -d out (Get-ChildItem -Recurse src -Filter *.java).FullName

Run

Terminal
java -cp out Main

Test against the expected output

Terminal
java -cp out Main < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt
# Windows PowerShell:
# Get-Content tests\sample-input.txt | java -cp out Main > actual.txt

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== Hotel Reservations === 3 single (Rs 2,500), 2 double (Rs 4,000), 1 suite (Rs 9,000); +20% Fri/Sat nights
>
Booking #1: Asha in room 301, 2026-12-24 to 2026-12-27 (3 nights) Rs 30,600
>
Error: no SUITE room is free from 2026-12-26 to 2026-12-28
>
Booking #2: Ravi in room 301, 2026-12-27 to 2026-12-29 (2 nights) Rs 18,000
>
Booking #3: Meera in room 201, 2026-12-23 to 2026-12-26 (3 nights) Rs 12,800
>
Booking #4: John in room 202, 2026-12-24 to 2026-12-25 (1 night) Rs 4,000
>
Error: no DOUBLE room is free from 2026-12-24 to 2026-12-26
>
No double rooms free.
>
Free: [101, 102, 103]
>
Error: room types are single, double and suite
>
Error: check-out must be after check-in
>
Error: stays are limited to 30 nights
>
Error: dates must be valid and written as YYYY-MM-DD
>
Cancelled #4 (John, room 202)
>
Free: [202]
>
2026-12-24 (THURSDAY): 33% occupied
room 201: Meera
room 301: Asha
>
2026-12-27 (SUNDAY): 17% occupied
room 301: Ravi
>
#3 Meera room 201 2026-12-23 -> 2026-12-26 Rs 12,800
#1 Asha room 301 2026-12-24 -> 2026-12-27 Rs 30,600
#2 Ravi room 301 2026-12-27 -> 2026-12-29 Rs 18,000
Total booked revenue: Rs 61,400
>
Input
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.txt and compare with tests/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

  1. 1Allow moving a booking to another room.
  2. 2Report monthly occupancy for a whole year.
  3. 3Offer the cheapest room type available when the requested one is full.

Built and tested it yourself? Mark it to update your dashboard.