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

hotel-operations 3: Overload Methods

Two ways to clock in — plus a brand new Hotel class with derived getters  |  Workbook p.45-46

The Exercise

You already wrote punchIn(int time) and punchOut(int time) in the last exercise. Sometimes, though, an employee just wants to punch the clock right now — not pass an integer. Add overloaded versions with no parameters that figure out the time using LocalDateTime.now(). After this step, Employee should have four punch methods: two with a time argument and two without.

Then build a brand new Hotel class. A hotel knows its name, how many suites and basic rooms it has, and how many of each are currently booked. The booked counts have no public setters — the only way they change is through bookRoom(...). The two getters for available rooms don't store anything; they calculate the answer from total minus booked. That is a derived getter.

You also need two overloaded constructors: one for a brand-new hotel where booked counts default to zero, and one for an existing hotel where you want to set every value yourself.

What's Being Added

Employee (overloads)

  • punchIn() new
  • punchOut() new
Use LocalDateTime.now() — no input parameter.

Hotel (new class)

  • 5 private fields
  • 2 overloaded constructors
  • bookRoom(int, boolean)
  • getAvailableSuites() derived
  • getAvailableRooms() derived
Two Ways to Clock In
With a time argument
employee.punchIn(9);
Caller decides the hour. Useful when entering historical timesheets.
No argument — uses current time
employee.punchIn();
Reads LocalDateTime.now() internally. The most common case — punching the clock right now.

Both methods share the same name. Java picks the right one by looking at the arguments. That's method overloading.

About the Hotel Constructors
Hotel(name, suites, rooms) — use this when opening a brand-new hotel. bookedSuites and bookedBasicRooms default to 0.
Hotel(name, suites, rooms, bookedSuites, bookedBasicRooms) — use this when restoring an existing hotel where some rooms are already booked.

Same class name, different parameter lists — constructor overloading. Java picks the right one by counting the arguments.

Concepts You'll Use
Flow
1Employee: add punchIn() — LocalDateTime.now()
2Add punchOut() the same way
3Create Hotel.java
4Add 5 private fields, no public setters for booked counts
53-param constructor (booked = 0)
65-param constructor (all explicit)
7Add bookRoom(int, boolean)
8Derived getters — no backing field
9In Main: create hotel, book rooms, print availability
Tip — Derived Getters
No backing field. getAvailableSuites() should not store its answer anywhere. It computes it: numberOfSuites - bookedSuites. Same for getAvailableRooms(). This is exactly the pattern from Visual #3 — the truth lives in the booked count, and "available" is always a fresh calculation.

Workbook 4a, p.45-46 — Module 2, Exercise 3: hotel-operations (Overload Methods)

← hotel-operations 2: Add Methods hotel-operations Unit Tests →