-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
140 lines (116 loc) · 4.14 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Interfaces;
using Ninject;
namespace ElevatorRunner
{
class Program
{
private static Floor currentFloor;
private static IElevatorStatus elevatorStatus;
private static IElevatorControls elevatorControls;
private static readonly List<Person> People = new List<Person>();
static void Main()
{
Bootstrap();
DrawInitialState();
//some sample calls here. Feel free to add more
People.Add(new Person { StartingFloor = Floor.Four, Destination = Floor.Twelve, DelayInSeconds = 0});
People.Add(new Person { StartingFloor = Floor.Eight, Destination = Floor.Eleven, DelayInSeconds = 10});
People.Add(new Person { StartingFloor = Floor.Ground, Destination = Floor.Twelve, DelayInSeconds = 15 });
People.Add(new Person { StartingFloor = Floor.Eight, Destination = Floor.Ground, DelayInSeconds = 10 });
TestElevator();
Console.ReadLine();
}
private static void TestElevator()
{
foreach (Person person in People)
{
Task.Delay(person.DelayInSeconds * 1000).ContinueWith(x =>
{
person.Status = Status.Waiting;
elevatorControls.CallElevator(person.StartingFloor, person.Direction);
Log($"Calling Elevator to floor {person.StartingFloor} in direction {person.Direction}"); });
}
}
private static void Bootstrap()
{
IKernel kernel = new StandardKernel();
//Perform binding here
//See https://github.com/ninject/Ninject/wiki/Dependency-Injection-With-Ninject for help
//Hint: You may need to use Bind<A, B>() depending on your implementation
//Hint: You may need .InSingletonScope()
elevatorStatus = kernel.Get<IElevatorStatus>();
elevatorControls = kernel.Get<IElevatorControls>();
elevatorStatus.FloorChanged += ElevatorStatusOnFloorChanged;
}
private static void DrawInitialState()
{
Console.WriteLine("Olmec elevator");
Console.WriteLine(" G: [|]");
for (int i = 1; i < 13; i++)
Console.WriteLine($"{i.ToString(),2}:");
}
private static void ClearFloor(int floor) => DrawFloor(floor, " ");
private static void DrawElevatorAtFloor(int floor) => DrawFloor(floor, "[|]");
private static void DrawFloor(int floor, string status)
{
Console.SetCursorPosition(0, floor + 1);
if (floor == 0)
Console.Write($" G: {status}");
else
Console.Write($"{floor.ToString(),2}: {status}");
Console.SetCursorPosition(0, 14);
}
private static void ElevatorStatusOnFloorChanged(Floor floor, Direction direction)
{
ClearFloor((int)currentFloor);
currentFloor = floor;
DrawElevatorAtFloor((int)currentFloor);
CheckForPeopleEnteringElevator(floor, direction);
CheckForPeopleLeavingElevator(floor);
}
private static void CheckForPeopleLeavingElevator(Floor floor)
{
IEnumerable<Person> peopleLeavingAtCurrentFloor =
People.Where(x => x.Status == Status.Riding && x.Destination == currentFloor);
foreach (Person person in peopleLeavingAtCurrentFloor)
{
person.Status = Status.Complete;
Log($"Dropping off person at floor {floor}");
}
}
private static void CheckForPeopleEnteringElevator(Floor floor, Direction direction)
{
IEnumerable<Person> peopleWaitingAtCurrentFloor = People.Where(x =>
x.Status == Status.Waiting && x.StartingFloor == currentFloor &&
(x.Direction == direction || direction == Direction.None));
foreach (Person person in peopleWaitingAtCurrentFloor)
{
person.Status = Status.Riding;
elevatorControls.SelectDestination(person.Destination);
Log($"Picking up person at floor {floor}. Destination {person.Destination}");
}
}
private static int lineNumber;
private static int maxLogLines = 10;
private static void Log(string s)
{
Console.SetCursorPosition(0, 16 + lineNumber);
ClearCurrentConsoleLine();
lineNumber++;
Console.WriteLine(s);
if (lineNumber == maxLogLines)
lineNumber = 0;
}
private static void ClearCurrentConsoleLine()
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, currentLineCursor);
}
}
}