Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
27d45e4
setup console program and testing with cli tools
dan-kiss-dev-this Sep 18, 2023
1dae7d7
add notes to readme
dan-kiss-dev-this Sep 18, 2023
791bf1f
add code to gather input and also be able to turn elevator off
dan-kiss-dev-this Sep 18, 2023
159773b
add ElevatorEventObject
dan-kiss-dev-this Sep 18, 2023
d93fdd5
add test to add floor to visit from user
dan-kiss-dev-this Sep 18, 2023
e6ca0b5
add way to store and add floor
dan-kiss-dev-this Sep 18, 2023
7efe432
add test to visit multiple floors
dan-kiss-dev-this Sep 18, 2023
49028a8
change test to keep track of events in list
dan-kiss-dev-this Sep 18, 2023
52c2115
update how Elevator keeps track of event
dan-kiss-dev-this Sep 18, 2023
b30bbd9
update data model of event of floor being requested
dan-kiss-dev-this Sep 18, 2023
c02338d
add Newtonsoft to see object in string format for test debugging
dan-kiss-dev-this Sep 18, 2023
ba906ec
update readme with programs notes for tdd
dan-kiss-dev-this Sep 18, 2023
187593f
update test and elevator class to sort events based on floor requested
dan-kiss-dev-this Sep 18, 2023
5e05543
update readme
dan-kiss-dev-this Sep 18, 2023
69a96bf
remake Elevator model to account for direction
dan-kiss-dev-this Sep 18, 2023
517656a
add new readme notes
dan-kiss-dev-this Sep 18, 2023
d5cfdfd
adjust model
dan-kiss-dev-this Sep 19, 2023
fbdcbae
add test showing elevator storing floors to visit
dan-kiss-dev-this Sep 19, 2023
3cf2045
add test showing elevator storing floors to visit
dan-kiss-dev-this Sep 19, 2023
9e298b8
add the Run, GetNextFloor and MoveToFloor methods
dan-kiss-dev-this Sep 19, 2023
14b0c26
adjust elevator model
dan-kiss-dev-this Sep 19, 2023
ba57980
add test to verify multiple floors can be added
dan-kiss-dev-this Sep 19, 2023
23047eb
adjust program to make elevator run in loop
dan-kiss-dev-this Sep 19, 2023
c8b76cb
add to readme
dan-kiss-dev-this Sep 19, 2023
7255c6e
add in way to sort order floors go in
dan-kiss-dev-this Sep 19, 2023
845ca5a
add a 3 second delay to adding a floor if you are one floor away
dan-kiss-dev-this Sep 19, 2023
174a1d7
update testing to account for sorting
dan-kiss-dev-this Sep 19, 2023
5f9ef66
update program.cs
dan-kiss-dev-this Sep 19, 2023
23965b1
clean up logs in Elevator model
dan-kiss-dev-this Sep 19, 2023
df019cd
update naming of test
dan-kiss-dev-this Sep 19, 2023
5482a47
add test to confirm direction of elevator on floor request
dan-kiss-dev-this Sep 19, 2023
20a5ff3
move enum to namespace to pass testing
dan-kiss-dev-this Sep 19, 2023
dd15f50
add test that shows calculation of next floor to visit
dan-kiss-dev-this Sep 20, 2023
a6dbadf
add testing that shows floorRequests are removed after they are visted
dan-kiss-dev-this Sep 20, 2023
4cb7660
add notes to readme
dan-kiss-dev-this Sep 20, 2023
c159cbc
rewrite elevator event to focus on needed event information only
dan-kiss-dev-this Sep 20, 2023
704679a
add enum to TypeOfEvent and include in EvevatorEvent
dan-kiss-dev-this Sep 20, 2023
4ce2b96
add testing to verify elevator is tracking events
dan-kiss-dev-this Sep 20, 2023
e26efe4
adjust ElevatorInBuilding model to pass testing
dan-kiss-dev-this Sep 20, 2023
1516837
update readme
dan-kiss-dev-this Sep 20, 2023
0bb1151
merge changes
dan-kiss-dev-this Sep 20, 2023
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
obj
bin
TestResults
.DS
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
Expand Down
22 changes: 22 additions & 0 deletions Elevator.Tests/Elevator.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Elevator\Elevator.csproj" />
</ItemGroup>

