-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson54.cs
More file actions
202 lines (154 loc) · 6.67 KB
/
Lesson54.cs
File metadata and controls
202 lines (154 loc) · 6.67 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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Tired
{
public class Program
{
static void Main(string[] args)
{
UserMenu userMenu = new UserMenu();
userMenu.Run();
}
}
public class UserMenu
{
private const string WordToExit = "выход";
private const string WordToSortByIllness = "фильтровать";
private const string WordToSortAscending = "сортировать по возрастанию";
private const string WordToSortDescending = "сортировать по убыванию";
private const string WordToClearSort = "очистить фильтр";
private List<SickPerson> _sickPeople = new List<SickPerson>();
private IEnumerable<SickPerson> _sortedList = null;
public UserMenu()
{
CreateRandomSickPeople();
_sortedList = _sickPeople.ToArray();
}
public void Run()
{
string word = "";
while (word != WordToExit)
{
PrintList(_sickPeople);
Console.WriteLine("-------------Отфильтрованный список-------------");
PrintList(_sortedList);
PrintHelp();
Console.Write("Ввод:");
word = Console.ReadLine();
switch (word)
{
case WordToExit:
continue;
break;
case WordToSortByIllness:
FilterPeopleListByIllnes();
break;
case WordToSortAscending:
SortAscending();
break;
case WordToSortDescending:
SortDescending();
break;
case WordToClearSort:
_sortedList = _sickPeople.ToArray();
break;
}
Console.Clear();
}
}
private void SortAscending()
{
var sortedListByFullname = _sickPeople.OrderBy(person => person.Fullname);
var sortedListByAge = _sickPeople.OrderBy(person => person.Age);
SortByCustom(sortedListByFullname, sortedListByAge);
}
private void SortDescending()
{
var sortedListByFullname = _sortedList.OrderByDescending(person => person.Fullname);
var sortedListByAge = _sortedList.OrderByDescending(person => person.Age);
SortByCustom(sortedListByFullname, sortedListByAge);
}
private void SortByCustom(IEnumerable<SickPerson> requestToSortByFullname, IEnumerable<SickPerson> requestToSortByAge)
{
const string FullNameField = "ФИО";
const string AgeField = "Возраст";
string word;
Console.Write("Введите поле для сортировки(" + FullNameField + ", " + AgeField + "):");
word = Console.ReadLine();
switch (word)
{
case FullNameField:
_sortedList = requestToSortByFullname;
break;
case AgeField:
_sortedList = requestToSortByAge;
break;
}
}
private void FilterPeopleListByIllnes()
{
string illness;
Console.Write("Наименование болезни:");
illness = Console.ReadLine();
_sortedList = from SickPerson person in _sickPeople
where person.IllnesName.ToUpper().StartsWith(illness.ToUpper())
select person;
}
private void CreateRandomSickPeople()
{
const int maxCount = 50;
for (int i = 0; i < maxCount; i++)
{
_sickPeople.Add(StudyHelper.CreatePersonWithRandomIllnes());
}
}
private void PrintList(IEnumerable<SickPerson> persons)
{
Console.WriteLine("---------------------------------------------------");
foreach (SickPerson prisoner in persons)
{
prisoner.PrintInfo();
}
Console.WriteLine("---------------------------------------------------");
}
private void PrintHelp()
{
Console.WriteLine("\nВведите '" + WordToExit + "', чтобы выйти из программы.");
Console.WriteLine("Введите '" + WordToSortByIllness + "', чтобы фильтровать больных по названию болезни.");
Console.WriteLine("Введите '" + WordToSortAscending + "', чтобы сортировать больных по возрастанию.");
Console.WriteLine("Введите '" + WordToSortDescending + "', чтобы сортировать больных по возрастанию.");
Console.WriteLine("Введите '" + WordToClearSort + "', чтобы очистить фильтр и сортировку.");
}
}
public class SickPerson
{
public string Fullname { get; private set; } = "Безымянный";
public string IllnesName { get; private set; } = "Неидентифицированная болезнь";
public int Age { get; private set; }
public SickPerson(string fullname, string illnesName, int age)
{
Fullname = fullname;
IllnesName = illnesName;
Age = age;
}
public void PrintInfo()
{
Console.WriteLine("Имя:" + Fullname + ", Возраст:" + Age + ", Болезнь:" + IllnesName);
}
}
public static class StudyHelper
{
private static string[] _names = { "Olov", "Bulty", "Raulf", "Gehri", "Shon", "Loden", "Red", "Drama" };
public static readonly string[] Illnesses = { "Акне", "Зуд", "Рак", "Амнезия", "Бронхит", "Корь", "Трахома" };
public static int GetRandomValue(int minValue = 0, int maxValue = 1) => new Random().Next(minValue, maxValue);
public static SickPerson CreatePersonWithRandomIllnes()
{
const int MaxAge = 120;
string fullname = _names[GetRandomValue(0, _names.Length)] + " " + _names[GetRandomValue(0, _names.Length)];
string illnesses = Illnesses[GetRandomValue(0, Illnesses.Length)];
int age = GetRandomValue(1, MaxAge);
return new SickPerson(fullname, illnesses, age);
}
}
}