-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
152 lines (137 loc) · 4.53 KB
/
Program.cs
File metadata and controls
152 lines (137 loc) · 4.53 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
using System;
using System.Text;
namespace Lesson1
{
class Program
{
#region Menu Methods
static void WriteMenuOption(string optionText, bool isChecked)
{
if (isChecked)
{
Console.BackgroundColor = ConsoleColor.Blue;
optionText = "->" + optionText;
Console.WriteLine(optionText);
Console.BackgroundColor = ConsoleColor.Black;
}
else
{
Console.WriteLine(optionText);
}
}
static void PrintMenu(string header, string[] options, int currentOption)
{
Console.Clear();
Console.WriteLine(header);
for (int i = 0; i < options.Length; i++)
{
WriteMenuOption(options[i], currentOption == i);
}
}
static int CreateMenu(string header, string[] options)
{
var currentOption = 0;
PrintMenu(header, options, currentOption);
while (true)
{
var key = Console.ReadKey().Key;
switch (key)
{
case ConsoleKey.DownArrow:
if (currentOption < options.Length)
{
PrintMenu(header, options, ++currentOption);
}
break;
case ConsoleKey.UpArrow:
if (currentOption > 0)
{
PrintMenu(header, options, --currentOption);
}
break;
case ConsoleKey.Enter:
return currentOption;
}
}
}
#endregion
#region Main Methods
static string[] AddWorker(string[] attenders)
{
Console.Clear();
Console.Write("Enter newcomer's name\n->");
var newcomer = Console.ReadLine();
var tempArray = new string[attenders.Length + 1];
for (int i = 0; i < attenders.Length; i++)
{
tempArray[i] = attenders[i];
}
tempArray[tempArray.Length - 1] = newcomer;
return tempArray;
}
static void ShowWorkers(string[] attenders)
{
Console.Clear();
if (attenders.Length == 0)
{
Console.WriteLine("The list is empty");
}
for (int i = 0; i < attenders.Length; i++)
{
Console.WriteLine(i + " " + attenders[i]);
}
Console.WriteLine("\n Press any key to continue");
Console.ReadKey();
}
static string[] RemoveWorker(string[] attenders)
{
if (attenders.Length == 0)
{
Console.Clear();
Console.WriteLine("No workers to delete");
}
var numberOfItemToRemove = CreateMenu("Select worker who has left:", attenders);
var tempArray = new string[attenders.Length - 1];
for (int i = 0; i < numberOfItemToRemove; i++)
{
tempArray[i] = attenders[i];
}
for (int i = numberOfItemToRemove; i < tempArray.Length; i++)
{
tempArray[i] = attenders[i + 1];
}
return tempArray;
}
static bool ConfirmExit()
{
return CreateMenu("Do you really want to exit and lose all data?", new[] {"Yes", "No"}) == 0;
}
#endregion
static void Main()
{
var attenders = new string[0];
while (true)
{
var chosenOption = CreateMenu("Welcome to Attendence, choose option", new[] {"New worker", "Show current workers", "Remove worker", "Exit"});
switch (chosenOption)
{
case 0:
attenders = AddWorker(attenders);
break;
case 1:
ShowWorkers(attenders);
break;
case 2:
attenders = RemoveWorker(attenders);
break;
case 3:
if (ConfirmExit())
{
return;
}
break;
}
}
}
}
}