You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.4 KiB
53 lines
1.4 KiB
1 year ago
|
namespace CarStateMachine.Model
|
||
|
{
|
||
|
public class Car
|
||
|
{
|
||
|
public enum State
|
||
|
{
|
||
|
Stopped,
|
||
|
Started,
|
||
|
Running,
|
||
|
}
|
||
|
|
||
|
public enum Action
|
||
|
{
|
||
|
Stop,
|
||
|
Start,
|
||
|
Accelerate,
|
||
|
}
|
||
|
|
||
|
private State _state = State.Stopped;
|
||
|
public State CurrentState { get { return _state; } }
|
||
|
|
||
|
public void TakeAction(Action action)
|
||
|
{
|
||
|
State state = _state;
|
||
|
switch (_state, action)
|
||
|
{
|
||
|
case (State.Stopped, Action.Start):
|
||
|
state = State.Started;
|
||
|
break;
|
||
|
case (State.Started, Action.Accelerate):
|
||
|
state = State.Running;
|
||
|
break;
|
||
|
case (State.Started, Action.Stop):
|
||
|
state = State.Stopped;
|
||
|
break;
|
||
|
case (State.Running, Action.Stop):
|
||
|
state = State.Stopped;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
_state = state;
|
||
|
|
||
|
// _state = (_state, action) switch
|
||
|
// {
|
||
|
// (State.Stopped, Action.Start) => State.Started,
|
||
|
// (State.Started, Action.Accelerate) => State.Running,
|
||
|
// (State.Started, Action.Stop) => State.Stopped,
|
||
|
// (State.Running, Action.Stop) => State.Stopped,
|
||
|
// _ => _state
|
||
|
// };
|
||
|
}
|
||
|
}
|
||
|
}
|