← Back to Week 5
Workbook 4a Module 2 — Exercise 1

Hotel Operations — Create Classes

Build the Room, Reservation, and Employee classes  |  Workbook p.28–30

The Exercise

Create a new Maven project called hotel-operations. Inside it, build three classes that model parts of a hotel: a Room, a Reservation, and an Employee.

Each class follows the same recipe: private fields, a constructor, and getters (and setters where the workbook says so). Some getters just return a stored field; others calculate a value from other fields — those are derived getters, and they don't need a backing field.

Once all three classes compile, create one of each in Main and print their info to confirm the values come out right.

Class Specs
Room
Knows everything about a hotel room.
  • getNumberOfBeds()Returns how many beds are in the room.
  • getPrice()Returns the nightly price for this room.
  • isOccupied()booleanTrue if a guest is currently in the room.
  • isDirty()booleanTrue if the room hasn't been cleaned since last use.
  • isAvailable()booleanDerivedA room is available when it is clean AND not occupied. No backing field — calculate from the others.
Reservation
Stores information about a guest stay.
  • getRoomType() / setRoomType(String)Either "king" or "double".
  • getNumberOfNights() / setNumberOfNights(int)How many nights the guest is staying.
  • isWeekend() / setIsWeekend(boolean)booleanTrue if the reservation falls on a weekend.
  • getPrice()DerivedPer-night price based on room type, with a 10% surcharge if it's a weekend.
  • getReservationTotal()DerivedTotal cost for the entire stay.
Room TypeWeekday PriceWeekend Price (+10%)
king$139.00$152.90
double$124.00$136.40
Employee
Payroll information for a hotel staff member.
  • employeeId, name, department, payRate, hoursWorkedPrivate fields. Add a constructor and standard getters.
  • getRegularHours()DerivedHours up to the regular-time cap (no overtime portion).
  • getOvertimeHours()DerivedHours past the regular-time cap (only the overtime portion).
  • getTotalPay()DerivedCombines regular pay and overtime pay using payRate.
Concepts You'll Use
Flow
New Maven project
Build Room.java
Build Reservation.java
Build Employee.java
Test in Main
Commit & push
Tip
Every get-style method that calculates instead of just reading a stored field is a derived getter. The workbook tells you which ones are derived — for those, don't add a backing field. Computing from existing fields keeps the data in one place and prevents the two values from disagreeing.

Workbook 4a Module 2, p.28–30 — Exercise 1: Hotel Operations

← Hotel Responsibilities Hotel Operations 2 →