From 27d45e4de81884732be2309bde1eba9544f87944 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 10:21:34 -0700 Subject: [PATCH 01/40] setup console program and testing with cli tools --- .gitignore | 4 ++++ Elevator.Tests/Elevator.Tests.csproj | 22 ++++++++++++++++++++++ Elevator.Tests/ModelTests/ElevatorTests.cs | 22 ++++++++++++++++++++++ Elevator/Elevator.csproj | 10 ++++++++++ Elevator/Models/ElevatorEvent.cs | 7 +++++++ Elevator/Models/ElevatorInBuilding.cs | 9 +++++++++ Elevator/Program.cs | 2 ++ README.md | 21 +++++++++++++++++++++ 8 files changed, 97 insertions(+) create mode 100644 .gitignore create mode 100644 Elevator.Tests/Elevator.Tests.csproj create mode 100644 Elevator.Tests/ModelTests/ElevatorTests.cs create mode 100644 Elevator/Elevator.csproj create mode 100644 Elevator/Models/ElevatorEvent.cs create mode 100644 Elevator/Models/ElevatorInBuilding.cs create mode 100644 Elevator/Program.cs create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..461572c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +obj +bin +TestResults +.DS_Store \ No newline at end of file diff --git a/Elevator.Tests/Elevator.Tests.csproj b/Elevator.Tests/Elevator.Tests.csproj new file mode 100644 index 0000000..be37266 --- /dev/null +++ b/Elevator.Tests/Elevator.Tests.csproj @@ -0,0 +1,22 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + + + + + + + diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs new file mode 100644 index 0000000..a7213d6 --- /dev/null +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -0,0 +1,22 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Elevator.Models; +using System; +using System.Collections.Generic; + +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()); + } + + //move a floor + + } +} \ No newline at end of file diff --git a/Elevator/Elevator.csproj b/Elevator/Elevator.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/Elevator/Elevator.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Elevator/Models/ElevatorEvent.cs b/Elevator/Models/ElevatorEvent.cs new file mode 100644 index 0000000..c8ce4fd --- /dev/null +++ b/Elevator/Models/ElevatorEvent.cs @@ -0,0 +1,7 @@ +namespace Elevator.Models +{ + public class ElevatorEvent + { + + } +} \ No newline at end of file diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs new file mode 100644 index 0000000..717a85e --- /dev/null +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Elevator.Models +{ + public class ElevatorInBuilding + { + + } +} \ No newline at end of file diff --git a/Elevator/Program.cs b/Elevator/Program.cs new file mode 100644 index 0000000..83fa4f4 --- /dev/null +++ b/Elevator/Program.cs @@ -0,0 +1,2 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); diff --git a/README.md b/README.md new file mode 100644 index 0000000..4fedd5a --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +Build an Elevator Coding Challenge! +The Challenge +Create an application that simulates the operation of a simple elevator. + +Requirements +The elevator must travel in one direction at a time until it needs to go no further (e.g. keep going until the elevator has reached the top/bottom of the building, or no stop is requested on any floor ahead). +Elevator floor request buttons can be pressed asynchronously from inside or outside the elevator while it is running. +Elevator will stop at the closest floor first, in the direction of motion, then the next closest and so on. Any floors requested while the elevator is moving should be taken into account. +Elevator will stop at all asynchronously requested floors, only if the request is made while the elevator is at least one floor away (e.g. if elevator is between 4th and 5th floor, going up, and the 5th floor is requested at that moment, elevator will not stop at the 5th floor while going up; it will stop there while going down). +When elevator arrives at a requested floor, it waits for 1 second. It takes 3 seconds to travel between consecutive floors. +A sensor tells the elevator its direction, next/current floor, state (stopped, moving) and if the elevator has reached its max weight limit. +Use the sensor data plus the asynchronous floor request button data to work the elevator. +Write meaningful unit tests that show the elevator works correctly, even if the application is not run. +Log the following to a file, to verify elevator works well: +Timestamp and asynchronous floor request, every time one occurs. +Timestamp and floor, every time elevator passes a floor. +Timestamp and floor, every time elevator stops at a floor. +Bonus Enhancement: + +Enhance the application as follows: If the elevator has reached its weight limit, it should stop only at floors that were selected from inside the elevator (to let passengers out), until it is no longer at the max weight limit. +Note: For simplicity, the asynchronous request buttons can be entered by the application user via the console, by entering "5U" (request from 5th floor wanting to go Up) or "8D" (request from 8th floor wanting to go Down) or "2" (request from inside elevator wanting to stop at 2nd floor). When the user enters "Q" on the console, the application must end after visiting all floors entered before "Q". \ No newline at end of file From 1dae7d790665eb218897a748b241a5800c405017 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 11:08:54 -0700 Subject: [PATCH 02/40] add notes to readme --- README.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4fedd5a..07fa4b3 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,36 @@ Timestamp and floor, every time elevator stops at a floor. Bonus Enhancement: Enhance the application as follows: If the elevator has reached its weight limit, it should stop only at floors that were selected from inside the elevator (to let passengers out), until it is no longer at the max weight limit. -Note: For simplicity, the asynchronous request buttons can be entered by the application user via the console, by entering "5U" (request from 5th floor wanting to go Up) or "8D" (request from 8th floor wanting to go Down) or "2" (request from inside elevator wanting to stop at 2nd floor). When the user enters "Q" on the console, the application must end after visiting all floors entered before "Q". \ No newline at end of file +Note: For simplicity, the asynchronous request buttons can be entered by the application user via the console, by entering "5U" (request from 5th floor wanting to go Up) or "8D" (request from 8th floor wanting to go Down) or "2" (request from inside elevator wanting to stop at 2nd floor). When the user enters "Q" on the console, the application must end after visiting all floors entered before "Q". + +DK ideas- use the elevator events to track what the elevator will do, + +Elevator fields and methods +DirectionUp +CheckDirection aka if going up see events up has value if so keep going +EventsUp +EventsDown + +ElevatorEvents +{ + Timestamp (Date and time including seconds) - use for timing + DirectionOfElevator is Up or Down + DirectionOfEventUp bool used to determine if elevator will hit this floor when going up if true + FloorRequestedBool bool used when floor is Requested + FloorOn int is floor elevator has moved to + FloorPassedBool bool used when floor is passed + FloorStopped bool used when the floor is stoppedon + CompletedEvent bool event is done +} + +-other items to keep in mind can add a floor async, add way to gather input + +example +go to floor 2, 8, 5 note start on 1. + +idea is to go through elevator events, elevator events are key driver for how elevator will move +-go up or go down + +idea the elevator will contain elevator events with floors above current floor, floors below current floor, completedEvents aka moved to completed events when the floor is reached + +so you determine if event is above or below current floor if same floor nothing happens, just log same floor requested and have a record \ No newline at end of file From 791bf1f14ec8162d49deeceee65ddeb79ccf6445 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 11:10:23 -0700 Subject: [PATCH 03/40] add code to gather input and also be able to turn elevator off --- Elevator/Program.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Elevator/Program.cs b/Elevator/Program.cs index 83fa4f4..8dba100 100644 --- a/Elevator/Program.cs +++ b/Elevator/Program.cs @@ -1,2 +1,15 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +bool IsOn = true; +while (IsOn) +{ + Console.WriteLine("Hello, World, enter a floor or q to quit"); + string floor = Console.ReadLine(); + Console.WriteLine($"you want floor {floor}"); + + if (floor == "q") + { + IsOn = false; + Console.WriteLine("turn elevator off"); + } +} + From 159773b77dd1141be407cc4564f758c1f33e4525 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 11:32:00 -0700 Subject: [PATCH 04/40] add ElevatorEventObject --- Elevator/Models/ElevatorEvent.cs | 33 +++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Elevator/Models/ElevatorEvent.cs b/Elevator/Models/ElevatorEvent.cs index c8ce4fd..afca298 100644 --- a/Elevator/Models/ElevatorEvent.cs +++ b/Elevator/Models/ElevatorEvent.cs @@ -1,7 +1,38 @@ +using System; + namespace Elevator.Models { public class ElevatorEvent { - + public ElevatorEvent( + bool DirectionOfElevatorUp, + bool DirectionOfEventUp, + bool FloorRequestedBool, + bool FloorOn, + bool FloorPassedBool, + bool FloorStopped, + bool CompletedEvent + ) + { + this.Now = (DateTimeOffset)DateTime.UtcNow; + this.DirectionOfElevatorUp = DirectionOfElevatorUp; + } + // Timestamp (Date and time including seconds) - use for timing // using System; + public DateTimeOffset Now { get; set; } + + public bool DirectionOfElevatorUp { get; set; } + // bool used to determine if elevator will hit this floor when going up if true + + public bool DirectionOfEventUp { get; set; } + // bool used when floor is Requested + public bool FloorRequestedBool { get; set; } + // int is floor elevator has moved to + public int FloorOn { get; set; } + // FloorPassedBool bool used when floor is passed + public bool FloorPassedBool { get; set; } + // bool used when the floor is stoppedon + public bool FloorStopped { get; set; } + // bool event is done + public bool CompletedEvent { get; set; } } } \ No newline at end of file From d93fdd5b1960a4e2bfa423816777e65fb80dae9b Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 11:50:30 -0700 Subject: [PATCH 05/40] add test to add floor to visit from user --- Elevator.Tests/ModelTests/ElevatorTests.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index a7213d6..65042b5 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -15,8 +15,18 @@ public void ElevatorConstructor_CreateInstanceOfElevator_Elevator() ElevatorInBuilding newElevator = new ElevatorInBuilding(); Assert.AreEqual(typeof(ElevatorInBuilding), newElevator.GetType()); } - - //move a floor + + //get floor to visit + [TestMethod] + // naming convention is MethodOrFieldName_Description_ReturnType + public void AddFloor_AddFloorToVisit_Void() + { + ElevatorInBuilding newElevator = new ElevatorInBuilding(); + int floorToVisit = 2; + List expectedAnswer = new List { 2 }; + newElevator.AddFloor(floorToVisit); + CollectionAssert.AreEqual(newElevator.FloorsToVisit, expectedAnswer); + } } } \ No newline at end of file From e6ca0b52874d686debff7231ee86114da2e70326 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 11:50:59 -0700 Subject: [PATCH 06/40] add way to store and add floor --- Elevator/Models/ElevatorInBuilding.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs index 717a85e..93029ec 100644 --- a/Elevator/Models/ElevatorInBuilding.cs +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -4,6 +4,11 @@ namespace Elevator.Models { public class ElevatorInBuilding { - + public List FloorsToVisit = new List { }; + + public void AddFloor(int floor) + { + FloorsToVisit.Add(floor); + } } } \ No newline at end of file From 7efe4326b9a03a18d47d63be903ce9e35f4e0178 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 11:57:01 -0700 Subject: [PATCH 07/40] add test to visit multiple floors --- Elevator.Tests/ModelTests/ElevatorTests.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index 65042b5..b18668e 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -28,5 +28,20 @@ public void AddFloor_AddFloorToVisit_Void() CollectionAssert.AreEqual(newElevator.FloorsToVisit, expectedAnswer); } + [TestMethod] + // naming convention is MethodOrFieldName_Description_ReturnType + public void AddFloor_AddFloorToVisitMultipleTimes_Void() + { + ElevatorInBuilding newElevator = new ElevatorInBuilding(); + int floorToVisit = 2; + int floorToVisit2 = 8; + int floorToVisit3 = 5; + List expectedAnswer = new List { 2, 8, 5 }; + newElevator.AddFloor(floorToVisit); + newElevator.AddFloor(floorToVisit2); + newElevator.AddFloor(floorToVisit3); + CollectionAssert.AreEqual(newElevator.FloorsToVisit, expectedAnswer); + } + } } \ No newline at end of file From 49028a89fe2916fe80fda0e1d610dd0c6f3ecc08 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 12:30:37 -0700 Subject: [PATCH 08/40] change test to keep track of events in list --- Elevator.Tests/ModelTests/ElevatorTests.cs | 39 ++++++++++++---------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index b18668e..f7e1dc2 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -2,6 +2,7 @@ using Elevator.Models; using System; using System.Collections.Generic; +using Newtonsoft.Json; namespace Elevator.Tests { @@ -19,29 +20,31 @@ public void ElevatorConstructor_CreateInstanceOfElevator_Elevator() //get floor to visit [TestMethod] // naming convention is MethodOrFieldName_Description_ReturnType - public void AddFloor_AddFloorToVisit_Void() + public void AddFloor_AddFloorToVisit_ListOfEvents() { ElevatorInBuilding newElevator = new ElevatorInBuilding(); int floorToVisit = 2; - List expectedAnswer = new List { 2 }; - newElevator.AddFloor(floorToVisit); - CollectionAssert.AreEqual(newElevator.FloorsToVisit, expectedAnswer); + List expectedAnswer = newElevator.AddEvent(floorToVisit); + Console.WriteLine("2929"); + Console.WriteLine(JsonConvert.SerializeObject(expectedAnswer)); + Console.WriteLine(JsonConvert.SerializeObject(newElevator.EventsOrderedByFloor)); + Console.WriteLine("3232"); + CollectionAssert.AreEqual(expectedAnswer, newElevator.EventsOrderedByFloor); } - [TestMethod] - // naming convention is MethodOrFieldName_Description_ReturnType - public void AddFloor_AddFloorToVisitMultipleTimes_Void() - { - ElevatorInBuilding newElevator = new ElevatorInBuilding(); - int floorToVisit = 2; - int floorToVisit2 = 8; - int floorToVisit3 = 5; - List expectedAnswer = new List { 2, 8, 5 }; - newElevator.AddFloor(floorToVisit); - newElevator.AddFloor(floorToVisit2); - newElevator.AddFloor(floorToVisit3); - CollectionAssert.AreEqual(newElevator.FloorsToVisit, expectedAnswer); - } + // [TestMethod] + // public void AddFloor_AddFloorToVisitMultipleTimes_Void() + // { + // ElevatorInBuilding newElevator = new ElevatorInBuilding(); + // int floorToVisit = 2; + // int floorToVisit2 = 8; + // int floorToVisit3 = 5; + // List expectedAnswer = new List { 2, 8, 5 }; + // newElevator.AddFloor(floorToVisit); + // newElevator.AddFloor(floorToVisit2); + // newElevator.AddFloor(floorToVisit3); + // CollectionAssert.AreEqual(newElevator.FloorsToVisit, expectedAnswer); + // } } } \ No newline at end of file From 52c2115660bfaf8f1c3f952fac857432ec3c0633 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 12:31:12 -0700 Subject: [PATCH 09/40] update how Elevator keeps track of event --- Elevator/Models/ElevatorInBuilding.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs index 93029ec..e960764 100644 --- a/Elevator/Models/ElevatorInBuilding.cs +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -4,11 +4,15 @@ namespace Elevator.Models { public class ElevatorInBuilding { - public List FloorsToVisit = new List { }; + public List EventsOrderedByFloor = new List { }; - public void AddFloor(int floor) + public List AddEvent(int floorToVisit) { - FloorsToVisit.Add(floor); + ElevatorEvent newEvent = new ElevatorEvent(floorToVisit); + + EventsOrderedByFloor.Add(newEvent); + + return EventsOrderedByFloor; } } } \ No newline at end of file From b30bbd98f464fdc1376a6023c36c9c11ae6e6d9e Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 12:31:42 -0700 Subject: [PATCH 10/40] update data model of event of floor being requested --- Elevator/Models/ElevatorEvent.cs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Elevator/Models/ElevatorEvent.cs b/Elevator/Models/ElevatorEvent.cs index afca298..653967f 100644 --- a/Elevator/Models/ElevatorEvent.cs +++ b/Elevator/Models/ElevatorEvent.cs @@ -5,21 +5,26 @@ namespace Elevator.Models public class ElevatorEvent { public ElevatorEvent( - bool DirectionOfElevatorUp, - bool DirectionOfEventUp, - bool FloorRequestedBool, - bool FloorOn, - bool FloorPassedBool, - bool FloorStopped, - bool CompletedEvent + int FloorRequested + // bool DirectionOfElevatorUp, + // bool DirectionOfEventUp, + // bool FloorRequestedBool, + // bool FloorOn, + // bool FloorPassedBool, + // bool FloorStopped, + // bool CompletedEvent ) { this.Now = (DateTimeOffset)DateTime.UtcNow; - this.DirectionOfElevatorUp = DirectionOfElevatorUp; + this.FloorRequested = FloorRequested; + + // this.DirectionOfElevatorUp = DirectionOfElevatorUp; } // Timestamp (Date and time including seconds) - use for timing // using System; public DateTimeOffset Now { get; set; } + public int FloorRequested { get; set; } + public bool DirectionOfElevatorUp { get; set; } // bool used to determine if elevator will hit this floor when going up if true From c02338dc218ba564d3eb1c862a6f84a2fbfff779 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 12:32:35 -0700 Subject: [PATCH 11/40] add Newtonsoft to see object in string format for test debugging --- Elevator/Elevator.csproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Elevator/Elevator.csproj b/Elevator/Elevator.csproj index 40c60dd..48c94e1 100644 --- a/Elevator/Elevator.csproj +++ b/Elevator/Elevator.csproj @@ -7,4 +7,8 @@ enable + + + + From ba906ec6cd028e554fc9400a1acc702b8a49aeab Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 12:33:26 -0700 Subject: [PATCH 12/40] update readme with programs notes for tdd --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 07fa4b3..0ad775e 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,9 @@ Note: For simplicity, the asynchronous request buttons can be entered by the app DK ideas- use the elevator events to track what the elevator will do, Elevator fields and methods -DirectionUp +DirectionOfElevator CheckDirection aka if going up see events up has value if so keep going -EventsUp -EventsDown +Events, note everytime a floor is visited you track if evelator is going up or down and resort events ElevatorEvents { @@ -50,4 +49,7 @@ idea is to go through elevator events, elevator events are key driver for how el idea the elevator will contain elevator events with floors above current floor, floors below current floor, completedEvents aka moved to completed events when the floor is reached -so you determine if event is above or below current floor if same floor nothing happens, just log same floor requested and have a record \ No newline at end of file +so you determine if event is above or below current floor if same floor nothing happens, just log same floor requested and have a record + +// now the elevator, be able to gather inputs of what floors to visit. +-once floors are gathered, turn that into event objects for tdd \ No newline at end of file From 187593f7feb4547a232c8bc855fd42c1201a7c92 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 13:24:29 -0700 Subject: [PATCH 13/40] update test and elevator class to sort events based on floor requested --- Elevator.Tests/ModelTests/ElevatorTests.cs | 19 ++++++++++++++++--- Elevator/Models/ElevatorInBuilding.cs | 4 ++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index f7e1dc2..df1a71a 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -25,10 +25,23 @@ public void AddFloor_AddFloorToVisit_ListOfEvents() ElevatorInBuilding newElevator = new ElevatorInBuilding(); int floorToVisit = 2; List expectedAnswer = newElevator.AddEvent(floorToVisit); - Console.WriteLine("2929"); + // Console.WriteLine(JsonConvert.SerializeObject(expectedAnswer)); + CollectionAssert.AreEqual(expectedAnswer, newElevator.EventsOrderedByFloor); + } + + [TestMethod] + // naming convention is MethodOrFieldName_Description_ReturnType + public void AddFloor_AddFloorToVisitMultipleTimesAndSort_ListOfEvents() + { + ElevatorInBuilding newElevator = new ElevatorInBuilding(); + int floorToVisit1 = 2; + int floorToVisit2 = 8; + int floorToVisit3 = 5; + newElevator.AddEvent(floorToVisit1); + newElevator.AddEvent(floorToVisit2); + List expectedAnswer = newElevator.AddEvent(floorToVisit3); + // List sample = new List { new ElevatorEvent(1) }; Console.WriteLine(JsonConvert.SerializeObject(expectedAnswer)); - Console.WriteLine(JsonConvert.SerializeObject(newElevator.EventsOrderedByFloor)); - Console.WriteLine("3232"); CollectionAssert.AreEqual(expectedAnswer, newElevator.EventsOrderedByFloor); } diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs index e960764..d3c3b38 100644 --- a/Elevator/Models/ElevatorInBuilding.cs +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -4,6 +4,7 @@ namespace Elevator.Models { public class ElevatorInBuilding { + public int CurrentFloor { get; set; } = 1; public List EventsOrderedByFloor = new List { }; public List AddEvent(int floorToVisit) @@ -12,6 +13,9 @@ public List AddEvent(int floorToVisit) EventsOrderedByFloor.Add(newEvent); + //sort floors in order + EventsOrderedByFloor.Sort((x, y) => x.FloorRequested.CompareTo(y.FloorRequested)); + return EventsOrderedByFloor; } } From 5e055434191c56a0a37b193241370757be29d5ae Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 13:24:53 -0700 Subject: [PATCH 14/40] update readme --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0ad775e..c621496 100644 --- a/README.md +++ b/README.md @@ -52,4 +52,6 @@ idea the elevator will contain elevator events with floors above current floor, so you determine if event is above or below current floor if same floor nothing happens, just log same floor requested and have a record // now the elevator, be able to gather inputs of what floors to visit. --once floors are gathered, turn that into event objects for tdd \ No newline at end of file +-once floors are gathered, turn that into event objects for tdd + +-do the timer last for now just add floors to visit and sort the floors in order, keep going the needed direction \ No newline at end of file From 69a96bfdd7c1a1dce4ee5f988f60790426655ed9 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 16:41:45 -0700 Subject: [PATCH 15/40] remake Elevator model to account for direction --- Elevator/Models/ElevatorInBuilding.cs | 39 ++++++++++++++++++++------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs index d3c3b38..b683343 100644 --- a/Elevator/Models/ElevatorInBuilding.cs +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -1,22 +1,43 @@ using System.Collections.Generic; +using System; +using System.Threading; + +public enum Direction +{ + Up, //0 + Down, //1 + Idle //2 +} namespace Elevator.Models { public class ElevatorInBuilding { - public int CurrentFloor { get; set; } = 1; - public List EventsOrderedByFloor = new List { }; + private int currentFloor; + private Direction direction; + private List floorRequests; - public List AddEvent(int floorToVisit) + public ElevatorInBuilding() { - ElevatorEvent newEvent = new ElevatorEvent(floorToVisit); - - EventsOrderedByFloor.Add(newEvent); + currentFloor = 1; + direction = Direction.Idle; + floorRequests = new List(); + } - //sort floors in order - EventsOrderedByFloor.Sort((x, y) => x.FloorRequested.CompareTo(y.FloorRequested)); + public void RequestFloor(int floor) + { + if(floor == currentFloor) + { + return; + } + if(direction == Direction.Idle) + { + direction = (floor > currentFloor) ? Direction.Up : Direction.Down; + } - return EventsOrderedByFloor; + floorRequests.Add(floor); } + + } } \ No newline at end of file From 517656a4f34cb2d679609f45933306cebf9e0180 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 16:41:54 -0700 Subject: [PATCH 16/40] add new readme notes --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c621496..551aa9c 100644 --- a/README.md +++ b/README.md @@ -54,4 +54,12 @@ so you determine if event is above or below current floor if same floor nothing // now the elevator, be able to gather inputs of what floors to visit. -once floors are gathered, turn that into event objects for tdd --do the timer last for now just add floors to visit and sort the floors in order, keep going the needed direction \ No newline at end of file +-do the timer last for now just add floors to visit and sort the floors in order, keep going the needed direction + + +//notes on what I have +I can collect floors to visit, this is made into event objects and events sorted ascending by floor are availible. + + +// new idea focus on the direction the elevator goes and what happens between the floors +Up, Down, Stopped(Idle) \ No newline at end of file From d5cfdfd9c88b5356c10713468424820c56449d7d Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 17:02:04 -0700 Subject: [PATCH 17/40] adjust model --- Elevator/Models/ElevatorInBuilding.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs index b683343..d6aa9a6 100644 --- a/Elevator/Models/ElevatorInBuilding.cs +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -15,7 +15,7 @@ public class ElevatorInBuilding { private int currentFloor; private Direction direction; - private List floorRequests; + public List floorRequests; public ElevatorInBuilding() { From fbdcbae74de494f87124a697c5e6d4b5a497d1c0 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 17:02:40 -0700 Subject: [PATCH 18/40] add test showing elevator storing floors to visit --- Elevator.Tests/ModelTests/ElevatorTests.cs | 41 ++++++++-------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index df1a71a..55ce0fa 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -24,40 +24,29 @@ public void AddFloor_AddFloorToVisit_ListOfEvents() { ElevatorInBuilding newElevator = new ElevatorInBuilding(); int floorToVisit = 2; - List expectedAnswer = newElevator.AddEvent(floorToVisit); - // Console.WriteLine(JsonConvert.SerializeObject(expectedAnswer)); - CollectionAssert.AreEqual(expectedAnswer, newElevator.EventsOrderedByFloor); - } - - [TestMethod] - // naming convention is MethodOrFieldName_Description_ReturnType - public void AddFloor_AddFloorToVisitMultipleTimesAndSort_ListOfEvents() - { - ElevatorInBuilding newElevator = new ElevatorInBuilding(); - int floorToVisit1 = 2; - int floorToVisit2 = 8; - int floorToVisit3 = 5; - newElevator.AddEvent(floorToVisit1); - newElevator.AddEvent(floorToVisit2); - List expectedAnswer = newElevator.AddEvent(floorToVisit3); - // List sample = new List { new ElevatorEvent(1) }; - Console.WriteLine(JsonConvert.SerializeObject(expectedAnswer)); - CollectionAssert.AreEqual(expectedAnswer, newElevator.EventsOrderedByFloor); + newElevator.RequestFloor(floorToVisit); + List expectedAnswer = new List { 2 }; + Console.WriteLine(JsonConvert.SerializeObject(newElevator.floorRequests)); + CollectionAssert.AreEqual(expectedAnswer, newElevator.floorRequests); } // [TestMethod] - // public void AddFloor_AddFloorToVisitMultipleTimes_Void() + // // naming convention is MethodOrFieldName_Description_ReturnType + // public void AddFloor_AddFloorToVisitMultipleTimesAndSort_ListOfEvents() // { // ElevatorInBuilding newElevator = new ElevatorInBuilding(); - // int floorToVisit = 2; + // int floorToVisit1 = 2; // int floorToVisit2 = 8; // int floorToVisit3 = 5; - // List expectedAnswer = new List { 2, 8, 5 }; - // newElevator.AddFloor(floorToVisit); - // newElevator.AddFloor(floorToVisit2); - // newElevator.AddFloor(floorToVisit3); - // CollectionAssert.AreEqual(newElevator.FloorsToVisit, expectedAnswer); + // newElevator.AddEvent(floorToVisit1); + // newElevator.AddEvent(floorToVisit2); + // List expectedAnswer = newElevator.AddEvent(floorToVisit3); + // // List sample = new List { new ElevatorEvent(1) }; + // Console.WriteLine(JsonConvert.SerializeObject(expectedAnswer)); + // CollectionAssert.AreEqual(expectedAnswer, newElevator.EventsOrderedByFloor); // } + + } } \ No newline at end of file From 3cf204557bd908d8185e03fcbcfc6a09b1b63778 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 17:02:51 -0700 Subject: [PATCH 19/40] add test showing elevator storing floors to visit --- Elevator.Tests/ModelTests/ElevatorTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index 55ce0fa..91db903 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -20,7 +20,7 @@ public void ElevatorConstructor_CreateInstanceOfElevator_Elevator() //get floor to visit [TestMethod] // naming convention is MethodOrFieldName_Description_ReturnType - public void AddFloor_AddFloorToVisit_ListOfEvents() + public void AddFloor_AddFloorToVisit_ListOfInt() { ElevatorInBuilding newElevator = new ElevatorInBuilding(); int floorToVisit = 2; From 9e298b856c682ea669d80f9d951993465a7008a1 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 18:56:44 -0700 Subject: [PATCH 20/40] add the Run, GetNextFloor and MoveToFloor methods --- Elevator/Models/ElevatorInBuilding.cs | 63 +++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs index d6aa9a6..f45e528 100644 --- a/Elevator/Models/ElevatorInBuilding.cs +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System; using System.Threading; +using System.Linq; public enum Direction { @@ -14,7 +15,7 @@ namespace Elevator.Models public class ElevatorInBuilding { private int currentFloor; - private Direction direction; + public Direction direction; public List floorRequests; public ElevatorInBuilding() @@ -26,18 +27,74 @@ public ElevatorInBuilding() public void RequestFloor(int floor) { - if(floor == currentFloor) + Console.WriteLine(3030); + if (floor == currentFloor) { return; } - if(direction == Direction.Idle) + if (direction == Direction.Idle) { + Console.WriteLine(3737); direction = (floor > currentFloor) ? Direction.Up : Direction.Down; } + Console.WriteLine(4141); floorRequests.Add(floor); + Run(); } + // Run the elevator, note on request floor we set the direction + public void Run() + { + while (floorRequests.Count > 0) + { + int nextFloor = GetNextFloor(); + Console.WriteLine(4848); + MoveToFloor(nextFloor); + } + 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}"); + } + } + else if (currentFloor > targetFloor) + { + direction = Direction.Down; + while (currentFloor > targetFloor) + { + Thread.Sleep(3000); + currentFloor--; + Console.WriteLine($"Arrived at Floor {currentFloor}"); + } + } + Console.WriteLine($"Elevator stopped at Floor {currentFloor}"); + floorRequests.Remove(currentFloor); + Thread.Sleep(1000); + } } } \ No newline at end of file From 14b0c26425d2a8023aedbf5af2d90dca8a4e5405 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 19:24:22 -0700 Subject: [PATCH 21/40] adjust elevator model --- Elevator/Models/ElevatorInBuilding.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs index f45e528..31a315c 100644 --- a/Elevator/Models/ElevatorInBuilding.cs +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -40,7 +40,7 @@ public void RequestFloor(int floor) Console.WriteLine(4141); floorRequests.Add(floor); - Run(); + // Run(); } // Run the elevator, note on request floor we set the direction From ba57980af6ccd0c78f852910f6b5b0db2bf9af56 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 19:24:44 -0700 Subject: [PATCH 22/40] add test to verify multiple floors can be added --- Elevator.Tests/ModelTests/ElevatorTests.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index 91db903..7e8423f 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using Newtonsoft.Json; +using System.Threading; namespace Elevator.Tests { @@ -30,6 +31,21 @@ public void AddFloor_AddFloorToVisit_ListOfInt() CollectionAssert.AreEqual(expectedAnswer, newElevator.floorRequests); } + [TestMethod] + public void Run_RunAfterAddFloorToVisit_Void() + { + ElevatorInBuilding newElevator = new ElevatorInBuilding(); + int floorToVisit = 5; + int floorToVisit2 = 2; + newElevator.RequestFloor(floorToVisit); + Thread.Sleep(3000); + newElevator.RequestFloor(floorToVisit2); + // newElevator.RequestFloor + List expectedAnswer = new List { 5, 2 }; + Console.WriteLine(JsonConvert.SerializeObject(newElevator.floorRequests)); + CollectionAssert.AreEqual(expectedAnswer, newElevator.floorRequests); + } + // [TestMethod] // // naming convention is MethodOrFieldName_Description_ReturnType // public void AddFloor_AddFloorToVisitMultipleTimesAndSort_ListOfEvents() From 23047eb3f7f624d3eae986aa8ae4a4de2e40edda Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 19:25:42 -0700 Subject: [PATCH 23/40] adjust program to make elevator run in loop --- Elevator/Program.cs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/Elevator/Program.cs b/Elevator/Program.cs index 8dba100..984b249 100644 --- a/Elevator/Program.cs +++ b/Elevator/Program.cs @@ -1,15 +1,28 @@ -// See https://aka.ms/new-console-template for more information -bool IsOn = true; -while (IsOn) +using Elevator.Models; +// See https://aka.ms/new-console-template for more information +ElevatorInBuilding elevator = new ElevatorInBuilding(); + +while (true) { - Console.WriteLine("Hello, World, enter a floor or q to quit"); - string floor = Console.ReadLine(); - Console.WriteLine($"you want floor {floor}"); + Console.WriteLine("Enter a floor request as a number or 'q' to quit, 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("16"); + elevator.RequestFloor(floor); + elevator.Run(); + } - if (floor == "q") + if (elevator.direction == Direction.Idle) { - IsOn = false; - Console.WriteLine("turn elevator off"); + Console.WriteLine("Elevator is not moving. Starting elevator..."); + elevator.Run(); } } + From c8b76cb46cbee66296938076396d29466bfa1657 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Mon, 18 Sep 2023 19:30:18 -0700 Subject: [PATCH 24/40] add to readme --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 551aa9c..d30dee9 100644 --- a/README.md +++ b/README.md @@ -62,4 +62,9 @@ I can collect floors to visit, this is made into event objects and events sorted // new idea focus on the direction the elevator goes and what happens between the floors -Up, Down, Stopped(Idle) \ No newline at end of file +Up, Down, Stopped(Idle) + +//setup notes +git branch -M main +git remote add origin https://github.com/dan-kiss-dev-this/dan-kiss-dev-this-SmaElevatorDk.git +git push -u origin main \ No newline at end of file From 7255c6ee0ee43cf1fa8117ce3ecc35d4eb47ff09 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Tue, 19 Sep 2023 14:25:20 -0700 Subject: [PATCH 25/40] add in way to sort order floors go in --- Elevator/Models/ElevatorInBuilding.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs index 31a315c..3c3fa75 100644 --- a/Elevator/Models/ElevatorInBuilding.cs +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -2,6 +2,7 @@ using System; using System.Threading; using System.Linq; +using Newtonsoft.Json; public enum Direction { @@ -39,7 +40,10 @@ public void RequestFloor(int floor) } Console.WriteLine(4141); + floorRequests.Add(floor); + //need to sort the floors here + floorRequests.Sort(); // Run(); } From 845ca5a0d8218ef2b220654e7d9795562831e8de Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Tue, 19 Sep 2023 14:36:39 -0700 Subject: [PATCH 26/40] add a 3 second delay to adding a floor if you are one floor away --- Elevator/Models/ElevatorInBuilding.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs index 3c3fa75..e649f9e 100644 --- a/Elevator/Models/ElevatorInBuilding.cs +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -42,7 +42,11 @@ public void RequestFloor(int floor) Console.WriteLine(4141); floorRequests.Add(floor); - //need to sort the floors here + //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(); } From 174a1d76390cc080f9ca7a1b3ca6fdf4d0f35120 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Tue, 19 Sep 2023 14:38:13 -0700 Subject: [PATCH 27/40] update testing to account for sorting --- Elevator.Tests/ModelTests/ElevatorTests.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index 7e8423f..a70c979 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -36,12 +36,11 @@ public void Run_RunAfterAddFloorToVisit_Void() { ElevatorInBuilding newElevator = new ElevatorInBuilding(); int floorToVisit = 5; - int floorToVisit2 = 2; + int floorToVisit2 = 3; newElevator.RequestFloor(floorToVisit); Thread.Sleep(3000); newElevator.RequestFloor(floorToVisit2); - // newElevator.RequestFloor - List expectedAnswer = new List { 5, 2 }; + List expectedAnswer = new List { 3, 5 }; Console.WriteLine(JsonConvert.SerializeObject(newElevator.floorRequests)); CollectionAssert.AreEqual(expectedAnswer, newElevator.floorRequests); } From 5f9ef666599947b642afd962a39fb1a20bac50cb Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Tue, 19 Sep 2023 15:55:03 -0700 Subject: [PATCH 28/40] update program.cs --- Elevator/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Elevator/Program.cs b/Elevator/Program.cs index 984b249..fb2c8b4 100644 --- a/Elevator/Program.cs +++ b/Elevator/Program.cs @@ -4,7 +4,7 @@ while (true) { - Console.WriteLine("Enter a floor request as a number or 'q' to quit, you can keep adding multiple floors just type them and press enter"); + 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")) { @@ -13,14 +13,14 @@ // check use of out else if (int.TryParse(input, out int floor)) { - Console.WriteLine("16"); + Console.WriteLine("Parsing inputted value to integer"); elevator.RequestFloor(floor); elevator.Run(); } if (elevator.direction == Direction.Idle) { - Console.WriteLine("Elevator is not moving. Starting elevator..."); + Console.WriteLine("Elevator is idle (not moving)."); elevator.Run(); } } From 23965b1e527ec3f965306c0b954532ab3f2c0167 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Tue, 19 Sep 2023 15:55:41 -0700 Subject: [PATCH 29/40] clean up logs in Elevator model --- Elevator/Models/ElevatorInBuilding.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs index e649f9e..ef38f97 100644 --- a/Elevator/Models/ElevatorInBuilding.cs +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -18,6 +18,7 @@ public class ElevatorInBuilding private int currentFloor; public Direction direction; public List floorRequests; + public int nextFloorToVisit; public ElevatorInBuilding() { @@ -28,19 +29,16 @@ public ElevatorInBuilding() public void RequestFloor(int floor) { - Console.WriteLine(3030); if (floor == currentFloor) { return; } if (direction == Direction.Idle) { - Console.WriteLine(3737); direction = (floor > currentFloor) ? Direction.Up : Direction.Down; + Console.WriteLine($"Direction was idle, now being set to {direction}"); } - Console.WriteLine(4141); - floorRequests.Add(floor); //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) @@ -48,7 +46,6 @@ public void RequestFloor(int floor) Thread.Sleep(3000); } floorRequests.Sort(); - // Run(); } // Run the elevator, note on request floor we set the direction @@ -56,9 +53,12 @@ public void Run() { while (floorRequests.Count > 0) { - int nextFloor = GetNextFloor(); - Console.WriteLine(4848); - MoveToFloor(nextFloor); + Console.WriteLine("Calculating next floor"); + int floor = GetNextFloor(); + nextFloorToVisit = floor; + Console.WriteLine("Moving to next floor"); + Console.WriteLine(nextFloorToVisit); + MoveToFloor(nextFloorToVisit); } direction = Direction.Idle; } From df019cdb1cd9cdf65eec66352eb9aadad061d606 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Tue, 19 Sep 2023 16:00:43 -0700 Subject: [PATCH 30/40] update naming of test --- Elevator.Tests/ModelTests/ElevatorTests.cs | 32 +++++++++++++--------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index a70c979..4aa53cc 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -32,33 +32,39 @@ public void AddFloor_AddFloorToVisit_ListOfInt() } [TestMethod] - public void Run_RunAfterAddFloorToVisit_Void() + public void RequestFloor_AddMultipleFloorsInSortedManner_Void() { ElevatorInBuilding newElevator = new ElevatorInBuilding(); int floorToVisit = 5; int floorToVisit2 = 3; newElevator.RequestFloor(floorToVisit); - Thread.Sleep(3000); newElevator.RequestFloor(floorToVisit2); List expectedAnswer = new List { 3, 5 }; Console.WriteLine(JsonConvert.SerializeObject(newElevator.floorRequests)); CollectionAssert.AreEqual(expectedAnswer, newElevator.floorRequests); } + // CheckThatThereIsDelayToAddOneFloorAway idea do 3,4,2 check the current floor or next floor // [TestMethod] - // // naming convention is MethodOrFieldName_Description_ReturnType - // public void AddFloor_AddFloorToVisitMultipleTimesAndSort_ListOfEvents() + // public void Run_CheckThatThereIsDelayToAddOneFloorAway_Void() // { // ElevatorInBuilding newElevator = new ElevatorInBuilding(); - // int floorToVisit1 = 2; - // int floorToVisit2 = 8; - // int floorToVisit3 = 5; - // newElevator.AddEvent(floorToVisit1); - // newElevator.AddEvent(floorToVisit2); - // List expectedAnswer = newElevator.AddEvent(floorToVisit3); - // // List sample = new List { new ElevatorEvent(1) }; - // Console.WriteLine(JsonConvert.SerializeObject(expectedAnswer)); - // CollectionAssert.AreEqual(expectedAnswer, newElevator.EventsOrderedByFloor); + // int floorToVisit = 3; + // int floorToVisit2 = 4; + // int floorToVisit3 = 2; + // newElevator.RequestFloor(floorToVisit); + // //need to Run + // newElevator.Run(); + // Thread.Sleep(2000); + // newElevator.RequestFloor(floorToVisit2); + // Thread.Sleep(2000); + // int nextFloorYouGoTo = newElevator.nextFloorToVisit; + // Console.WriteLine(JsonConvert.SerializeObject(nextFloorYouGoTo)); + // Assert.AreEqual(nextFloorYouGoTo, floorToVisit); + // newElevator.RequestFloor(floorToVisit3); + // List expectedAnswer = new List { 3, 4, 1 }; + // Console.WriteLine(JsonConvert.SerializeObject(newElevator.floorRequests)); + // CollectionAssert.AreEqual(expectedAnswer, newElevator.floorRequests); // } From 5482a4731c3aa677fc04ac035dc2a597f0a62b2d Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Tue, 19 Sep 2023 16:54:32 -0700 Subject: [PATCH 31/40] add test to confirm direction of elevator on floor request --- Elevator.Tests/ModelTests/ElevatorTests.cs | 37 +++++++++++----------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index 4aa53cc..671d716 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -41,33 +41,34 @@ public void RequestFloor_AddMultipleFloorsInSortedManner_Void() newElevator.RequestFloor(floorToVisit2); List expectedAnswer = new List { 3, 5 }; Console.WriteLine(JsonConvert.SerializeObject(newElevator.floorRequests)); + Assert.AreEqual(newElevator.direction, Direction.Up); CollectionAssert.AreEqual(expectedAnswer, newElevator.floorRequests); } - // CheckThatThereIsDelayToAddOneFloorAway idea do 3,4,2 check the current floor or next floor + [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_CheckThatThereIsDelayToAddOneFloorAway_Void() + // public void Run_ElevatorMovesToRequestedFloors() // { + // // Arrange // ElevatorInBuilding newElevator = new ElevatorInBuilding(); - // int floorToVisit = 3; - // int floorToVisit2 = 4; - // int floorToVisit3 = 2; + // int floorToVisit = 5; + // int floorToVisit2 = 3; // newElevator.RequestFloor(floorToVisit); - // //need to Run - // newElevator.Run(); - // Thread.Sleep(2000); // newElevator.RequestFloor(floorToVisit2); - // Thread.Sleep(2000); - // int nextFloorYouGoTo = newElevator.nextFloorToVisit; - // Console.WriteLine(JsonConvert.SerializeObject(nextFloorYouGoTo)); - // Assert.AreEqual(nextFloorYouGoTo, floorToVisit); - // newElevator.RequestFloor(floorToVisit3); - // List expectedAnswer = new List { 3, 4, 1 }; - // Console.WriteLine(JsonConvert.SerializeObject(newElevator.floorRequests)); - // CollectionAssert.AreEqual(expectedAnswer, newElevator.floorRequests); - // } - + // newElevator.Run(); + // CollectionAssert.AreEqual(newElevator.floorRequests, new List()); + // } } } \ No newline at end of file From 20a5ff35913bf7ec1efb491fd40d491a43e5be1f Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Tue, 19 Sep 2023 16:55:13 -0700 Subject: [PATCH 32/40] move enum to namespace to pass testing --- Elevator/Models/ElevatorInBuilding.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs index ef38f97..92bdf1b 100644 --- a/Elevator/Models/ElevatorInBuilding.cs +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -4,18 +4,17 @@ using System.Linq; using Newtonsoft.Json; -public enum Direction -{ - Up, //0 - Down, //1 - Idle //2 -} - namespace Elevator.Models { + public enum Direction + { + Up, //0 + Down, //1 + Idle //2 + } public class ElevatorInBuilding { - private int currentFloor; + public int currentFloor; public Direction direction; public List floorRequests; public int nextFloorToVisit; From dd15f50f85c7bb5224d8edd50b26ed725bbb14c4 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Tue, 19 Sep 2023 17:02:19 -0700 Subject: [PATCH 33/40] add test that shows calculation of next floor to visit --- Elevator.Tests/ModelTests/ElevatorTests.cs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index 671d716..07ba809 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -56,19 +56,14 @@ public void RequestFloor_DirectionIsUp_Void() Assert.AreEqual(newElevator.direction, Direction.Up); } - // [TestMethod] - // public void Run_ElevatorMovesToRequestedFloors() - // { - // // Arrange - // ElevatorInBuilding newElevator = new ElevatorInBuilding(); - // int floorToVisit = 5; - // int floorToVisit2 = 3; - // newElevator.RequestFloor(floorToVisit); - // newElevator.RequestFloor(floorToVisit2); - // newElevator.Run(); - - // CollectionAssert.AreEqual(newElevator.floorRequests, new List()); - // } + public void Run_NextFloorToVisitIsCalculated_Void() + { + ElevatorInBuilding newElevator = new ElevatorInBuilding(); + int floorToVisit = 5; + newElevator.RequestFloor(floorToVisit); + newElevator.Run(); + Assert.AreEqual(newElevator.nextFloorToVisit, floorToVisit); + } } } \ No newline at end of file From a6dbadff5a77ac77ae5fce5110bd49d3ec67bf37 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Tue, 19 Sep 2023 17:08:02 -0700 Subject: [PATCH 34/40] add testing that shows floorRequests are removed after they are visted --- Elevator.Tests/ModelTests/ElevatorTests.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index 07ba809..14e1532 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -65,5 +65,17 @@ public void Run_NextFloorToVisitIsCalculated_Void() Assert.AreEqual(newElevator.nextFloorToVisit, floorToVisit); } + public void Run_FloorRequestsAreRemovedAfterVisited_Void() + { + ElevatorInBuilding newElevator = new ElevatorInBuilding(); + int floorToVisit = 5; + int floorToVisit2 = 3; + newElevator.RequestFloor(floorToVisit); + newElevator.RequestFloor(floorToVisit2); + newElevator.Run(); + List blankList = new List { }; + Assert.AreEqual(newElevator.floorRequests, blankList); + } + } } \ No newline at end of file From 4cb7660316f64dd27b3281a8d8eb7d7035e21eb0 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Tue, 19 Sep 2023 17:20:07 -0700 Subject: [PATCH 35/40] add notes to readme --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d30dee9..34650be 100644 --- a/README.md +++ b/README.md @@ -67,4 +67,14 @@ Up, Down, Stopped(Idle) //setup notes git branch -M main git remote add origin https://github.com/dan-kiss-dev-this/dan-kiss-dev-this-SmaElevatorDk.git -git push -u origin main \ No newline at end of file +git push -u origin main + +log time Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); + +Timestamp and asynchronous floor request, every time one occurs. +Timestamp and floor, every time elevator passes a floor. +Timestamp and floor, every time elevator stops at a floor. + + +//need a way to keep being able to accept input on the console +//need to log the events \ No newline at end of file From c159cbc491d7e880b5e75b4eb018dc0e89124fff Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Tue, 19 Sep 2023 17:20:53 -0700 Subject: [PATCH 36/40] rewrite elevator event to focus on needed event information only --- Elevator/Models/ElevatorEvent.cs | 39 +++++++------------------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/Elevator/Models/ElevatorEvent.cs b/Elevator/Models/ElevatorEvent.cs index 653967f..f3421da 100644 --- a/Elevator/Models/ElevatorEvent.cs +++ b/Elevator/Models/ElevatorEvent.cs @@ -2,42 +2,19 @@ namespace Elevator.Models { + // Timestamp and asynchronous floor request, every time one occurs. + // Timestamp and floor, every time elevator passes a floor. + // Timestamp and floor, every time elevator stops at a floor. public class ElevatorEvent + + // 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 ElevatorEvent( - int FloorRequested - // bool DirectionOfElevatorUp, - // bool DirectionOfEventUp, - // bool FloorRequestedBool, - // bool FloorOn, - // bool FloorPassedBool, - // bool FloorStopped, - // bool CompletedEvent - ) + public DateTimeOffset Now { get; set; } + public ElevatorEvent(string type) { this.Now = (DateTimeOffset)DateTime.UtcNow; this.FloorRequested = FloorRequested; - - // this.DirectionOfElevatorUp = DirectionOfElevatorUp; } - // Timestamp (Date and time including seconds) - use for timing // using System; - public DateTimeOffset Now { get; set; } - - public int FloorRequested { get; set; } - - public bool DirectionOfElevatorUp { get; set; } - // bool used to determine if elevator will hit this floor when going up if true - - public bool DirectionOfEventUp { get; set; } - // bool used when floor is Requested - public bool FloorRequestedBool { get; set; } - // int is floor elevator has moved to - public int FloorOn { get; set; } - // FloorPassedBool bool used when floor is passed - public bool FloorPassedBool { get; set; } - // bool used when the floor is stoppedon - public bool FloorStopped { get; set; } - // bool event is done - public bool CompletedEvent { get; set; } + } } \ No newline at end of file From 704679a87a4d8913516f38b6afa2737d143d4693 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Wed, 20 Sep 2023 12:58:32 -0700 Subject: [PATCH 37/40] add enum to TypeOfEvent and include in EvevatorEvent --- Elevator/Models/ElevatorEvent.cs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Elevator/Models/ElevatorEvent.cs b/Elevator/Models/ElevatorEvent.cs index f3421da..c9618ad 100644 --- a/Elevator/Models/ElevatorEvent.cs +++ b/Elevator/Models/ElevatorEvent.cs @@ -2,19 +2,26 @@ namespace Elevator.Models { - // Timestamp and asynchronous floor request, every time one occurs. - // Timestamp and floor, every time elevator passes a floor. - // Timestamp and floor, every time elevator stops at a floor. - public class ElevatorEvent + // 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 ElevatorEvent(string type) + public TypeOfEvent TypeOfEvent { get; set; } + public ElevatorEvent(TypeOfEvent type) { - this.Now = (DateTimeOffset)DateTime.UtcNow; - this.FloorRequested = FloorRequested; + Now = (DateTimeOffset)DateTime.UtcNow; + TypeOfEvent = type; } - + } } \ No newline at end of file From 4ce2b96bd9b08df156c342fbc0092fb686401393 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Wed, 20 Sep 2023 13:37:56 -0700 Subject: [PATCH 38/40] add testing to verify elevator is tracking events --- Elevator.Tests/ModelTests/ElevatorTests.cs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Elevator.Tests/ModelTests/ElevatorTests.cs b/Elevator.Tests/ModelTests/ElevatorTests.cs index 14e1532..9ab0e99 100644 --- a/Elevator.Tests/ModelTests/ElevatorTests.cs +++ b/Elevator.Tests/ModelTests/ElevatorTests.cs @@ -56,6 +56,7 @@ public void RequestFloor_DirectionIsUp_Void() Assert.AreEqual(newElevator.direction, Direction.Up); } + [TestMethod] public void Run_NextFloorToVisitIsCalculated_Void() { ElevatorInBuilding newElevator = new ElevatorInBuilding(); @@ -65,6 +66,7 @@ public void Run_NextFloorToVisitIsCalculated_Void() Assert.AreEqual(newElevator.nextFloorToVisit, floorToVisit); } + [TestMethod] public void Run_FloorRequestsAreRemovedAfterVisited_Void() { ElevatorInBuilding newElevator = new ElevatorInBuilding(); @@ -73,9 +75,23 @@ public void Run_FloorRequestsAreRemovedAfterVisited_Void() newElevator.RequestFloor(floorToVisit); newElevator.RequestFloor(floorToVisit2); newElevator.Run(); - List blankList = new List { }; - Assert.AreEqual(newElevator.floorRequests, blankList); + List blankList = new List() { }; + 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 eventList = new List { newEvent1, newEvent2, newEvent3, newEvent4 }; + Assert.AreEqual(newElevator.events.Count, eventList.Count); + } } } \ No newline at end of file From e26efe4698ae7aab73dcab6a5744d1acbcc3d017 Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Wed, 20 Sep 2023 13:39:48 -0700 Subject: [PATCH 39/40] adjust ElevatorInBuilding model to pass testing --- Elevator/Models/ElevatorInBuilding.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Elevator/Models/ElevatorInBuilding.cs b/Elevator/Models/ElevatorInBuilding.cs index 92bdf1b..36f277f 100644 --- a/Elevator/Models/ElevatorInBuilding.cs +++ b/Elevator/Models/ElevatorInBuilding.cs @@ -16,14 +16,15 @@ public class ElevatorInBuilding { public int currentFloor; public Direction direction; - public List floorRequests; + public List floorRequests = new List(); public int nextFloorToVisit; + public List events = new List(); public ElevatorInBuilding() { currentFloor = 1; direction = Direction.Idle; - floorRequests = new List(); + // floorRequests = new List(); } public void RequestFloor(int floor) @@ -39,6 +40,8 @@ public void RequestFloor(int floor) } 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) { @@ -86,6 +89,7 @@ private void MoveToFloor(int targetFloor) Thread.Sleep(3000); currentFloor++; Console.WriteLine($"Arrived at Floor {currentFloor}"); + events.Add(new ElevatorEvent(TypeOfEvent.PassFloor)); } } else if (currentFloor > targetFloor) @@ -96,9 +100,11 @@ private void MoveToFloor(int 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); } From 15168374f9cb19d1b09be2a5327570d4f972aead Mon Sep 17 00:00:00 2001 From: Daniel Kiss Date: Wed, 20 Sep 2023 14:31:49 -0700 Subject: [PATCH 40/40] update readme --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 34650be..3f6f7ce 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +DK Notes, I had 2 approach ideas. First was to focus on the events on the elevator, this did not work as I had too may variables to track. I scrapped that approach and started fresh by focusing on the elevator and writing tests, I wrote the test before the code in some cases also to be doing test driven development. I built a way to store the floors the elevator has to visit and write testing for that. Then I added testing to confirm that the elevator could have floors requested not in order and that they could be sorted and have the elevator stop on the way up at floors above in order. Then I added testing to confirm the direction of the elevator was tracked, and the floors once visited are removed. I ended up adding in the events last using a test driven approach. I built a simple event object. Wrote tests that as the elevator was called to floor 3 from a starting point of floor 1 would have 4 events generate. Ran the test, confirmed it did not pass. Wrote the needed code, then ran the test again and confirmed it passed. + +What I would do with more time? I would take this console app and make this program run as a Web Api including making a controller with actions to collect different floors with a fun React based frontend. + +Prompt below and my notes are under that: Build an Elevator Coding Challenge! The Challenge Create an application that simulates the operation of a simple elevator. @@ -20,6 +25,8 @@ Bonus Enhancement: Enhance the application as follows: If the elevator has reached its weight limit, it should stop only at floors that were selected from inside the elevator (to let passengers out), until it is no longer at the max weight limit. Note: For simplicity, the asynchronous request buttons can be entered by the application user via the console, by entering "5U" (request from 5th floor wanting to go Up) or "8D" (request from 8th floor wanting to go Down) or "2" (request from inside elevator wanting to stop at 2nd floor). When the user enters "Q" on the console, the application must end after visiting all floors entered before "Q". +/////// Notes from the project, note these notes contain the old event based approach as well as the new elevator based approach. + DK ideas- use the elevator events to track what the elevator will do, Elevator fields and methods