</Project>
97 changes: 97 additions & 0 deletions Elevator.Tests/ModelTests/ElevatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Elevator.Models;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Threading;

namespace Elevator.Tests
{
[TestClass]
public class ElevatorTests
{
[TestMethod]
// naming convention is MethodOrFieldName_Description_ReturnType
public void ElevatorConstructor_CreateInstanceOfElevator_Elevator()
{
ElevatorInBuilding newElevator = new ElevatorInBuilding();
Assert.AreEqual(typeof(ElevatorInBuilding), newElevator.GetType());
}

//get floor to visit
[TestMethod]
// naming convention is MethodOrFieldName_Description_ReturnType
public void AddFloor_AddFloorToVisit_ListOfInt()
{
ElevatorInBuilding newElevator = new ElevatorInBuilding();
int floorToVisit = 2;
newElevator.RequestFloor(floorToVisit);
List<int> expectedAnswer = new List<int> { 2 };
Console.WriteLine(JsonConvert.SerializeObject(newElevator.floorRequests));
CollectionAssert.AreEqual(expectedAnswer, newElevator.floorRequests);
}

[TestMethod]
public void RequestFloor_AddMultipleFloorsInSortedManner_Void()
{
ElevatorInBuilding newElevator = new ElevatorInBuilding();
int floorToVisit = 5;
int floorToVisit2 = 3;
newElevator.RequestFloor(floorToVisit);
newElevator.RequestFloor(floorToVisit2);
List<int> expectedAnswer = new List<int> { 3, 5 };
Console.WriteLine(JsonConvert.SerializeObject(newElevator.floorRequests));
Assert.AreEqual(newElevator.direction, Direction.Up);
CollectionAssert.AreEqual(expectedAnswer, newElevator.floorRequests);
}

[TestMethod]
public void RequestFloor_DirectionIsUp_Void()
{
ElevatorInBuilding newElevator = new ElevatorInBuilding();
int floorToVisit = 5;
int floorToVisit2 = 3;
newElevator.RequestFloor(floorToVisit);
newElevator.RequestFloor(floorToVisit2);
Assert.AreEqual(newElevator.direction, Direction.Up);
}

[TestMethod]
public void Run_NextFloorToVisitIsCalculated_Void()
{
ElevatorInBuilding newElevator = new ElevatorInBuilding();
int floorToVisit = 5;
newElevator.RequestFloor(floorToVisit);
newElevator.Run();
Assert.AreEqual(newElevator.nextFloorToVisit, floorToVisit);
}

[TestMethod]
public void Run_FloorRequestsAreRemovedAfterVisited_Void()
{
ElevatorInBuilding newElevator = new ElevatorInBuilding();
int floorToVisit = 5;
int floorToVisit2 = 3;
newElevator.RequestFloor(floorToVisit);
newElevator.RequestFloor(floorToVisit2);
newElevator.Run();
List<int> blankList = new List<int>() { };
CollectionAssert.AreEqual(newElevator.floorRequests, blankList);
}

[TestMethod]
public void Run_AddEventsToElevator_Void()
{
ElevatorInBuilding newElevator = new ElevatorInBuilding();
int floorToVisit = 3;
newElevator.RequestFloor(floorToVisit);
newElevator.Run();
ElevatorEvent newEvent1 = new ElevatorEvent(TypeOfEvent.FloorRequest);
ElevatorEvent newEvent2 = new ElevatorEvent(TypeOfEvent.PassFloor); // 1
ElevatorEvent newEvent3 = new ElevatorEvent(TypeOfEvent.PassFloor); // 2
ElevatorEvent newEvent4 = new ElevatorEvent(TypeOfEvent.StopFloor);
List<ElevatorEvent> eventList = new List<ElevatorEvent> { newEvent1, newEvent2, newEvent3, newEvent4 };
Assert.AreEqual(newElevator.events.Count, eventList.Count);
}
}
}
14 changes: 14 additions & 0 deletions Elevator/Elevator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions Elevator/Models/ElevatorEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Elevator.Models
{
// 1-Timestamp and asynchronous floor request, every time one occurs.
// 2-Timestamp and floor, every time elevator passes a floor.
// 3-Timestamp and floor, every time elevator stops at a floor.

// 3 types are floor request, stop on floor, pass a floor-check if stopped if so 4 second wait otherwise 3 second wait, just add the needed seconds to the timestamp
public enum TypeOfEvent
{
FloorRequest,
PassFloor,
StopFloor
}
public class ElevatorEvent
{
public DateTimeOffset Now { get; set; }
public TypeOfEvent TypeOfEvent { get; set; }
public ElevatorEvent(TypeOfEvent type)
{
Now = (DateTimeOffset)DateTime.UtcNow;
TypeOfEvent = type;
}

}
}
113 changes: 113 additions & 0 deletions Elevator/Models/ElevatorInBuilding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System.Collections.Generic;
using System;
using System.Threading;
using System.Linq;
using Newtonsoft.Json;

