-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartMain.cs
More file actions
245 lines (193 loc) · 6.83 KB
/
Copy pathStartMain.cs
File metadata and controls
245 lines (193 loc) · 6.83 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace ElevatorAlgorithm
{
class StartMain
{
static void Main(string[] args)
{
//Assigning all default values like minFloor,maxFloor,maxCapacity and number of elevators from a text file
//this could be used but have to create the file according to the location
List<int> termsList = new List<int>();
String filepath = @"../../../Variables.txt";
List<string> lines = File.ReadAllLines(filepath).ToList();
foreach(var line in lines){
string[] entries = line.Split('=');
termsList.Add(Int32.Parse(entries[1]));
}
//these variables can be changed according to the user in Variables.txt file
//numOfElevators = termsList[0];
//minFloor = termsList[1];
//maxFloor = termsList[2];
//maxCapacity = termsList[3];
Elevator[] elevator = new Elevator[termsList[0]];
// Assigning Lowest Floor
//Highest Floor
//Maximun Capacity
//to all the elevators present
for (int i = 0; i < termsList[0]; i++)
{
elevator[i] = new Elevator(termsList[1], termsList[2], termsList[3]);
}
//Temporary Elevator Object
Elevator change = null;
while (true)
{
//Status of all Elevators
for (int j = 0; j < elevator.Length; j++)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Out.WriteLine("\nElevator " + (j + 1) + " is at Floor----> " + elevator[j].getCurrentFloor()
+ "\t Number of people on it.....>" + elevator[j].getSum());
Console.ForegroundColor = ConsoleColor.White;
}
Console.Out.WriteLine("--------------------------++++++++-------------------------");
// Checking if the user wants Multiple floors call
Console.Out.WriteLine("\nHOW many MULTIPLE FLOORS are people waiting on?");
int numFloors = Int32.Parse(Console.ReadLine());
int[] floor = new int[numFloors];
int[] people = new int[numFloors];
int z = 0;
for (int i = 0; i < numFloors; i++)
{
//User selection of desired floor where people are waiting
while (true)
{
Console.Out.WriteLine("Please enter the NEXT FLOOR NUMBER where people are waiting..("
+ elevator[1].getMinFloor() + " to " + elevator[1].getMaxFloor() + ")");
floor[i] = Int32.Parse(Console.ReadLine());
if (floor[i] < elevator[1].getMinFloor() || floor[i] > elevator[1].getMaxFloor())
{
Console.ForegroundColor = ConsoleColor.DarkRed; //Color Coding for Error Message
Console.Out.WriteLine("FLOOR NUMBER INVALID");
Console.ForegroundColor = ConsoleColor.White;
}
else
{
break;
}
}
// Optimizing the BEST ELEVATOR for that location selected by the user
int[] unsorted = new int[elevator.Length];
int[] sorted = new int[elevator.Length];
for (int j = 0; j < elevator.Length; j++)
{
unsorted[j] = Math.Abs(floor[i] - elevator[j].getCurrentFloor());
sorted[j] = unsorted[j];
}
Array.Sort(sorted);
for (int j = 0; j < sorted.Length; j++)
{
for (int k = 0; k < unsorted.Length; k++)
{
{
if (elevator[k].getIsIdle() == 0 && sorted[j] == unsorted[k])
{
change = elevator[k]; //Selecting the desired elevator into temp
z = k + 1;
elevator[k].setIsIdle(1);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("\nElevator " + z + " is selected\n");
Console.ForegroundColor = ConsoleColor.White;
goto LoopEnd;
}
}
}
}
LoopEnd:
// Registering the User input of amount of users waiting on that desired floor
while (true)
{
Console.Out.WriteLine("Enter the NUMBER OF PEOPLE waiting at level " + floor[i] + "----->MAXIMUM "
+ change.getMaxCapacity());
people[i] = Int32.Parse(Console.ReadLine());
change.setSum(people[i]);
if (change.getSum() > 0 && change.getSum() <= (change.getMaxCapacity()))
{
break;
}
else if (change.getSum() <= 0)
{
change.setSum(-people[i]);
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.Out.WriteLine("PLEASE ENTER A POSITIVE INTEGER");
Console.ForegroundColor = ConsoleColor.White;
}
else
{
change.setSum(-people[i]);
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.Out.WriteLine("SORRY WEIGHT LIMIT OF ELEVATOR " + z + " EXEEDED...\nOnly "
+ (change.getMaxCapacity() - change.getSum()) + " CAN COME ON THIS FLOOR ");
Console.ForegroundColor = ConsoleColor.White;
}
}
// Moving to the selected floor Step by Step
change.moveNext(floor[i], z);
// Assigning the number of people boarding the elevator
change.setPeople(people[i], z);
//Keeps looping until the people on the elevator is 0
while (change.getSum() != 0)
{
// selecting the floor where people want to get down
while (true)
{
Console.Out.WriteLine("Please enter your DESTINATION FLOOR(" + elevator[1].getMinFloor() + " to "
+ elevator[1].getMaxFloor() + ")");
floor[i] = Int32.Parse(Console.ReadLine());
if (floor[i] < elevator[1].getMinFloor() || floor[i] > elevator[1].getMaxFloor())
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.Out.WriteLine("FLOOR NUMBER INVALID");
Console.ForegroundColor = ConsoleColor.White;
}
else
{
break;
}
}
// Registering the number of people getting down at that floor
while (true)
{
Console.Out.WriteLine("Enter the NUMBER OF PEOPLE getting down at " + floor[i]);
people[i] = Int32.Parse(Console.ReadLine());
if (change.getSum() < people[i])
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.Out.WriteLine(
"Sorry there are only " + change.getSum() + " people left on elevator " + z);
Console.ForegroundColor = ConsoleColor.White;
}
else if (people[i] < 0)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.Out.WriteLine("Please enter a positive integer...");
Console.ForegroundColor = ConsoleColor.White;
}
else
{
change.setSum(-people[i]);
change.moveNext(floor[i], z);
change.setPeople(-people[i], z);
break;
}
}
}
// forwarding the values from temporary elevator object to desired elevator object
for (int k = 0; k < elevator.Length; k++)
{
if (z == k + 1)
{
elevator[k] = change;
elevator[k].setIsIdle(-1);
Console.WriteLine("\nElevator " + z + " has ended its action and is idle now \n");
break;
}
}
}
}
}
}
}