Initial upload
This commit is contained in:
24
elixir/need-for-speed/lib/need_for_speed/race.ex
Normal file
24
elixir/need-for-speed/lib/need_for_speed/race.ex
Normal file
@@ -0,0 +1,24 @@
|
||||
defmodule NeedForSpeed.Race do
|
||||
defstruct [
|
||||
:title,
|
||||
:total_distance_in_meters,
|
||||
cars: []
|
||||
]
|
||||
|
||||
def display_status(%NeedForSpeed.Race{} = race) do
|
||||
cond do
|
||||
Enum.any?(race.cars, &(&1.distance_driven_in_meters >= race.total_distance_in_meters)) ->
|
||||
"Finished"
|
||||
|
||||
Enum.any?(race.cars, &(&1.distance_driven_in_meters > 0)) ->
|
||||
"In Progress"
|
||||
|
||||
true ->
|
||||
"Not Started"
|
||||
end
|
||||
end
|
||||
|
||||
def display_distance(%NeedForSpeed.Race{total_distance_in_meters: d}) do
|
||||
"#{d} meters"
|
||||
end
|
||||
end
|
@@ -0,0 +1,24 @@
|
||||
defmodule NeedForSpeed.RemoteControlCar do
|
||||
defstruct [
|
||||
:nickname,
|
||||
:color,
|
||||
battery_percentage: 100,
|
||||
distance_driven_in_meters: 0
|
||||
]
|
||||
|
||||
def new(color, nickname) when color in [:red, :blue, :green] do
|
||||
%NeedForSpeed.RemoteControlCar{nickname: nickname, color: color}
|
||||
end
|
||||
|
||||
def display_distance(%NeedForSpeed.RemoteControlCar{distance_driven_in_meters: d}) do
|
||||
"#{d} meters"
|
||||
end
|
||||
|
||||
def display_battery(%NeedForSpeed.RemoteControlCar{battery_percentage: 0}) do
|
||||
"Battery empty"
|
||||
end
|
||||
|
||||
def display_battery(%NeedForSpeed.RemoteControlCar{battery_percentage: b}) do
|
||||
"Battery at #{b}%"
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user