namespace Elevator.Models
{
public enum Direction
{
Up, //0
Down, //1
Idle //2
}
public class ElevatorInBuilding
{
public int currentFloor;
public Direction direction;
public List<int> floorRequests = new List<int>();
public int nextFloorToVisit;
public List<ElevatorEvent> events = new List<ElevatorEvent>();

public ElevatorInBuilding()
{
currentFloor = 1;
direction = Direction.Idle;
// floorRequests = new List<int>();
}

public void RequestFloor(int floor)
{
if (floor == currentFloor)
{
return;
}
if (direction == Direction.Idle)
{
direction = (floor > currentFloor) ? Direction.Up : Direction.Down;
Console.WriteLine($"Direction was idle, now being set to {direction}");
}

floorRequests.Add(floor);
ElevatorEvent floorRequest = new ElevatorEvent(TypeOfEvent.FloorRequest);
events.Add(floorRequest);
//need to sort the floors here however if one floor away hold on for 3 seconds before doing the sort since the elevator cannot stop there on time
if (Math.Abs(floor - currentFloor) == 1)
{
Thread.Sleep(3000);
}
floorRequests.Sort();
}

// Run the elevator, note on request floor we set the direction
public void Run()
{
while (floorRequests.Count > 0)
{
Console.WriteLine("Calculating next floor");
int floor = GetNextFloor();
nextFloorToVisit = floor;
Console.WriteLine("Moving to next floor");
Console.WriteLine(nextFloorToVisit);
MoveToFloor(nextFloorToVisit);
}
direction = Direction.Idle;
}

private int GetNextFloor()
{
if (direction == Direction.Up)
{
return floorRequests.Where(someFloor => someFloor > currentFloor).DefaultIfEmpty(floorRequests.Max()).Min();
}
else if (direction == Direction.Down)
{
return floorRequests.Where(someFloor => someFloor < currentFloor).DefaultIfEmpty(floorRequests.Min()).Max();
}
return currentFloor;
}

private void MoveToFloor(int targetFloor)
{
if (currentFloor < targetFloor)
{
direction = Direction.Up;
while (currentFloor < targetFloor)
{
// wait 3 seconds for elevator to move
Thread.Sleep(3000);
currentFloor++;
Console.WriteLine($"Arrived at Floor {currentFloor}");
events.Add(new ElevatorEvent(TypeOfEvent.PassFloor));
}
}
else if (currentFloor > targetFloor)
{
direction = Direction.Down;
while (currentFloor > targetFloor)
{
Thread.Sleep(3000);
currentFloor--;
Console.WriteLine($"Arrived at Floor {currentFloor}");
events.Add(new ElevatorEvent(TypeOfEvent.PassFloor));
}
}
Console.WriteLine($"Elevator stopped at Floor {currentFloor}");
events.Add(new ElevatorEvent(TypeOfEvent.StopFloor));
floorRequests.Remove(currentFloor);
Thread.Sleep(1000);
}

}
}
28 changes: 28 additions & 0 deletions Elevator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Elevator.Models;
// See https://aka.ms/new-console-template for more information
ElevatorInBuilding elevator = new ElevatorInBuilding();

while (true)
{
Console.WriteLine("Input a floor request as a number and press enter or 'q' to quit then press enter, you can keep adding multiple floors just type them and press enter");
string input = Console.ReadLine().Trim();
if (input.Equals("q"))
{
break;
}
// check use of out
else if (int.TryParse(input, out int floor))
{
Console.WriteLine("Parsing inputted value to integer");
elevator.RequestFloor(floor);
elevator.Run();
}

if (elevator.direction == Direction.Idle)
{
Console.WriteLine("Elevator is idle (not moving).");
elevator.Run();
}
}


Loading