exercism/elixir/remote-control-car/lib/remote_control_car.ex

28 lines
902 B
Elixir

defmodule RemoteControlCar do
@enforce_keys [:nickname]
defstruct [:nickname, battery_percentage: 100, distance_driven_in_meters: 0]
def new(nickname \\ "none"), do: %__MODULE__{nickname: nickname}
def display_distance(%__MODULE__{distance_driven_in_meters: distance_driven_in_meters} = _remote_car) do
"#{distance_driven_in_meters} meters"
end
def display_battery(%__MODULE__{battery_percentage: 0} = _remote_car) do
"Battery empty"
end
def display_battery(%__MODULE__{battery_percentage: battery_percentage} = _remote_car) do
"Battery at #{battery_percentage}%"
end
def drive(%__MODULE__{battery_percentage: 0} = remote_car), do: remote_car
def drive(%__MODULE__{} = remote_car) do
%{
remote_car |
battery_percentage: remote_car.battery_percentage - 1,
distance_driven_in_meters: remote_car.distance_driven_in_meters + 20,
}
end
end