diff --git a/StateMachine/CarStateMachine/CarStateMachine.csproj b/StateMachine/CarStateMachine/CarStateMachine.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/StateMachine/CarStateMachine/CarStateMachine.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/StateMachine/CarStateMachine/Model/Car.cs b/StateMachine/CarStateMachine/Model/Car.cs new file mode 100644 index 0000000..3209e01 --- /dev/null +++ b/StateMachine/CarStateMachine/Model/Car.cs @@ -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 + // }; + } + } +} \ No newline at end of file diff --git a/StateMachine/CarStateMachine/Program.cs b/StateMachine/CarStateMachine/Program.cs new file mode 100644 index 0000000..44afdb2 --- /dev/null +++ b/StateMachine/CarStateMachine/Program.cs @@ -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}"); + } +} diff --git a/StateMachine/StateMachine.sln b/StateMachine/StateMachine.sln new file mode 100644 index 0000000..8e8c159 --- /dev/null +++ b/StateMachine/StateMachine.sln @@ -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 diff --git a/StateMachine/StateMachine/Model/Order.cs b/StateMachine/StateMachine/Model/Order.cs new file mode 100644 index 0000000..ece92ae --- /dev/null +++ b/StateMachine/StateMachine/Model/Order.cs @@ -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."); + } + } + } +} \ No newline at end of file diff --git a/StateMachine/StateMachine/Model/OrderState.cs b/StateMachine/StateMachine/Model/OrderState.cs new file mode 100644 index 0000000..e1d2e9e --- /dev/null +++ b/StateMachine/StateMachine/Model/OrderState.cs @@ -0,0 +1,11 @@ +namespace StateMachine.Model +{ + public enum OrderState + { + New, + Processing, + Shipped, + Delivered, + Canceled + } +} \ No newline at end of file diff --git a/StateMachine/StateMachine/Program.cs b/StateMachine/StateMachine/Program.cs new file mode 100644 index 0000000..4e27033 --- /dev/null +++ b/StateMachine/StateMachine/Program.cs @@ -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}"); + } +} diff --git a/StateMachine/StateMachine/StateMachine.csproj b/StateMachine/StateMachine/StateMachine.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/StateMachine/StateMachine/StateMachine.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + +