-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInterfaceExample.cs
309 lines (248 loc) · 10.5 KB
/
InterfaceExample.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
using System;
using System.Collections;
using System.Linq;
using MyLibrary;
namespace LabWork_10
{
class InterfaceExample
{
private static InterfaceExample singleton = null;
private IExecutable[] executables;
public static InterfaceExample GetExample()
{
if (singleton == null)
{
singleton = new InterfaceExample()
{
executables = new IExecutable[0]
};
}
return singleton;
}
public static void Start()
{
InterfaceExample example = InterfaceExample.GetExample();
example.Run();
}
public void Run()
{
int cmd;
do
{
PrintMenu();
cmd = Input.Integer("Команда > ", "Неизвестная команда", 0, 5);
DoOperation(cmd);
if (cmd != 0) Output.PauseAndClear();
else Output.Clear();
} while (cmd != 0);
}
#region Реализация меню
void PrintMenu()
{
PrintArray();
Console.WriteLine("" +
"1. Создать массив типа IExecutable\n" +
"2. Просмотр массива, демонстрация метода PrintInformation\n" +
"3. Сортировка массива с помощью IComparable\n" +
"4. Сортировка и поиск в массиве с помощью ICompare\n" +
"5. Клонировать элемент с помощью IClonable\n" +
"\n" +
"0. Выход");
}
void PrintArray()
{
if (executables.Length == 0)
{
Console.WriteLine("Массив пуст\n");
}
else
{
int i = 1;
foreach (IExecutable elem in executables)
Console.WriteLine($"{i++,2}. {elem.Name}");
Console.WriteLine();
}
}
void DoOperation(int cmd)
{
switch (cmd)
{
case 1: InitArray(); return;
case 2: PrintInformation(); return;
case 3: Sort(); return;
case 4: SortAndFind(); return;
case 5: CloneElement(); return;
case 0: return;
}
}
#endregion
#region Инициализация массива
IExecutable[] InitPlaces()
{
int len = Input.Integer("Введите количество объектов типа Place: ", "Количество мест не может быть отрицательным", 0, Int32.MaxValue);
if (len == 0) return new IExecutable[0];
bool inputManually = Input.Bool("Заполнить данные вручную? да/нет: ", "да");
string name;
IExecutable[] places = new IExecutable[len];
for (int i = 0; i < len; i++)
{
if (inputManually) name = Input.String($"Название {i + 1}-го места: ");
else Program.FillRandom(out name, out _, out _, out _, out _, out _, out _);
places[i] = new Place(name);
}
return places;
}
IExecutable[] InitRegions()
{
int len = Input.Integer("Введите количество объектов типа Region: ", "Количество регионов не может быть отрицательным", 0, Int32.MaxValue);
if (len == 0) return new IExecutable[0];
bool inputManually = Input.Bool("Заполнить данные вручную? да/нет: ", "да");
string region;
int population;
IExecutable[] regions = new IExecutable[len];
for (int i = 0; i < len; i++)
{
if (inputManually)
{
region = Input.String($"Название {i + 1}-го региона: ");
population = Input.Integer($"Население региона {region}: ");
}
else Program.FillRandom(out _, out region, out _, out _, out population, out _, out _);
regions[i] = new Region(region, population);
}
return regions;
}
IExecutable[] InitCities()
{
int len = Input.Integer("Введите количество объектов типа City: ", "Количество городов не может быть отрицательным", 0, Int32.MaxValue);
if (len == 0) return new IExecutable[0];
bool inputManually = Input.Bool("Заполнить данные вручную? да/нет: ", "да");
string city;
int houses, population;
IExecutable[] cities = new IExecutable[len];
for (int i = 0; i < len; i++)
{
if (inputManually)
{
city = Input.String($"Название {i + 1}-го города: ");
population = Input.Integer($"Население города {city}: ");
houses = Input.Integer($"Количество домов в городе {city}: ");
}
else Program.FillRandom(out _, out _, out city, out _, out population, out houses, out _);
cities[i] = new City(city, population, houses);
}
return cities;
}
IExecutable[] InitAddresses()
{
int len = Input.Integer("Введите количество объектов типа Address: ", "Количество адресов не может быть отрицательным", 0, Int32.MaxValue);
if (len == 0) return new IExecutable[0];
bool inputManually = Input.Bool("Заполнить данные вручную? да/нет: ", "да");
string name, street;
int houseNumber;
IExecutable[] addresses = new IExecutable[len];
for (int i = 0; i < len; i++)
{
if (inputManually)
{
name = Input.String($"Название {i + 1}-го адреса: ");
street = Input.String($"Улица: ");
houseNumber = Input.Integer($"Дом: ");
}
else Program.FillRandom(out name, out _, out _, out street, out _, out _, out houseNumber);
addresses[i] = new Address(name, street, houseNumber);
}
return addresses;
}
void InitArray()
{
executables = new IExecutable[0];
executables = executables.Union(InitPlaces()).ToArray();
executables = executables.Union(InitRegions()).ToArray();
executables = executables.Union(InitCities()).ToArray();
executables = executables.Union(InitAddresses()).ToArray();
}
#endregion
#region Работа с массивом
void PrintInformation()
{
if (executables.Length == 0) Console.WriteLine("\nМассив пуст\n");
else
{
int i = 1;
foreach (IExecutable elem in executables)
{
Console.WriteLine($"-- {i++,2} --");
elem.PrintInformation();
}
}
}
void Sort()
{
if (executables.Length == 0) Console.WriteLine("\nМассив пуст\n");
else
{
SortArrayIComparable();
Console.WriteLine("Массив был отсортирован по алфавиту Я-А");
}
}
void SortArrayIComparable() => Array.Sort(executables);
void SortAndFind()
{
if (executables.Length == 0) Console.WriteLine("\nМассив пуст\n");
else
{
SearchInArray();
SortArrayIComparer();
Console.WriteLine("Массив был отсортирован по алфавиту А-Я");
}
}
void SearchInArray()
{
string name = Input.String("Введите искомое название: ");
int num = 0;
bool isFound = false;
foreach (IExecutable elem in executables)
{
num++;
if (elem.Name == name)
{
isFound = true;
Console.WriteLine($"Найдено совпадение, номер {num}");
}
}
if (!isFound) Console.WriteLine($"Совпадений с названием {name} не найдено");
}
void SortArrayIComparer() => Array.Sort(executables, new SortByName());
#endregion
void CloneElement()
{
if (executables.Length == 0) Console.WriteLine("\nМассив пуст\n");
else
{
Address clonableAdr = new Address("exmp", "Пушкина", 17);
City exmp = new City("Для клонирования", 100, 100, clonableAdr);
City sCopy = (City)exmp.ShallowCopy();
City dCopy = (City)exmp.Clone();
Console.WriteLine("Изначальные адреса: ");
Console.WriteLine("exmp: "); exmp.CopyAddress.PrintInformation();
Console.WriteLine("sCopy: "); sCopy.CopyAddress.PrintInformation();
Console.WriteLine("dCopy: "); dCopy.CopyAddress.PrintInformation();
exmp.CopyAddress.Name = Input.String("Введите новое имя для адреса: ");
Console.WriteLine("\nПосле изменения адреса: ");
Console.WriteLine("exmp: "); exmp.CopyAddress.PrintInformation();
Console.WriteLine("sCopy: "); sCopy.CopyAddress.PrintInformation();
Console.WriteLine("dCopy: "); dCopy.CopyAddress.PrintInformation();
}
}
}
public class SortByName : IComparer
{
public int Compare(object x, object y)
{
IExecutable ie1 = (IExecutable)x;
IExecutable ie2 = (IExecutable)y;
return String.Compare(ie1.Name, ie2.Name);
}
}
}