This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
631 lines (602 loc) · 25.9 KB
/
Program.cs
File metadata and controls
631 lines (602 loc) · 25.9 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
using Internship_3_OOP_Calendar.Classes;
using System.Globalization;
using System.Text.RegularExpressions;
namespace Internship_3_OOP_Calendar
{
public class Program
{
#region Filter functions
static List<Event> FilterEvents(List<Event> events, Predicate<Event> predicate)
{
var queried = from e in events
where predicate(e)
select e;
return queried.ToList();
}
static List<Event> PastEvents(List<Event> events) => FilterEvents(events, (e) => e.EndDateTime < DateTime.UtcNow);
static List<Event> UpcomingEvents(List<Event> events) => FilterEvents(events, (e) => e.StartDateTime > DateTime.UtcNow);
static List<Event> ActiveEvents(List<Event> events) => FilterEvents(events, (e) =>
{
return e.StartDateTime < DateTime.UtcNow && e.EndDateTime > DateTime.UtcNow;
}
);
#endregion
#region UI utility functions
static void Spacer()
{
Console.WriteLine(
"-----------------------------------------------------------------------------------"
);
}
static void WriteEvents(List<Event> events, List<Person> people, Action<Event, List<Person>> writeStyle)
{
foreach (var e in events)
{
writeStyle(e, people);
}
Spacer();
}
static void WriteStyleActive(Event e, List<Person> people)
{
TimeSpan tsLength = e.EndDateTime - DateTime.UtcNow;
Spacer();
Console.WriteLine(
$"Id: {e.Id}\n" +
$"Title: {e.Title} - Location: {e.Location} - " +
$"Ends in: {tsLength.ToString("%h")}h {tsLength.ToString("%m")}min\n" +
$"Invited:"
);
foreach (var person in people)
{
if (!person.AttendanceSet(e.Id))
person.Attended(e.Id);
if (e.CheckIsInvited(person.Email))
{
Console.WriteLine($"\t{person.FirstName} {person.LastName} : {person.Email}");
}
}
}
static void WriteStyleUpcoming(Event e, List<Person> people)
{
TimeSpan tsLength = e.EndDateTime - e.StartDateTime;
TimeSpan tsUntill = e.StartDateTime - DateTime.UtcNow;
Spacer();
Console.WriteLine(
$"Id: {e.Id}\n" +
$"Title: {e.Title} - Location: {e.Location} - " +
$"Starts in {tsUntill.ToString("dd")} days {tsUntill.ToString("%h")}h - " +
$"Length: {tsLength.ToString("%h")}h {tsLength.ToString("%m")}min\n" +
$"Invited:"
);
foreach (var person in people)
{
if (e.CheckIsInvited(person.Email))
Console.WriteLine($"\t{person.FirstName} {person.LastName} : {person.Email}");
}
}
static void WriteStylePast(Event e, List<Person> people)
{
TimeSpan tsLength = e.EndDateTime - e.StartDateTime;
TimeSpan tsSince = DateTime.UtcNow - e.EndDateTime;
Spacer();
Console.WriteLine(
$"Id: {e.Id}\n" +
$"Title: {e.Title} - Location: {e.Location} - " +
$"Ended {tsSince.ToString("%d")} days ago - " +
$"Length: {tsLength.ToString("%h")}h {tsLength.ToString("%m")}min\n" +
$"Atended:"
);
foreach (var person in people)
{
if (!e.CheckIsInvited(person.Email))
continue;
if (person.CheckAttended(e.Id))
Console.WriteLine($"\t{person.FirstName} {person.LastName} : {person.Email}");
}
Console.WriteLine("Missing:");
foreach (var person in people)
{
if (!e.CheckIsInvited(person.Email))
continue;
if (!person.CheckAttended(e.Id))
Console.WriteLine($"\t{person.FirstName} {person.LastName} : {person.Email}");
}
}
static void WaitForUser()
{
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
Console.Clear();
}
static void WriteWarning(string warning)
{
Console.WriteLine(warning);
WaitForUser();
}
static int WriteOptions(string[] options)
{
while (true)
{
Console.WriteLine("Available options:");
for (int i = 0; i < options.Length; i++)
{
Console.WriteLine($"{i} - {options[i]}");
}
Console.Write("Input: ");
string? userInput = Console.ReadLine();
if (!int.TryParse(userInput, out int parsedInput))
{
WriteWarning("Please input a valid option!");
continue;
}
if (parsedInput < 0 || parsedInput > options.Length -1)
{
WriteWarning("Please input a valid option!");
continue;
}
return parsedInput;
}
}
static bool GetConfirmation(string question)
{
while (true)
{
Console.WriteLine(question);
Console.Write("Are you sure? Y/N: ");
string answer = Console.ReadLine();
if (string.IsNullOrWhiteSpace(answer))
{
WriteWarning("Please input Y or N!");
continue;
}
if (answer.ToLower() == "y")
{
return true;
}
if (answer.ToLower() == "n")
{
return false;
}
}
}
#endregion
#region Data handling functions
/// <summary>
/// Tries to find an Event object given the id.
/// </summary>
/// <param name="id">Event id to search for.</param>
/// <param name="events">Event collection to search from.</param>
/// <param name="output">Found event.</param>
/// <returns>Boolean indicating wether the lookup was successful.</returns>
static bool TryGetEvent(Guid id, List<Event> events, out Event output)
{
output = null;
foreach (var e in events)
{
if (e.Id == id)
{
output = e;
return true;
}
}
return false;
}
/// <summary>
/// <inheritdoc cref="TryGetEvent(Guid, List{Event}, out Event)"/>
/// </summary>
/// <param name="stringId">Event id string to search for.</param>
/// <param name="events">Event collection to search from.</param>
/// <param name="output">Found event.</param>
/// <returns></returns>
static bool TryGetEvent(string stringId, List<Event> events, out Event output)
{
output = null;
if (!Guid.TryParse(stringId, out Guid id))
{
return false;
}
return TryGetEvent(stringId, events, out output);
}
/// <summary>
/// Sets the person as missing from an event.
/// </summary>
/// <param name="id">Event id.</param>
/// <param name="people">Collection of all people.</param>
/// <param name="emails">Emails of people that missed a given event.</param>
static void MarkAsMissing(Guid id, List<Event> events, List<Person> people, string[] emails)
{
TryGetEvent(id, events, out Event e);
var invited = from i in e.Invited
where emails.Contains(i)
select i;
foreach (var person in people)
foreach (var email in emails)
{
if (person.Email == email && invited.Contains(person.Email))
{
person.Missed(id);
}
}
foreach (var person in people)
{
if (emails.Contains(person.Email) && !invited.Contains(person.Email))
{
Console.WriteLine($"\t{person.Email} is not on the list of invited people!");
}
}
}
/// <summary>
/// Removes an arbitrary number of invited people from a given event.
/// </summary>
/// <param name="id">Event id.</param>
/// <param name="events">Collection of events.</param>
/// <param name="people">Collection of all people.</param>
/// <param name="emails">Invited people to remove from given event.</param>
static void RemoveInvited(Guid id, List<Event> events, List<Person> people, string[] emails)
{
TryGetEvent(id, events, out Event e);
var toRemove = from i in e.Invited
where emails.Contains(i)
select i;
foreach (var i in toRemove)
{
e.RemoveInvited(i);
}
foreach (var person in people)
{
if (emails.Contains(person.Email) && !toRemove.Contains(person.Email))
{
Console.WriteLine($"\t{person.Email} is not on the list of invited people!");
}
}
}
/// <summary>
/// Wrapper function for DateTime.ParseExact
/// </summary>
/// <returns></returns>
static DateTime InputDateTime()
{
try
{
DateTime parsed;
Console.Write("Input date and time in dd/MM/yyyy HH:m format: ");
string toParse = Console.ReadLine();
parsed = DateTime.ParseExact(toParse,"d/MM/yyyy HH:m",CultureInfo.InvariantCulture);
parsed.ToUniversalTime();
return parsed;
}
catch (Exception)
{
throw new("Invalid input format!");
}
}
#endregion
static void Main()
{
#region Predefined data
List<Event> events = new()
{
new Event("e794943a-6a10-46a4-8d84-80f906ea9c6c",
"DUMP DESIGN #1",
"FESB",
DateTime.Parse("22 Nov 2022 14:00:00 GMT"),
DateTime.Parse("22 Nov 2022 16:00:00 GMT"),
new List<string>()
{
"eldred.moor2@hotmail.com",
"eunice2017@gmail.com",
"kennedi_kli@gmail.com",
}
),
new Event("44bd33b8-01b9-45b0-9420-a209236fbcec",
"DUMP DEV #1",
"FESB",
DateTime.Parse("22 Nov 2022 17:00:00 GMT"),
DateTime.Parse("22 Nov 2022 19:00:00 GMT"),
new List<string>()
{
"margarett9@yahoo.com",
"mattie_watsi@gmail.com",
"leann1977@yahoo.com",
"kennedi_kli@gmail.com",
"eldred.moor2@hotmail.com",
}
),
new Event("DUMP DEV #2",
"Dom mladih",
DateTime.Parse("26 Nov 2022 17:00:00 GMT"),
DateTime.Parse("26 Nov 2022 19:00:00 GMT"),
new List<string>()
{
"margarett9@yahoo.com",
"mattie_watsi@gmail.com",
"leann1977@yahoo.com",
"kennedi_kli@gmail.com",
"eldred.moor2@hotmail.com",
}
),
new Event("DUMP DESIGN #2",
"Dom mladih",
DateTime.Parse("26 Nov 2022 19:00:00 GMT"),
DateTime.Parse("26 Nov 2022 21:00:00 GMT"),
new List<string>()
{
"eldred.moor2@hotmail.com",
"eunice2017@gmail.com",
"kennedi_kli@gmail.com",
}
),
new Event("DUMP DEV #3",
"Dom mladih",
DateTime.Parse("10 Dec 2022 13:30:00 GMT"),
DateTime.Parse("10 Dec 2022 19:00:00 GMT"),
new List<string>()
{
"margarett9@yahoo.com",
"mattie_watsi@gmail.com",
"leann1977@yahoo.com",
"kennedi_kli@gmail.com",
"eldred.moor2@hotmail.com",
}
),
new Event("DUMP DESIGN #3",
"FESB",
DateTime.Parse("11 Dec 2022 10:30:00 GMT"),
DateTime.Parse("11 Dec 2022 13:00:00 GMT"),
new List<string>()
{
"eldred.moor2@hotmail.com",
"eunice2017@gmail.com",
"kennedi_kli@gmail.com",
}
),
};
List<Person> people = new()
{
new Person("Mary","Leroy","eldred.moor2@hotmail.com",
new()
{
{Guid.Parse("e794943a-6a10-46a4-8d84-80f906ea9c6c"),false },
{Guid.Parse("44bd33b8-01b9-45b0-9420-a209236fbcec"),true },
}),
new Person("Steven","Wick","eunice2017@gmail.com"),
new Person("Mario","Ehrhardt","kennedi_kli@gmail.com",
new()
{
{Guid.Parse("44bd33b8-01b9-45b0-9420-a209236fbcec"),true },
}),
new Person("Charles","Lean","leann1977@yahoo.com"),
new Person("Karen","Moss","moss_k@gmail.com"),
new Person("Kimberly","Woodward","kayleigh1991@gmail.com"),
new Person("Clifton","Cook","cliftoncook@yahoo.com"),
new Person("Gladys","Rodriguez","mattie_watsi@gmail.com"),
new Person("Miguel","Booher","kurt_ko1971@hotmail.com"),
new Person("Terry","Martin","margarett9@yahoo.com")
};
#endregion
while (true)
{
string[] options =
{
"Close the program",
"Currently active events",
"Upcoming events",
"Past events",
"Create event",
};
Console.Clear();
int option = WriteOptions(options);
Console.Clear();
switch (option)
{
case 0:
{
return;
}
case 1:
{
#region Active Events
Console.Clear();
WriteEvents(ActiveEvents(events), people, WriteStyleActive);
string[] activeEventsOptions =
{
"Return to main menu",
"Mark as missing",
};
int submenuOption = WriteOptions(activeEventsOptions);
switch (submenuOption)
{
case 0:
{
break;
}
case 1:
{
Console.Write("Input event id: ");
if (!Guid.TryParse(Console.ReadLine(), out Guid id))
{
WriteWarning("Invalid id format!");
break;
}
if (!TryGetEvent(id, events, out _))
{
WriteWarning("Event with given id does not exist!");
break;
}
Console.WriteLine("Input emails of people to mark as missing," +
" separated with whitespace.");
Console.Write("Input here: ");
string[] emails = (Console.ReadLine().Split(' '));
MarkAsMissing(id, events, people, emails);
WaitForUser();
break;
}
}
}
break;
#endregion
case 2:
{
#region Upcoming Events
Console.Clear();
WriteEvents(UpcomingEvents(events), people, WriteStyleUpcoming);
string[] upcomingEventsOptions =
{
"Return to main menu",
"Delete event",
"Remove invited people",
};
int submenuOption = WriteOptions(upcomingEventsOptions);
switch (submenuOption)
{
case 0:
{
break;
}
case 1:
{
Console.Write("Input the id of the event you want to delete: ");
if (!TryGetEvent(Console.ReadLine(), events, out Event e))
{
WriteWarning("Event with given id does not exist!");
break;
}
var upcomingEvents = UpcomingEvents(events);
if (!upcomingEvents.Contains(e))
{
WriteWarning("Cannot delete a past event!");
break;
}
bool confirm = GetConfirmation($"Are you sure you want to delete: {e.Title}?");
if (confirm)
{
WriteWarning("Deleted!");
events.Remove(e);
break;
}
WriteWarning("Aborted!");
break;
}
case 2:
{
Console.Write("Input event id: ");
if (!Guid.TryParse(Console.ReadLine(), out Guid id))
{
WriteWarning("Invalid id format!");
break;
}
if (!TryGetEvent(id, events, out Event e))
{
WriteWarning("Event with given id does not exist!");
break;
}
var upcomingEvents = UpcomingEvents(events);
if (!upcomingEvents.Contains(e))
{
WriteWarning("Cannot delete participants of a past event!");
break;
}
bool confirm = GetConfirmation($"Are you sure you want to remove people from: {e.Title}?");
if (confirm)
{
Console.WriteLine("Input emails of people to remove as invited from event," +
" separated with whitespace.");
Console.Write("Input here: ");
string[] emails = (Console.ReadLine().Split(' '));
RemoveInvited(e.Id, events, people, emails);
WriteWarning("Deleted!");
break;
}
WaitForUser();
break;
}
}
break;
#endregion
}
case 3:
{
#region Past Events
Console.Clear();
WriteEvents(PastEvents(events), people, WriteStylePast);
WaitForUser();
break;
#endregion
}
case 4:
{
#region Create Event
string title, location;
DateTime start, end;
string[] invite;
Console.Write("Please input the title of the event: ");
title = Console.ReadLine();
if (string.IsNullOrWhiteSpace(title))
{
WriteWarning("Title cannot be empty!");
break;
}
Console.Write("Optionally input event location: ");
location = Console.ReadLine();
try
{
Console.WriteLine("Start:");
start = InputDateTime();
Console.WriteLine("End:");
end = InputDateTime();
if (start < DateTime.UtcNow)
throw new("Event start time must be in the future!");
if (start > end)
throw new("End time must be after start time!");
}
catch (Exception err)
{
WriteWarning(err.Message);
break;
}
Console.WriteLine("Input emails of people to invite," +
" separated with whitespace.");
Console.Write("Input here: ");
string[] emails = (Console.ReadLine().Split(' '));
var invited = from p in people
where emails.Contains(p.Email)
select p;
List<string> available = new();
List<string> unavailable = new();
foreach (var e in events)
{
foreach (var i in invited)
{
// Overlap between the new event and an existing event
if(e.StartDateTime < end && start < e.EndDateTime)
{
if (e.Invited.Contains(i.Email))
{
unavailable.Add(i.Email);
}
else
{
available.Add(i.Email);
}
}
}
}
if (unavailable.Count > 0)
{
Console.WriteLine("Unavailable at the time:");
foreach (var u in unavailable)
{
Console.WriteLine("\t" + u);
}
}
Event newEvent = new(title, location ?? "", start, end, available.ToList());
events.Add(newEvent);
WaitForUser();
break;
#endregion
}
}
}
}
}
}