syneffort 1 year ago
parent 410c342102
commit 47d2a97cdd
  1. 10
      StateMachine/CarStateMachine/CarStateMachine.csproj
  2. 53
      StateMachine/CarStateMachine/Model/Car.cs
  3. 24
      StateMachine/CarStateMachine/Program.cs
  4. 28
      StateMachine/StateMachine.sln
  5. 47
      StateMachine/StateMachine/Model/Order.cs
  6. 11
      StateMachine/StateMachine/Model/OrderState.cs
  7. 32
      StateMachine/StateMachine/Program.cs
  8. 10
      StateMachine/StateMachine/StateMachine.csproj

@ -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,24 @@
using CarStateMachine.Model;
namespace CarStateMachine;
class Program
{
static void Main(string[] args)
{
Car car = new Car();
System.Console.WriteLine($"State: {car.CurrentState}");
car.TakeAction(Car.Action.Start);
System.Console.WriteLine($"State: {car.CurrentState}");
car.TakeAction(Car.Action.Start);
System.Console.WriteLine($"State: {car.CurrentState}");
car.TakeAction(Car.Action.Accelerate);
System.Console.WriteLine($"State: {car.CurrentState}");
car.TakeAction(Car.Action.Stop);
System.Console.WriteLine($"State: {car.CurrentState}");
}
}

@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StateMachine", "StateMachine\StateMachine.csproj", "{C90D550D-277E-4321-A01E-542FB4B5F57E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarStateMachine", "CarStateMachine\CarStateMachine.csproj", "{F1C27B6E-1E78-40CA-9564-81074AB955B1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C90D550D-277E-4321-A01E-542FB4B5F57E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C90D550D-277E-4321-A01E-542FB4B5F57E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C90D550D-277E-4321-A01E-542FB4B5F57E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C90D550D-277E-4321-A01E-542FB4B5F57E}.Release|Any CPU.Build.0 = Release|Any CPU
{F1C27B6E-1E78-40CA-9564-81074AB955B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F1C27B6E-1E78-40CA-9564-81074AB955B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1C27B6E-1E78-40CA-9564-81074AB955B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1C27B6E-1E78-40CA-9564-81074AB955B1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

@ -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,32 @@
using StateMachine.Model;
namespace StateMachine;
class Program
{
static void Main(string[] args)
{
Order order = new Order();
System.Console.WriteLine($"Initial order state: {order.State}");
// process
order.ProcessOrder();
System.Console.WriteLine($"Current order state: {order.State}");
// ship
order.ProcessOrder();
System.Console.WriteLine($"Current order state: {order.State}");
// deliver
order.ProcessOrder();
System.Console.WriteLine($"Current order state: {order.State}");
// cancel
order.CancelOrder();
System.Console.WriteLine($"Current order state: {order.State}");
order.ProcessOrder();
System.Console.WriteLine($"Current order state: {order.State}");
}
}

@ -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…
Cancel
Save