AlgoMaster Logo
AlgoMasterRefactor a Climate Consolemedium

Refactor a Climate Console

medium

Design and refactor a ClimateConsole that manages room temperatures.

Every room starts at 20C and owns a thermostat whose valid range is 16 through 30C. A room may be occupied or empty. Empty rooms refuse temperature changes before the thermostat is consulted.

The console supports:

  • addRoom(name, occupied), which returns the new room id;
  • setOccupied(roomId, occupied), which returns false for an unknown room;
  • requestTemperature(roomId, temperature), which returns "UNKNOWN", "EMPTY_ROOM", "OUT_OF_RANGE", or "SET";
  • temperature(roomId), which returns -1 for an unknown room;
  • status(roomId), which returns "<name>: <temperature>C, OCCUPIED" or "<name>: <temperature>C, EMPTY", and "UNKNOWN" for an invalid id; and
  • roomCount().

The legacy console retrieves a room's thermostat and talks to it directly. That shortcut bypasses the room's occupancy rule. Refactor the code so the console knows rooms, rooms know thermostats, and each layer exposes the operation its caller actually needs.

Example 1:

Input:

Output:

Explanation: The room enforces occupancy. The thermostat handles only its own temperature range and stored value.

Example 2:

Input:

Output:

Constraints

  • 1 <= name.length <= 40
  • -100 <= temperature <= 100
  • At most 100 calls will be made across all methods.

Starter Code

The starter compiles, but the console reaches through each room to its thermostat and can change an empty room.

How the design is graded

needs 7/10 to pass
  • Console talks to rooms

    Full marks when ClimateConsole calls meaningful methods on Room and never retrieves a Thermostat. Lose points heavily for getThermostat, thermostat properties or console code that invokes thermostat methods.

  • Room protects its rule

    Full marks when Room owns the occupancy check and delegates valid temperature changes to its thermostat. Lose points when the console checks occupancy or can bypass EMPTY_ROOM behavior.

  • Complete delegation

    Full marks when Room supplies temperature and status as well as the change command, invalid ids are handled exactly, and failed requests preserve temperature. Lose points for duplicated status formatting or printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new ClimateConsole()null
addRoom("Studio", false)0
requestTemperature(0, 22)"EMPTY_ROOM"
temperature(0)20
setOccupied(0, true)true
requestTemperature(0, 31)"OUT_OF_RANGE"
requestTemperature(0, 25)"SET"
temperature(0)25
status(0)"Studio: 25C, OCCUPIED"

The room blocks changes while empty, then delegates range validation and the valid update to its thermostat.

Run checks these cases. Submit also runs a larger hidden set.