parent
410c342102
commit
47d2a97cdd
@ -0,0 +1,10 @@ |
||||
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
||||
<PropertyGroup> |
||||
<OutputType>Exe</OutputType> |
||||
<TargetFramework>net7.0</TargetFramework> |
||||
<ImplicitUsings>enable</ImplicitUsings> |
||||
<Nullable>enable</Nullable> |
||||
</PropertyGroup> |
||||
|
||||
</Project> |
@ -0,0 +1,53 @@ |
||||
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 |
||||
// }; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,47 @@ |
||||
namespace StateMachine.Model |
||||
{ |
||||
public class Order |
||||
{ |
||||
public OrderState State { get; private set; } |
||||
|
||||
public Order() |
||||
{ |
||||
State = OrderState.New; |
||||
} |
||||
|
||||
public void ProcessOrder() |
||||
{ |
||||
switch (State) |
||||
{ |
||||
case OrderState.New: |
||||
State = OrderState.Processing; |
||||
System.Console.WriteLine("Order is being processed."); |
||||
break; |
||||
case OrderState.Processing: |
||||
State = OrderState.Shipped; |
||||
System.Console.WriteLine("Order has been shipped."); |
||||
break; |
||||
case OrderState.Shipped: |
||||
State = OrderState.Delivered; |
||||
System.Console.WriteLine("Order has been delivered."); |
||||
break; |
||||
default: |
||||
System.Console.WriteLine("Invalid state transition."); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
public void CancelOrder() |
||||
{ |
||||
if (State != OrderState.Delivered && State != OrderState.Canceled) |
||||
{ |
||||
State = OrderState.Canceled; |
||||
System.Console.WriteLine("Order has been canceled."); |
||||
} |
||||
else |
||||
{ |
||||
System.Console.WriteLine("Unable to cancel order in this state."); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
namespace StateMachine.Model |
||||
{ |
||||
public enum OrderState |
||||
{ |
||||
New, |
||||
Processing, |
||||
Shipped, |
||||
Delivered, |
||||
Canceled |
||||
} |
||||
} |
@ -0,0 +1,10 @@ |
||||
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
||||
<PropertyGroup> |
||||
<OutputType>Exe</OutputType> |
||||
<TargetFramework>net7.0</TargetFramework> |
||||
<ImplicitUsings>enable</ImplicitUsings> |
||||
<Nullable>enable</Nullable> |
||||
</PropertyGroup> |
||||
|
||||
</Project> |
Loading…
Reference in new issue