The goal of this task is to create a class, PowerStation, that simulates an uninterruptible power
supply. This power station manages the battery charge and discharge, power input and output limits, and displays
status updates. It can also function as a power throughput, allowing simultaneous charging of its own battery and
powering of connected devices from the external source. You'll implement the class following the specified
interface, ensuring it meets the functionality and handles various edge cases.
The PowerStation class should manage the following:
Please submit your solution in a .txt text file. Name the file using the following format: your_name.txt.
For example, if your name is John Doe, the file name should be: john_doe.txt
Please copy this code and implement your solution:
class PowerStation {
constructor(batteryCapacity, maximumInput, maximumOutput) {
// Implement constructor
}
updateInput(voltage, current) {
// Implement this and other methods and getters
}
connectOutput(outputId) {
}
updateOutput(outputId, voltage, current) {
}
disconnectOutput(outputId) {
}
updateBatteryLevel(capacityLeft) {
}
get batteryPercentage() {
}
get totalOutputPower() {
}
get timeRemaining() {
}
get status() {
}
}
You don't need to export the class from the file. Keep the class name, method names, argument order, and constructor interface unchanged. The tests will evaluate your implementation by simulating various usage scenarios. Only use built-in JavaScript methods; any code relying on third-party libraries will fail the tests. Below is a detailed description of the required implementation.
Below are the specifications for each required method and getter. Unless specified otherwise, assume all input values are valid so the negative or non-numeric values will not be tested.
constructor(batteryCapacity, maximumInput, maximumOutput) // e.g., new PowerStation(2000, 500, 800)
Parameters:
batteryCapacity (number): Maximum battery capacity in watt-hours (Wh), e.g., 2000maximumInput (number): Maximum allowable input power in watts (W), e.g, 500maximumOutput (number): Maximum allowable output power in watts (W), e.g, 800Battery should be at its full capacity (charged to 100%) at the initial state.
Each power station may need to be charged from an external source, such as the grid or solar panels. This method updates the device's input state. See how to calculate power based on this data here.
updateInput(voltage, current) // e.g., updateInput(220, 2)
Parameters:
voltage (number): Voltage of the input source in volts.current (number): Current of the input source in amps.Devices like mobile phones and household appliances can be connected to the Power Station outputs. This method is called when a new device is being connected. See how to calculate power based on this data here.
connectOutput(outputId) // e.g., connectOutput("usb_1")
Parameters:
outputId (string): Unique identifier for the output to which the devices are being connected.
You may need to track which devices are connected to manage output power accurately.
Assume that outputId is always unique, and the same output will not be connected multiple times. You
don't have to worry about the number of connected devices.
Connected devices consume varying amounts of power during operation; for instance, a washing machine consumes more energy when heating water.
updateOutput(outputId, voltage, current) // e.g., updateOutput("usb_1", 5, 1.2)
Parameters:
outputId (string): Identifier for the output device.
voltage (number): The voltage of the output device is in volts.
current (number): Current of the output device in amps.
This method updates the singular output's power consumption. See how to calculate power based on this data here.
Assume that outputId is always valid and connected.
disconnectOutput(outputId) // e.g., disconnectOutput("usb_1")
Parameters:
outputId (string): Identifier for the output device being disconnected.This method disconnects an output device from the power station. Ensure that any power calculations reflect the device's disconnection.
updateBatteryLevel(capacityLeft) // e.g., updateBatteryLevel(875)
The device battery can be charged or discharged. You don't need to calculate its current capacity manually; this method will trigger automatically and handle that.
Parameters:
capacityLeft (number): The remaining capacity in watt-hours (Wh).
This should directly impact the battery percentage and status.
get batteryPercentage() // Number (e.g., 99.9)
Returns the current battery level as a percentage of battery capacity, rounded to 1 decimal place.
Example
For a Power Station with a battery capacity of 500 Wh, at the capacity level of 376 Wh, it
should return 75.2
get totalOutputPower() // Number (e.g., 250)
Returns the sum of all output powers in watts, rounded to the nearest integer. If no devices are using power, this should return 0. Should return number.
get timeRemaining() // String (e.g., "01:30")
Returns the estimated time remaining either until the battery is fully charged or fully discharged in HH:MM format, depending on the current battery state:
The returned value is based on the relationship between battery capacity and power consumption/charging rate. See time calculation for more details.
Hint: The totalOutputPower returns a rounded number, suitable for display purposes. However, the
actual consumption may not be an integer number, which can impact the calculation of the remaining time.
get status() // String (e.g., "charging")
Returns the current status of the power station.
Required values:
"charging": When input power is active and enough to charge the battery.
"discharging": When the output power is active and a battery is used.
"idle": No input or output power goes through the battery. (When the input power equals
the output power).
"overload": When either input or output power exceeds the limits (takes priority over
all other statuses).
These formulas should cover most of the requirements for this task without needing deep electronics knowledge.
Power (in watts) is calculated using voltage (V) and current (A):
Example: If a phone charger uses 5 volts and 2 amps, its power is
Time (in hours) is calculated using energy capacity (Wh) and power (W)
Example: If you connect a device that consumes 100 W to a 200 Wh battery, the charge will last for
A brief example of how the class might be instantiated and used:
const station = new PowerStation(500, 200, 150);
station.connectOutput("lamp_1");
station.updateOutput("lamp_1", 12, 5); // Updates power consumption for the lamp
station.updateBatteryLevel(496); // Updates battery capacity
console.log(station.batteryPercentage); // Displays current battery level 99.2
console.log(station.timeRemaining); // Displays the remaining time 08:16
Good luck with implementing your PowerStation class! Consider all edge cases and ensure your methods work together to maintain an accurate state at all times.