442. Design Device Manager With Message Capabilities
Asked in
Design Device Manager With Message Capabilities

Design a system for managing devices with a common power state and optional message capabilities.

Every device records whether it is plugged in and its current charging percentage. A device may support a display, a speaker, both capabilities, or neither capability.

When a message is sent to a device, the system must produce an output for every supported message capability.

Class

DeviceCapabilityManager

Constructor

DeviceCapabilityManager

public DeviceCapabilityManager()

  • Creates an empty device capability manager.

Methods

registerDevice

public void registerDevice(String deviceId, boolean pluggedIn, int chargingPercentage, List<String> capabilities)

  • Registers a device using the supplied unique deviceId.
  • pluggedIn indicates whether the device is currently connected to a power source.
  • chargingPercentage is the device's current charge from 0 to 100.
  • capabilities may contain "DISPLAY", "SPEAKER", both values, or no values.

updatePowerStatus

public void updatePowerStatus(String deviceId, boolean pluggedIn, int chargingPercentage)

  • Updates the current power information for the specified device.
  • The device's capabilities remain unchanged.

isPluggedIn

public boolean isPluggedIn(String deviceId)

  • Returns true if the specified device is currently plugged in; otherwise, returns false.

getChargingPercentage

public int getChargingPercentage(String deviceId)

  • Returns the current charging percentage of the specified device.

sendMessage

public List<String> sendMessage(String deviceId, String message)

  • Sends message through every capability supported by the device.
  • For a display, include "DISPLAY,message" in the returned list.
  • For a speaker, include "SPEAKER,message" in the returned list.
  • Always place the display output before the speaker output, regardless of the order in which the capabilities were registered.
  • Returns an empty list if the device has neither a display nor a speaker.
  • A device's power information does not affect sendMessage. Message outputs depend only on its registered capabilities.

Rules

  • Every registered device supports the common power operations.
  • The display and speaker capabilities are independent.
  • A message must only be sent through capabilities supported by the device.
  • The text of a message must not be changed.
  • Calling updatePowerStatus replaces the previous plugged-in state and charging percentage.
  • The returned message records represent the performed actions; no real display or audio hardware is required.

Constraints

  • 1 ≤ deviceId.length() ≤ 50
  • Every deviceId is non-blank.
  • Each device is registered exactly once.
  • 0 ≤ chargingPercentage ≤ 100
  • 0 ≤ capabilities.size() ≤ 2
  • Every value in capabilities is either "DISPLAY" or "SPEAKER".
  • Capability values for one device are unique.
  • 1 ≤ message.length() ≤ 1,000
  • At most 10,000 devices will be registered.
  • At most 100,000 method calls will be made.
  • Every deviceId passed to a method other than registerDevice identifies a registered device.

Examples

Example 1

DeviceCapabilityManager manager = new DeviceCapabilityManager()

manager.registerDevice(deviceId = "travel-tablet", pluggedIn = false, chargingPercentage = 58, capabilities = List.of("SPEAKER", "DISPLAY"))

Output: No value is returned.

manager.isPluggedIn(deviceId = "travel-tablet")

Output: false

manager.getChargingPercentage(deviceId = "travel-tablet")

Output: 58

manager.sendMessage(deviceId = "travel-tablet", message = "Navigation ready")

Output: List.of("DISPLAY,Navigation ready", "SPEAKER,Navigation ready")

The tablet supports both message capabilities. Display output appears first even though the speaker capability was registered first.

Example 2

DeviceCapabilityManager manager = new DeviceCapabilityManager()

manager.registerDevice(deviceId = "kitchen-panel", pluggedIn = true, chargingPercentage = 91, capabilities = List.of("DISPLAY"))

Output: No value is returned.

manager.sendMessage(deviceId = "kitchen-panel", message = "Timer finished")

Output: List.of("DISPLAY,Timer finished")

Only a display record is returned because the device does not support a speaker.

Example 3

DeviceCapabilityManager manager = new DeviceCapabilityManager()

manager.registerDevice(deviceId = "audio-pod", pluggedIn = true, chargingPercentage = 73, capabilities = List.of("SPEAKER"))

Output: No value is returned.

manager.sendMessage(deviceId = "audio-pod", message = "Connection complete")

Output: List.of("SPEAKER,Connection complete")

manager.updatePowerStatus(deviceId = "audio-pod", pluggedIn = false, chargingPercentage = 69)

Output: No value is returned.

manager.isPluggedIn(deviceId = "audio-pod")

Output: false

manager.getChargingPercentage(deviceId = "audio-pod")

Output: 69

Example 4

DeviceCapabilityManager manager = new DeviceCapabilityManager()

manager.registerDevice(deviceId = "sensor-node", pluggedIn = false, chargingPercentage = 27, capabilities = List.of())

Output: No value is returned.

manager.sendMessage(deviceId = "sensor-node", message = "Reading recorded")

Output: List.of()

The device supports neither message capability, so no output record is produced.



Please use Laptop/Desktop or any other large screen to add/edit code.