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.
DeviceCapabilityManager
public DeviceCapabilityManager()
public void registerDevice(String deviceId, boolean pluggedIn, int chargingPercentage, List<String> capabilities)
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.public void updatePowerStatus(String deviceId, boolean pluggedIn, int chargingPercentage)
public boolean isPluggedIn(String deviceId)
true if the specified device is currently plugged in; otherwise, returns false.public int getChargingPercentage(String deviceId)
public List<String> sendMessage(String deviceId, String message)
message through every capability supported by the device."DISPLAY,message" in the returned list."SPEAKER,message" in the returned list.sendMessage. Message outputs depend only on its registered capabilities.updatePowerStatus replaces the previous plugged-in state and charging percentage.1 ≤ deviceId.length() ≤ 50deviceId is non-blank.0 ≤ chargingPercentage ≤ 1000 ≤ capabilities.size() ≤ 2capabilities is either "DISPLAY" or "SPEAKER".1 ≤ message.length() ≤ 1,00010,000 devices will be registered.100,000 method calls will be made.deviceId passed to a method other than registerDevice identifies a registered device.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.
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.
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
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.