Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions elevator/Elevator/Elevator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ElevatorTests", "ElevatorTests\ElevatorTests.csproj", "{E275736A-8470-4443-81CC-CE4CD316250C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elevator", "Elevator\Elevator.csproj", "{FBC375F6-0EA9-4D43-A0BC-83786A5D3711}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E275736A-8470-4443-81CC-CE4CD316250C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E275736A-8470-4443-81CC-CE4CD316250C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E275736A-8470-4443-81CC-CE4CD316250C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E275736A-8470-4443-81CC-CE4CD316250C}.Release|Any CPU.Build.0 = Release|Any CPU
{FBC375F6-0EA9-4D43-A0BC-83786A5D3711}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FBC375F6-0EA9-4D43-A0BC-83786A5D3711}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FBC375F6-0EA9-4D43-A0BC-83786A5D3711}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FBC375F6-0EA9-4D43-A0BC-83786A5D3711}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E4BCEE56-C28E-432D-933C-2A103AF9947C}
EndGlobalSection
EndGlobal
139 changes: 139 additions & 0 deletions elevator/Elevator/Elevator/CommandProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using NLog;

namespace Elevator
{
public interface ICommandProcessor
{
public Task RunInputLoopAsync(ElevatorSystem elevatorSystem, int numFloors);

public Task<bool> ProcessCommandAsync(string command, ElevatorSystem elevatorSystem);
}

public class CommandProcessor : ICommandProcessor
{
private readonly ILogger Logger;

public CommandProcessor(ILogger logger)
{
Logger = logger;
}

public async Task RunInputLoopAsync(ElevatorSystem elevatorSystem, int numFloors)
{
ShowHelp(numFloors);

while (elevatorSystem.Status == ElevatorSystem.ElevatorSystemStatus.Running)
{
string command = Console.ReadLine();

var valid = await ProcessCommandAsync(command, elevatorSystem);

if (!valid)
{
Logger.Info($"Invalid command: \"{command}\"");

ShowHelp(numFloors);
}
else
{
Logger.Info($"Valid command: \"{command}\"");
}
}
}

private void ShowHelp(int numFloors)
{
Console.WriteLine("Invalid command. Valid commands are:");
Console.WriteLine("'#U' - Up button pressed on floor #. ex '5U'");
Console.WriteLine("'#D' - Down button pressed on floor #. ex '5D'");
Console.WriteLine("'#' - # button pressed on elevator. ex '5'");
Console.WriteLine($"Valid floor numbers are 1 to {numFloors}");
Console.WriteLine("'W+' - set the overweight state to true.");
Console.WriteLine("'W-' - set the overweight state to false");
Console.WriteLine("'Q' - Stop recieving input and quit the application after doing all scheduled stops.");
}

public async Task<bool> ProcessCommandAsync(string command, ElevatorSystem elevatorSystem)
{
if (command == null)
{
return false;
}

command = command.Trim().ToLower();
if (command == "q")
{
elevatorSystem.Status = ElevatorSystem.ElevatorSystemStatus.ShuttingDown;
return true;
}

if (command.StartsWith('w'))
{
command = command.Substring(1).Trim();

if (command == "+")
{
elevatorSystem.Elevators[0].SensorOverWeight = true;
return true;
}
else if (command == "-")
{
elevatorSystem.Elevators[0].SensorOverWeight = false;
return true;
}
return false;
}


int floor;

//up button pressed: ex "5U", "15u", "25 u"
if (command.EndsWith('u'))
{
command = command.Substring(0, command.Length - 1).Trim();

if (int.TryParse(command, out floor))
{
if ((0 < floor) && (floor <= elevatorSystem.NumFloors))
{
elevatorSystem.FloorStates[floor].Up = true;
return true;
}
}

return false;
}

//Down button pressed: ex "5D", "15D", "25 D"
if (command.EndsWith('d'))
{
command = command.Substring(0, command.Length - 1).Trim();

if (int.TryParse(command, out floor))
{
if ((0 < floor) && (floor <= elevatorSystem.NumFloors))
{
elevatorSystem.FloorStates[floor].Down = true;
return true;
}
}

return false;
}

//Elevator button pressed: ex "5", "15", "25"
if (int.TryParse(command, out floor))
{
if ((0 < floor) && (floor <= elevatorSystem.NumFloors))
{
elevatorSystem.Elevators[0].Stops[floor] = true;
return true;
}

return false;
}

return false;
}
}
}
48 changes: 48 additions & 0 deletions elevator/Elevator/Elevator/Elevator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace Elevator
{
public class Elevator
{
public enum ElevatorAction
{
Idle,
Moving,
Stopping,
Open
}

public enum MovingDirection
{
Up,
Down,
None
}

public ElevatorAction CurrentAction { get; set; }

public MovingDirection DesiredMovingDirection { get; set; }
public MovingDirection SensorMovingDirection { get; set; }

public int SensorCurrentPosition { get; set; }
public int SensorNextPosition { get; set; }

public bool SensorOverWeight { get; set; }

public Dictionary<int, bool> Stops { get; set; } = new Dictionary<int, bool>();

public Elevator (int numFloors)
{

for (int i = 0; i < numFloors; i++)
{
Stops.Add(i + 1, false);
}

SensorOverWeight = false;
SensorCurrentPosition = 1;
SensorNextPosition = 1;
CurrentAction = ElevatorAction.Idle;
SensorMovingDirection = MovingDirection.None;
DesiredMovingDirection = MovingDirection.None;
}
}
}
16 changes: 16 additions & 0 deletions elevator/Elevator/Elevator/Elevator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="NLog" Version="5.2.3" />
<PackageReference Include="xunit" Version="2.5.0" />
</ItemGroup>

</Project>
Loading