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

hotel-operations 2: Add Methods

Give your Room and Employee classes real behavior — not just data  |  Workbook p.36

The Exercise

You already built the Room and Employee classes in the previous exercise — they hold data (fields and getters/setters), but they don't do anything yet. Now it's time to add behavior.

A real hotel room has a lifecycle: a guest checks in, the room becomes occupied and starts to get dirty; when the guest checks out, the room is empty but still dirty; only after housekeeping cleans it can a new guest check in. Your Room class needs methods that model that flow.

An Employee punches a time card. When they punch in, you store their start time. When they punch out, you compute how many hours they worked and add it to hoursWorked. Time is given as an int in 24-hour format — 10 means 10:00 AM, 14 means 2:00 PM.

Bonus: Replace the two punch methods with a single punchTimeCard that figures out from the current state whether the employee is punching in or out, and acts accordingly.

What's Being Added

Room

  • checkIn()
  • checkout()
  • cleanRoom()

Employee

  • punchIn(int time)
  • punchOut(int time)
Bonus: a single punchTimeCard method that replaces both
Expected Behavior — Room Lifecycle
Start
empty
clean
checkIn()
In use
occupied
dirty
checkout()
Vacated
empty
still dirty
cleanRoom()
Available
empty
clean

The room is still dirty after checkout — housekeeping has to clean it before the next guest can check in.

Concepts You'll Use
Flow
1Open existing Room class
2Add checkIn() — occupied=true, dirty=true
3Add checkout() — occupied=false, dirty stays
4Add cleanRoom() — only if unoccupied
5Open Employee, add punchIn(int)
6Add punchOut(int) — compute hours
7(Bonus) refactor into punchTimeCard
Think About Edge Cases
Worth pausing on: Should you be able to clean a room that's currently occupied? Should you be able to check in to a room that hasn't been cleaned yet? The workbook lists these as questions you should answer in your code — they are decisions, not afterthoughts.

Workbook 4a, p.36 — Module 2, Exercise 2: hotel-operations (Add Methods)

← hotel-operations: Create Classes hotel-operations 3: Overload Methods →