-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLift.cs
More file actions
194 lines (156 loc) · 5.12 KB
/
Lift.cs
File metadata and controls
194 lines (156 loc) · 5.12 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
public class Lift
{
public int CurrentFloor { get; set; }
public int CurrentTime { get; set;}
public int Capacity { get; }
public List<Person> PeopleInLift { get; set; }
public List<Person> PeopleWaiting { get; set; }
public Person person;
public List<string> output;
public Lift(List<Person> people)
{
CurrentFloor = 1;
CurrentTime = 0;
Capacity = 8;
PeopleWaiting = new List<Person>(people);
PeopleInLift = new List<Person>();
output = new List<string>();
person = new Person();
}
//methods:
// remove person from call queue/waiting list
public void RemoveFromPeopleWaiting(Person person)
{
PeopleWaiting.Remove(person);
}
// adding a person to the lift:
public void AddPersonToLift (Person person)
{
PeopleInLift.Add(person);
}
// removing person from the lift:
public void RemovePersonFromLift (Person person)
{
PeopleInLift.Remove(person);
}
// checking to see whether the lift can move or not
// checking if there are any pending calls to process or people to move
public bool CanMove()
{
return PeopleWaiting.Count > 0 || PeopleInLift.Count > 0;
}
// getting the next destination floor number
public int GetNextDestination()
{
if (PeopleInLift.Count > 0)
{
return PeopleInLift[0].DestinationFloor;
}
else if (PeopleWaiting.Count > 0)
{
return PeopleWaiting[0].FromFloor;
}
else
{
return 1; // if there is noone in the lift and no calls in the queue then return lift to floor 1
}
}
// deciding if the lift is moving up or down based on call and people in lift:
public void WhichDirection()
{
if (CurrentFloor < PeopleWaiting[0].FromFloor || CurrentFloor < PeopleInLift[0].DestinationFloor)
{
LiftMovingUp();
}
else if (CurrentFloor == 10 || CurrentFloor > PeopleWaiting[0].FromFloor || CurrentFloor > PeopleInLift[0].DestinationFloor)
{
LiftMovingDown();
}
}
public void LiftMovingUp()
{
while (CurrentFloor < PeopleWaiting[0].FromFloor || CurrentFloor < PeopleInLift[0].DestinationFloor)
{
WhenOnNextFloorGoingUp();
}
}
public void WhenOnNextFloorGoingUp()
{
CurrentFloor++;
CurrentTime = CurrentTime + 10; // time takem to move to floor
// check if anyone is waiting on floor
var peopleWaitingOnFloor = PeopleWaiting.Where(person => person.FromFloor == CurrentFloor && person.CallTime <= CurrentTime).ToList();
foreach (var person in peopleWaitingOnFloor)
{
AddPersonToLift(person);
RemoveFromPeopleWaiting(PeopleWaiting[0]);
}
// check if anyone is leaving the lift
var peopleLeavingTheLift = PeopleInLift.Where(person => person.DestinationFloor == CurrentFloor).ToList();
foreach (var person in peopleLeavingTheLift)
{
RemovePersonFromLift(person);
}
string liftStatus = GetLiftStatus();
output.Add(liftStatus);
WriteOutputToCSV();
}
public void LiftMovingDown()
{
while (CurrentFloor == 10 || CurrentFloor > PeopleWaiting[0].FromFloor || CurrentFloor > PeopleInLift[0].DestinationFloor)
{
WhenOnNextFloorGoingDown();
}
}
public void WhenOnNextFloorGoingDown()
{
CurrentFloor--;
CurrentTime = CurrentTime + 10;
// check if anyone is waiting on floor
var peopleWaitingOnFloor = PeopleWaiting.Where(person => person.FromFloor == CurrentFloor && person.CallTime <= CurrentTime).ToList();
foreach (var person in peopleWaitingOnFloor)
{
AddPersonToLift(person);
RemoveFromPeopleWaiting(PeopleWaiting[0]);
}
// check if anyone is leaving the lift
var peopleLeavingTheLift = PeopleInLift.Where(person => person.DestinationFloor == CurrentFloor).ToList();
foreach (var person in peopleLeavingTheLift)
{
RemovePersonFromLift(person);
}
string liftStatus = GetLiftStatus();
output.Add(liftStatus);
WriteOutputToCSV();
}
// on each floor we want to log: the time, current floor, people in the lift and where the lift is going(the destinations of people in the lift)
public string GetLiftStatus()
{
List<string> peopleInLiftList = new List<string>();
List<string> destinationList = new List<string>();
foreach (Person personInLift in PeopleInLift)
{
peopleInLiftList.Add(personInLift.Id.ToString());
destinationList.Add(personInLift.DestinationFloor.ToString());
}
string peopleInLiftStr = string.Join(", ", peopleInLiftList);
string destinationStr = string.Join(", ", destinationList);
Console.WriteLine($"{CurrentTime}, {CurrentFloor}, {peopleInLiftStr}, {destinationStr} ");
return ($"{CurrentTime}| {CurrentFloor}| {peopleInLiftStr}| {destinationStr} ");
}
private void WriteOutputToCSV()
{
string csvPath = "DataOutput.csv";
using (var outputData = new StreamWriter(csvPath))
{
// writing header:
outputData.WriteLine("Time| Current Floor| People In Lift| Call Queue");
// writing the output lines:
foreach (var line in output)
{
outputData.WriteLine(line);
}
}
Console.WriteLine($"Output has been written to {csvPath}.");
}
}