-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemote Control Car
More file actions
31 lines (25 loc) · 1.04 KB
/
Remote Control Car
File metadata and controls
31 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
defmodule RemoteControlCar do
# Please implement the struct with the specified fields
@enforce_keys [:nickname]
defstruct [:nickname, battery_percentage: 100, distance_driven_in_meters: 0]
def new() do
# Please implement the new/0 function
%RemoteControlCar{nickname: "none"}
end
def new(nickname) do
# Please implement the new/1 function
%RemoteControlCar{nickname: nickname}
end
def display_distance(%RemoteControlCar{} = remote_car) do
# Please implement the display_distance/1 function
"#{remote_car.distance_driven_in_meters} meters"
end
def display_battery(%RemoteControlCar{battery_percentage: percentage} = remote_car) do
# Please implement the display_battery/1 function
if percentage != 0, do: "Battery at #{percentage}%", else: "Battery empty"
end
def drive(%RemoteControlCar{battery_percentage: battery} = remote_car) do
# Please implement the drive/1 function
if (battery != 0) , do: %{remote_car | distance_driven_in_meters: 20 , battery_percentage: 99 } , else: remote_car
end
end