-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
467 lines (391 loc) · 18.5 KB
/
Copy pathProgram.cs
File metadata and controls
467 lines (391 loc) · 18.5 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
using System;
using System.Collections.Generic;
using System.Linq;
namespace BuildingManager
{
internal class Program
{
private static Section _selectedSection;
private static Default _selectedDevice;
private static readonly Building Building = new Building();
#region Initialization
// Simply populate building with some data
private static void PopulateBuilding()
{
// SectionA
var sectionA = new Section("SectionA");
sectionA.DeviceAdded += Helper.OnSectionModified;
sectionA.SectionRenamed += Helper.OnSectionModified;
sectionA.CardReaders.AddRange(new List<CardReader>()
{
new CardReader("MainDoorReader"),
new CardReader("CaffeeCardReader"),
new CardReader("FrontGateReader")
});
foreach (var aCard in sectionA.CardReaders)
{
aCard.CardReaderModified += Helper.OnDeviceModified;
aCard.DeviceRenamed += Helper.OnDeviceModified;
}
sectionA.Speakers.AddRange(new List<Speaker>()
{
new Speaker("Subwoofer"),
new Speaker("FrontSpeaker"),
new Speaker("SideSpeakerLeft"),
new Speaker("SideSpeakerRight"),
new Speaker("RareSpeaker")
});
foreach (var aSpeaker in sectionA.Speakers)
{
aSpeaker.SpeakerModified += Helper.OnDeviceModified;
aSpeaker.DeviceRenamed += Helper.OnDeviceModified;
}
// SectionB
var sectionB = new Section("SectionB");
sectionB.DeviceAdded += Helper.OnSectionModified;
sectionB.SectionRenamed += Helper.OnSectionModified;
sectionB.Doors.AddRange(new List<Door>()
{
new Door("FrontGate"),
new Door("CanteenDoor"),
new Door("BackDoor")
});
foreach (var bDoor in sectionB.Doors)
{
bDoor.DoorModified += Helper.OnDeviceModified;
bDoor.DeviceRenamed += Helper.OnDeviceModified;
}
sectionB.LedPanels.AddRange(new List<LedPanel>()
{
new LedPanel("FrontGateLabel"),
new LedPanel("HallPanel")
});
foreach (var bPanel in sectionB.LedPanels)
{
bPanel.LedPanelModified += Helper.OnDeviceModified;
bPanel.DeviceRenamed += Helper.OnDeviceModified;
}
sectionB.CardReaders.AddRange(new List<CardReader>()
{
new CardReader("FrontGateCardReader"),
new CardReader("HeadmasterDoorReader")
});
foreach (var bCard in sectionB.CardReaders)
{
bCard.CardReaderModified += Helper.OnDeviceModified;
bCard.DeviceRenamed += Helper.OnDeviceModified;
}
Building.Sections.Add(sectionA);
Building.Sections.Add(sectionB);
}
// Does some initial moving, renaming, deleting, etc...
private static void Initialize()
{
// MOVE DEVICE TO ANOTHER LOCATION
Console.WriteLine("Moving 'Subwoofer' from 'SectionA' -> 'SectionB'");
_selectedSection = Building.Sections.SingleOrDefault(sec => sec.Name == "SectionA");
_selectedDevice = _selectedSection?.FindDeviceByName(Device.Speaker, "Subwoofer");
MoveDeviceToAnotherSection(Building.Sections.SingleOrDefault(sec => sec.Name == "SectionB"));
// RENAME SECTION
Console.WriteLine("Renaming section 'SectionA' -> 'MainSection'");
_selectedSection.Name = "MainSection";
// Change Access Number
Console.WriteLine("Changing AccessNumber at 'MainDoorReader' -> 'A01234DE7FFF'");
_selectedDevice = _selectedSection.FindDeviceByName(Device.CardReader, "MainDoorReader");
((CardReader)_selectedDevice).AccessCardNumber = "A01234DE7FFF";
// Delete device
Console.WriteLine("Deleting 'RareSpeaker' from 'MainSection'");
_selectedSection = Building.Sections.SingleOrDefault(sec => sec.Name == "MainSection");
_selectedDevice = _selectedSection.FindDeviceByName(Device.Speaker, "RareSpeaker");
RemoveSelectedDeviceFromSelectedSection();
Console.WriteLine("INITIALIZATION DONE!\n\n");
_selectedSection = null;
_selectedDevice = null;
}
#endregion
private static void Main()
{
PopulateBuilding();
Helper.Greetings();
Initialize();
Helper.PrintHelp();
var input = "";
// Main App Loop, listening for commands
while (input != "exit")
{
input = Console.ReadLine();
switch (input?.ToLower())
{
case "help":
case "h":
Helper.PrintHelp();
break;
case "building plan":
case "plan":
Building.BuildingPlan();
break;
// SECTION COMMANDS
case "section":
if (!CheckIfSelectedSectionIsNull()) { Helper.SectionSelectedMessage(_selectedSection); }
break;
case "new section":
Console.WriteLine("What is the name of this section?");
var newSectionName = Console.ReadLine();
if (!CheckSectionNameAvailability(newSectionName))
{
Helper.PrintError("Sorry, this section already exists.");
continue;
}
Building.AddSection(newSectionName);
break;
case "select section":
case "section select":
_selectedSection = GetSection();
if (CheckIfSelectedSectionIsNull()) { continue; }
Helper.SectionSelectedMessage(_selectedSection);
break;
case "section info":
if (CheckIfSelectedSectionIsNull()) { continue; }
Helper.PrintSectionInfo(_selectedSection);
break;
case "rename section":
case "section rename":
if (CheckIfSelectedSectionIsNull()) { continue; }
Console.WriteLine("Enter new name for selected section...");
_selectedSection.Name = Console.ReadLine();
break;
case "delete section":
case "section delete":
if (CheckIfSelectedSectionIsNull()) { continue; }
Building.Sections.Remove(_selectedSection);
Helper.PrintSectionDeleted(_selectedSection);
_selectedSection = null;
break;
// DEVICE COMMANDS
case "device":
if (!CheckIfSelectedDeviceIsNull()) { Helper.DeviceSelectedMessage(_selectedDevice); }
break;
case "select device":
if (CheckIfSelectedSectionIsNull()) { continue; }
_selectedDevice = GetDevice();
if (!CheckIfSelectedDeviceIsNull()) { Helper.DeviceSelectedMessage(_selectedDevice); }
break;
case "new device":
if (CheckIfSelectedSectionIsNull()) { continue; }
var (device, name) = GetDeviceTypeAndName();
_selectedSection.AddDevice(device, name);
break;
case "rename device":
case "device rename":
if (CheckIfSelectedDeviceIsNull()) { continue; }
Console.WriteLine("Enter new name for device...");
_selectedDevice.Rename(Console.ReadLine());
break;
case "device info":
if (!CheckIfSelectedDeviceIsNull()) { Helper.PrintDeviceInfo(_selectedDevice); }
break;
case "delete device":
case "delete":
if (CheckIfSelectedSectionIsNull() || CheckIfSelectedDeviceIsNull()) { continue; }
RemoveSelectedDeviceFromSelectedSection();
break;
case "move device":
case "move":
if (CheckIfSelectedSectionIsNull() || CheckIfSelectedDeviceIsNull()) { continue; }
var targetSection = GetSection();
if (targetSection == null)
{
Helper.PrintError("Target Section Not Found!");
continue;
}
MoveDeviceToAnotherSection(targetSection);
break;
// LEDPANEL MANIPULATION
case "change text":
if (CheckIfSelectedDeviceIsMatch(Device.LedPanel))
{
Console.WriteLine("Enter new text...");
(_selectedDevice as LedPanel).Message = Console.ReadLine();
}
break;
// DOOR MANIPULATION
case "open door":
case "open":
if (!CheckIfSelectedDeviceIsNull() && CheckIfSelectedDeviceIsMatch(Device.Door))
{
(_selectedDevice as Door).OpenDoor();
}
break;
case "lock door":
case "lock":
if (!CheckIfSelectedDeviceIsNull() && CheckIfSelectedDeviceIsMatch(Device.Door))
{
(_selectedDevice as Door).LockDoor();
}
break;
case "opentoolong door":
case "opentoolong":
case "toolong":
if (!CheckIfSelectedDeviceIsNull() && CheckIfSelectedDeviceIsMatch(Device.Door))
{
(_selectedDevice as Door).SetOpenForTooLong();
}
break;
case "openforcibly door":
case "door openforcibly":
case "openforcibly":
case "forcibly":
if (!CheckIfSelectedDeviceIsNull() && CheckIfSelectedDeviceIsMatch(Device.Door))
{
(_selectedDevice as Door).SetOpenedForcibly();
}
break;
// SPEAKER MANIPULATION
case "volume":
case "change volume":
if (CheckIfSelectedDeviceIsNull() || !CheckIfSelectedDeviceIsMatch(Device.Speaker)) { continue; }
Console.WriteLine("Enter new volume (Number between 0 and 100)...");
if (!float.TryParse(Console.ReadLine(), out var parsedInput))
{
Helper.PrintError("Enter a number");
continue;
}
(_selectedDevice as Speaker).Volume = parsedInput;
break;
case "sound":
case "change sound":
if (CheckIfSelectedDeviceIsNull() || !CheckIfSelectedDeviceIsMatch(Device.Speaker)) { continue; }
Console.WriteLine($"What do you want {_selectedDevice.Name} to play? (Alarm/Music/None)");
switch (Console.ReadLine())
{
case "Alarm":
case "alarm":
(_selectedDevice as Speaker).PlayAlarm();
break;
case "Music":
case "music":
(_selectedDevice as Speaker).PlayMusic();
break;
case "None":
case "none":
(_selectedDevice as Speaker).StopPlaying();
break;
default:
Helper.PrintError("Invalid sound. Select 'Alarm/Music/None'.");
break;
}
break;
// CARD MANIPULATION
case "access":
case "change access":
if (!CheckIfSelectedDeviceIsMatch(Device.CardReader)) { continue; }
Console.WriteLine("Please enter new card number...");
(_selectedDevice as CardReader).AccessCardNumber = Console.ReadLine();
break;
// If Command was invalid or was not recognized
default:
Helper.PrintError("Unknown Command!");
break;
}
// print new line for cleaner output
Console.WriteLine();
}
}
#region CheckMethods
private static bool CheckIfSelectedSectionIsNull()
{
if (!(_selectedSection is null)) return false;
Helper.PrintError("No section selected.");
return true;
}
private static bool CheckIfSelectedDeviceIsNull()
{
if (!(_selectedDevice is null)) return false;
Helper.PrintError("Unknown Device!");
return true;
}
private static bool CheckIfSelectedDeviceIsMatch(Device device)
{
if (_selectedDevice.Type == device) return true;
Helper.PrintError($"You must select {device} to modify it.");
return false;
}
// Returns false if section with given name already exists
private static bool CheckSectionNameAvailability(string name) =>
Building.Sections.SingleOrDefault(x => x.Name == name) == null;
#endregion
// Moves _selectedDevice into targetSection
private static void MoveDeviceToAnotherSection(Section targetSection)
{
switch (_selectedDevice.Type)
{
case Device.Door:
targetSection.Doors.Add(_selectedDevice as Door);
_selectedSection.Doors.Remove(_selectedDevice as Door);
break;
case Device.Speaker:
targetSection.Speakers.Add(_selectedDevice as Speaker);
_selectedSection.Speakers.Remove(_selectedDevice as Speaker);
break;
case Device.LedPanel:
targetSection.LedPanels.Add(_selectedDevice as LedPanel);
_selectedSection.LedPanels.Remove(_selectedDevice as LedPanel);
break;
case Device.CardReader:
targetSection.CardReaders.Add(_selectedDevice as CardReader);
_selectedSection.CardReaders.Remove(_selectedDevice as CardReader);
break;
}
Helper.PrintDeviceMoved(_selectedDevice, _selectedSection, targetSection);
}
private static void RemoveSelectedDeviceFromSelectedSection()
{
switch (_selectedDevice.Type)
{
case Device.Door:
_selectedSection.Doors.Remove(_selectedDevice as Door);
break;
case Device.Speaker:
_selectedSection.Speakers.Remove(_selectedDevice as Speaker);
break;
case Device.LedPanel:
_selectedSection.LedPanels.Remove(_selectedDevice as LedPanel);
break;
case Device.CardReader:
_selectedSection.CardReaders.Remove(_selectedDevice as CardReader);
break;
}
Helper.PrintDeviceDeletedFromSectionMessage(_selectedDevice, _selectedSection);
_selectedDevice = null;
}
#region GetMethods
// Returns Device object or null if not found
private static Default GetDevice()
{
var (type, name) = GetDeviceTypeAndName();
return _selectedSection.FindDeviceByName(type, name);
}
// TODO CHECK WRONG INPUT
// Asks user to type DeviceType and name -> parses the type into correct enum, returns tuple
private static (Device, string) GetDeviceTypeAndName()
{
Console.WriteLine("What device (CardReader/Door/LedPanel/Speaker) and name? Ex.: 'Door MainDoor'");
var ln = Console.ReadLine()?.Trim().Split(' ');
string name = null;
if (ln?.Length == 2)
{
name = ln[1];
}
Enum.TryParse(ln?[0], out Device type);
return (type, name);
}
// Asks user to type section -> returns Section object or null if not found
private static Section GetSection()
{
Console.WriteLine("Please enter section name...");
var section = Console.ReadLine();
return Building.Sections.FirstOrDefault(x => x.Name == section);
}
#endregion
}
}