/
PresentationLayer.cs
389 lines (382 loc) · 16.2 KB
/
PresentationLayer.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
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
#define DEBUG
#undef DEBUG
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace Cinemas
{
class PresentationLayer //Sorry, but it became an ugly freak... 8|
{
#region Members
static int index = 0;
static int cinemaCounter = 0;
static Cinema activeCinema;
static Auditorium activeAuditorium;
static Projection activeProjection;
static string[] mostViewedProjectionData = new string[2];
static int offset = 40;
static int sleepTime = 1000;
static string appName = AppDomain.CurrentDomain.FriendlyName.ToString().Replace(".exe","");
struct MenuTitle
{
string titleName;
public int Length;
public static implicit operator MenuTitle(string value)
{
return new MenuTitle() { titleName = value.ToUpper(), Length = value.Length };
}
public override string ToString()
{
return titleName;
}
}
struct MenuItem
{
string itemName;
public int Length;
public static implicit operator MenuItem(string value)
{
return new MenuItem() { itemName = value, Length = value.Length };
}
public override string ToString()
{
return itemName;
}
}
delegate void MenuMethod();
static Dictionary<int, MenuMethod> MainMenuMethods = new Dictionary<int, MenuMethod>();
static Dictionary<int, MenuMethod> CinemasMenuMethods = new Dictionary<int, MenuMethod>();
static Dictionary<int, MenuMethod> MostViewedProjectionMenuMethods = new Dictionary<int, MenuMethod>();
static Dictionary<int, MenuMethod> InCinemaMenuMethods = new Dictionary<int, MenuMethod>();
static Dictionary<int, MenuMethod> InAuditoriumMenuMethods = new Dictionary<int, MenuMethod>();
static Dictionary<int, MenuMethod> InProjectionMenuMethods = new Dictionary<int, MenuMethod>();
static Dictionary<int, MenuMethod> ReservationMenuMethods = new Dictionary<int, MenuMethod>();
static Dictionary<string, Dictionary<int, MenuMethod>> DelegateDictionaries = new Dictionary<string, Dictionary<int, MenuMethod>>();
#endregion
public PresentationLayer()
{
#region debug message
#if DEBUG
IO_Handler.LogItsCaller();
#endif
#endregion
Console.OutputEncoding = Encoding.UTF8;
Console.CursorVisible = false;
Console.WindowWidth = 2 * offset;
Console.Title = appName;
MainMenuMethods[0] = NewCinemaMenu;
MainMenuMethods[1] = CinemasMenu;
MainMenuMethods[2] = MostViewedProjectionMenu;
MostViewedProjectionMenuMethods[0] = delegate () { IO_Handler.SaveToFile(mostViewedProjectionData); };//Western technique
DelegateDictionaries["MainMenu"] = MainMenuMethods;
DelegateDictionaries["CinemasMenu"] = CinemasMenuMethods;
DelegateDictionaries["MostViewedProjectionMenu"] = MostViewedProjectionMenuMethods;
DelegateDictionaries["InCinemaMenu"] = InCinemaMenuMethods;
DelegateDictionaries["InAuditoriumMenu"] = InAuditoriumMenuMethods;
DelegateDictionaries["InProjectionMenu"] = InProjectionMenuMethods;
DelegateDictionaries["ReservationMenu"] = ReservationMenuMethods;
InfoMenu();
MainMenu();
}
#region Menus
void MainMenu()
{
#region debug message
#if DEBUG
IO_Handler.LogItsCaller();
#endif
#endregion
MenuTitle mainMenu = "MAIN MENU";
MenuItem newCinema = "-Create New Cinema-";
MenuItem cinemas = "Cinemas Menu";
MenuItem mostProjection = "Most Viewed Projection";
MenuItem[] MainMenuItems = { newCinema, cinemas, mostProjection };
KeepDoingMenu(mainMenu, MainMenuItems);
}
//In Main:
void NewCinemaMenu()
{
MenuTitle newCinemaMenu = "New Cinema Menu";
MenuItem[] newCinemaMenuItems = { };
ShowMenu(newCinemaMenu, newCinemaMenuItems);
string cinemaName = IO_Handler.EnterString("Please, enter the name of the new cinema: ");
if (ObjectContainer.CDB.Contains(ObjectContainer.CDB.Find(i => i.Name == cinemaName.ToUpper())))
{
throw new Exception("This Cinema has already exists!");
}
byte auditoriumCount = IO_Handler.EnterByte("Please, enter the number of auditoriums: ");
new Cinema(cinemaName, auditoriumCount);
CinemasMenuMethods[cinemaCounter++] = InCinemaMenu;
IO_Handler.SuccessMessage($"New Cinema: \"{cinemaName}\" has been created with {auditoriumCount} Auditorium{(auditoriumCount>1?"s":"")} in it.");
Thread.Sleep(sleepTime);
}
void CinemasMenu()
{
#region debug message
#if DEBUG
IO_Handler.LogItsCaller();
#endif
#endregion
MenuTitle cinemasMenu = "Cinemas";
MenuItem[] cinemasMenuItems = ListToMenuItemArray(ObjectContainer.CDB);
KeepDoingMenu(cinemasMenu, cinemasMenuItems);
}
void MostViewedProjectionMenu()
{
MenuTitle mostViewedProjectionMenu = "Most Viewed Projection";
MenuItem[] mostViewedProjectionMenuItems = { "Save to file" };
try
{
if (ObjectContainer.FindProjectionMostViewed().ReservedSeatsCount < 1)
{
throw new Exception("Nothing to show");
}
Projection mostViewed = ObjectContainer.FindProjectionMostViewed();
string[] mostViewedData = {
$"{mostViewed.OwnMovie} ({mostViewed.OwnMovie.MinutesOfLength})",
$"On schedule: \n{mostViewed.OwnerAuditorium} in {mostViewed.OwnerAuditorium.OwnerCinema}"
};
Console.WriteLine(mostViewedData[0]);
Console.WriteLine(mostViewedData[1]);
mostViewedProjectionData = mostViewedData;
KeepDoingMenu(mostViewedProjectionMenu, mostViewedProjectionMenuItems);
}
catch (Exception)
{
Console.WriteLine("\n\n");
Console.WriteLine("Nothing to show yet.".PadLeft(offset + 10));
Thread.Sleep(1500);
}
}
//In Subs:
void InCinemaMenu()
{
#region debug message
#if DEBUG
IO_Handler.LogItsCaller();
Thread.Sleep(500);
#endif
#endregion
MenuTitle inCinemaMenu = "In Cinema: ";
MenuItem[] inCinemaMenuItems = DictToMenuItemArray(ObjectContainer.CDB[index].OwnAuditoriums);
string currentCinema = inCinemaMenu+ObjectContainer.CDB[index].Name;
activeCinema = ObjectContainer.CDB[index];
for (int i = 0; i < activeCinema.OwnAuditoriums.Count; i++)
{
InCinemaMenuMethods[i] = InAuditoriumMenu;
}
KeepDoingMenu(currentCinema, inCinemaMenuItems);
}
void InAuditoriumMenu()
{
#region debug message
#if DEBUG
IO_Handler.LogItsCaller();
Thread.Sleep(500);
#endif
#endregion
MenuTitle inAuditoriumMenu = "In ";
MenuItem[] inAuditoriumMenuItems = { "-Add Nem Projection-", "Projections" };
InAuditoriumMenuMethods[0] = activeAuditorium.AddNewProjection;
InAuditoriumMenuMethods[1] = InProjectionMenu;
string currentAuditorium = inAuditoriumMenu + activeCinema.OwnAuditoriums[(byte)(index+1)].ToString()+$"@{ activeCinema}";
KeepDoingMenu(currentAuditorium, inAuditoriumMenuItems);
}
void InProjectionMenu()
{
MenuTitle inProjectionMenu = $"Projections of {activeAuditorium}@{activeCinema}";
//MenuTitle current = inProjectionMenu+$"{activeAuditorium}@{activeCinema}";
MenuItem[] inProjectionMenuItems = DictToMenuItemArray(activeAuditorium.OwnProjections);
for (int i = 0; i < inProjectionMenuItems.Length; i++)
{
InProjectionMenuMethods[i] = ReservationMenu;
}
KeepDoingMenu(inProjectionMenu, inProjectionMenuItems);
}
void ReservationMenu()
{
MenuTitle inProjectionMenu = $"Reservations for {activeProjection.OwnMovie}({activeProjection.OwnMovie.MinutesOfLength})";
MenuItem[] inProjectionMenuItems = { "-Reserve Seat-", "-Free Seat-", "-Check Seat Availability-" };
ReservationMenuMethods[0] = activeProjection.ReserveSeat;
ReservationMenuMethods[1] = activeProjection.FreeSeat;
ReservationMenuMethods[2] = delegate () { activeProjection.PrintOwnSeatsByAvailability(); Console.WriteLine("\n\nPress any key to return..."); };
KeepDoingMenu(inProjectionMenu, inProjectionMenuItems);
}
#endregion
#region control+helper functions
void InfoMenu()
{
Console.BackgroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("Contorols:");
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("Enter menu: EnterKey \nQuit menu: EscapeKey \nNavigate up: UpArrow \nNavigate down: DownArrow \nFirst Menu Item: PageUp \nLast Menu Item: PageDown \n\n");
Console.ResetColor();
Console.WriteLine("\tPress any key to continue!");
Console.ReadKey(false);
Console.Clear();
}
void KeepDoingMenu(MenuTitle menuTitle, MenuItem[] MenuItems)
{
index = 0;
bool run = true;
while (run)
{
ShowMenu(menuTitle, MenuItems);
run = ControlMenu(MenuItems);
Console.Clear();
}
}
void ShowMenu(MenuTitle menuTitle, MenuItem[] MenuItems)
{
#region debug message
#if DEBUG
IO_Handler.LogItsCaller();
#endif
#endregion
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(menuTitle.ToString().PadLeft(offset + menuTitle.Length / 2));
Console.WriteLine();
Console.ResetColor();
for (int i = 0; i < MenuItems.Length; i++)
{
if (i == index)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(MenuItems[i].ToString().PadLeft(offset + MenuItems[i].Length / 2));
Console.ResetColor();
}
else
{
Console.WriteLine(MenuItems[i].ToString().PadLeft(offset + MenuItems[i].Length / 2));
}
}
}
bool ControlMenu(MenuItem[] MenuItems)
{
#region debug message
#if DEBUG
IO_Handler.LogItsCaller();
#endif
#endregion
bool invalid = true;
while (invalid)
{
ConsoleKey ck = Console.ReadKey(false).Key;
switch (ck)
{
case ConsoleKey.UpArrow:
if (--index == -1) index = MenuItems.Length - 1;
goto case ConsoleKey.Clear;
case ConsoleKey.DownArrow:
if (++index == MenuItems.Length) index = 0;
goto case ConsoleKey.Clear;
case ConsoleKey.PageDown:
index = MenuItems.Length - 1;
goto case ConsoleKey.Clear;
case ConsoleKey.PageUp:
index = 0;
goto case ConsoleKey.Clear;
case ConsoleKey.Enter:
Console.Clear();
try
{
StackFrame sf = new StackFrame(2);
string caller_lvl2 = sf.GetMethod().Name;
if (caller_lvl2.Equals("InCinemaMenu"))
{
try
{
activeAuditorium = activeCinema.OwnAuditoriums[(byte)(index + 1)];
#region DEBUG
#if DEBUG
IO_Handler.SuccessMessage("ACTIVE AUDITORIUM GOT SET NOW!");
Thread.Sleep(800);
#endif
#endregion
}
catch (Exception e)
{
IO_Handler.ErrorMessage($"active auditorium not set:\nLOC-{(new StackFrame(1, true)).GetFileLineNumber()}\n" + e.Message);
Thread.Sleep(sleepTime);
}
}
if (caller_lvl2.Equals("InProjectionMenu"))
{
try
{
activeProjection = activeAuditorium.OwnProjections.Skip(index).First().Value;
}
catch (Exception e)
{
IO_Handler.ErrorMessage($"active projection not set:\nLOC-{(new StackFrame(1, true)).GetFileLineNumber()}\n" + e.Message);
Thread.Sleep(sleepTime);
}
}
GetThisMenuMethods()[index]();
}
catch (Exception e)
{
IO_Handler.ErrorMessage($"Error while opening this menu!\nLOC-{(new StackFrame(1, true)).GetFileLineNumber()}\n" + e.Message);
Thread.Sleep(sleepTime);
}
goto case ConsoleKey.Clear;
case ConsoleKey.Escape:
index = 0;
return false;
case ConsoleKey.Clear:
return true;
default:
invalid = true;
break;
}
}
return true;
}
Dictionary<int, MenuMethod> GetThisMenuMethods()
{
StackFrame frame = new StackFrame(3);
try
{
Dictionary<int, MenuMethod> ResultDict = DelegateDictionaries[frame.GetMethod().Name];
return ResultDict;
}
catch (Exception e)
{
throw e;
}
}
MenuItem[] ListToMenuItemArray<T>(List<T> Collection)
{
#region debug message
#if DEBUG
IO_Handler.LogItsCaller();
#endif
#endregion
MenuItem[] MenuItems = new MenuItem[Collection.Count];
for (int i = 0; i < Collection.Count; i++)
{
MenuItems[i] = Collection[i].ToString();
}
return MenuItems;
}
MenuItem[] DictToMenuItemArray<K, V>(Dictionary<K, V> Collection)
{
#region debug message
#if DEBUG
IO_Handler.LogItsCaller();
#endif
#endregion
List<V> Helper = new List<V>();
foreach (var item in Collection)
{
Helper.Add(item.Value);
}
return ListToMenuItemArray(Helper);
}
#endregion
}
}