-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson28.cs
More file actions
225 lines (176 loc) · 7.52 KB
/
Lesson28.cs
File metadata and controls
225 lines (176 loc) · 7.52 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
using System;
namespace Tired
{
class Program
{
static void Main(string[] args)
{
const string ExitWord = "exit";
const string AddWord = "add";
const string RemoveWord = "remove";
const string ShowAllWord = "show_all";
const string SearchWord = "search";
string word = "";
string[] fullnamesRecords = null;
string[] positionsRecords = null;
Console.WriteLine("Введите "+ AddWord + " для добавления досье");
Console.WriteLine("Введите "+ RemoveWord + " для удаления досье");
Console.WriteLine("Введите "+ ShowAllWord + " для показа всех досье");
Console.WriteLine("Введите "+ SearchWord + " для поиска досье по фамилии");
Console.WriteLine("Введите "+ ExitWord + " для выхода из меню\n\n");
while (word != ExitWord)
{
Console.Write("Ввод:");
word = Console.ReadLine();
switch (word)
{
case AddWord:
AddDossierByUserInput(ref fullnamesRecords,ref positionsRecords);
break;
case RemoveWord:
RemoveDossierByUserInput(ref fullnamesRecords, ref positionsRecords);
break;
case ShowAllWord:
WriteAllDossiers(fullnamesRecords, positionsRecords);
break;
case SearchWord:
SearchDossierByUserInput(fullnamesRecords, positionsRecords);
break;
}
}
}
private static void SearchDossierByUserInput(string[] fullnamesRecords, string[] positionRecords)
{
string surname;
Console.Write("Введите фамилию для поиска досье:");
surname = Console.ReadLine();
SearchDossierBySurname(fullnamesRecords, positionRecords, surname);
}
private static void RemoveDossierByUserInput(ref string[] fullnamesRecords, ref string[] positionRecords)
{
int indexToRemove;
bool isRemovingSuccess;
Console.Write("Введите индекс для удаления записи:");
indexToRemove = Convert.ToInt32(Console.ReadLine());
isRemovingSuccess = RemoveDossierByIndex(ref fullnamesRecords, ref positionRecords, indexToRemove);
if (isRemovingSuccess == true)
{
Console.WriteLine("\nУдаление прошло успешно!");
}
else
{
Console.WriteLine("\nОшибка! Запись не была удалена.");
}
}
private static void AddDossierByUserInput(ref string[] fullnamesRecords, ref string[] positionRecords)
{
string fullnamePerson;
string positionPerson;
Console.Write("Введите ФИО:");
fullnamePerson = Console.ReadLine();
Console.Write("Введите Должность:");
positionPerson = Console.ReadLine();
AddDossier(ref fullnamesRecords, ref positionRecords, fullnamePerson, positionPerson);
Console.WriteLine("Запись добавлена.");
}
private static void SearchDossierBySurname(string[] fullnamesRecords, string[] positionRecords, string surname)
{
if (IsDossiersCorrect(fullnamesRecords, positionRecords, 0) == false)
{
Console.WriteLine("В базе досье присутствуют ошибки! Не могу найти что требуется.");
return;
}
for (int i = 0; i < fullnamesRecords.Length; i++)
{
if( fullnamesRecords[i].Contains(surname) )
{
WriteDossierByIndex(fullnamesRecords,positionRecords,i);
}
}
}
private static void WriteAllDossiers(string[] fullnamesRecords, string[] positionRecords)
{
if (IsDossiersCorrect(fullnamesRecords, positionRecords, 0) == false)
{
Console.WriteLine("В базе досье присутствуют ошибки! Не могу ничего вывести.");
return;
}
for (int i = 0; i < fullnamesRecords.Length; i++)
{
WriteDossierByIndex(fullnamesRecords, positionRecords,i);
}
}
private static void WriteDossierByIndex(string[] fullnamesRecords, string[] positionRecords, int index)
{
if(IsDossiersCorrect(fullnamesRecords,positionRecords, index) == false)
{
return;
}
Console.WriteLine("[" + index + "] " + fullnamesRecords[index] + " - " + positionRecords[index]);
}
private static void AddDossier(ref string[] fullnamesRecords, ref string[] positionRecords, string fullnameRecord, string positionRecord)
{
AddValueToArray(ref fullnamesRecords,fullnameRecord);
AddValueToArray(ref positionRecords,positionRecord);
}
private static bool RemoveDossierByIndex(ref string[] fullnamesRecords,ref string[] positionRecords,int index)
{
return ( DeleteValueFromArrayByIndex(ref fullnamesRecords, index) && DeleteValueFromArrayByIndex(ref positionRecords, index) );
}
private static bool IsDossiersCorrect(string[] fullnamesRecords, string[] positionRecords,int index)
{
if (fullnamesRecords != null && positionRecords != null)
{
if(fullnamesRecords.Length == positionRecords.Length)
{
if(index>=0 && index < fullnamesRecords.Length)
{
return true;
}
}
}
return false;
}
private static bool DeleteValueFromArrayByIndex<T>(ref T[] records, int recordIndex)
{
T[] newArray;
int iterator = 0;
if( records == null || (recordIndex >= 0 && recordIndex < records.Length) == false )
{
return false;
}
newArray = new T[records.Length - 1];
for(int i = 0; i < records.Length; i++)
{
if(recordIndex == i)
{
continue;
}
newArray[iterator] = records[i];
iterator++;
}
records = newArray;
return true;
}
private static void AddValueToArray<T>(ref T[] currentArray, T record)
{
ExtendArray(ref currentArray);
currentArray[currentArray.Length - 1] = record;
}
private static void ExtendArray<T>(ref T[] currentArray)
{
T[] newArray;
if (currentArray == null)
{
currentArray = new T[1];
return;
}
newArray = new T[currentArray.Length + 1];
for (int i = 0; i < currentArray.Length; i++)
{
newArray[i] = currentArray[i];
}
currentArray = newArray;
}
}
}