forked from panxuc/EESAST-hw2024-CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI.cs
236 lines (228 loc) · 7.55 KB
/
UI.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
namespace StudentGradeManager;
public class UI
{
private readonly StudentManager _sm = new();
private static void Menu()
{
Console.WriteLine("STUDENT GRADE MANAGER");
Console.WriteLine("1. Add student");
Console.WriteLine("2. Remove student");
Console.WriteLine("3. Edit student");
Console.WriteLine("4. Get students by ID");
Console.WriteLine("5. Get students by name");
Console.WriteLine("6. Get top students");
Console.WriteLine("7. Exit");
Console.Write("Enter your choice: ");
}
private void AddStudent()
{
string? name, id;
while (true)
{
Console.Write("Enter student name: ");
name = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(name)) break;
Console.WriteLine("The student name cannot be empty. Please try again.");
}
while (true)
{
Console.Write("Enter student ID: ");
id = Console.ReadLine();
if (int.TryParse(id, out _)) break;
Console.WriteLine("Invalid ID. Please try again.");
}
var student = new Student(name, id);
_sm.AddStudent(student);
while (true)
{
Console.WriteLine("1. Add grade");
Console.WriteLine("2. Finish");
Console.Write("Enter your choice: ");
var choice = Console.ReadLine();
if (choice == "1")
{
string? course, credit, score;
while (true)
{
Console.Write("Enter course name: ");
course = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(course)) break;
Console.WriteLine("The course name cannot be empty. Please try again.");
}
while (true)
{
Console.Write("Enter credit: ");
credit = Console.ReadLine();
if (int.TryParse(credit, out var result) && result >= 0) break;
Console.WriteLine("Invalid credit. Please try again.");
}
while (true)
{
Console.Write("Enter score: ");
score = Console.ReadLine();
if (int.TryParse(score, out var result) && result is >= 0 and <= 100) break;
Console.WriteLine("Invalid score. Please try again.");
}
student.AddGrade(course, credit, score);
}
else if (choice == "2")
{
break;
}
}
}
private void RemoveStudent()
{
string? id;
while (true)
{
Console.Write("Enter student ID: ");
id = Console.ReadLine();
if (int.TryParse(id, out _)) break;
Console.WriteLine("Invalid ID. Please try again.");
}
_sm.RemoveStudent(int.Parse(id));
}
private void EditStudent()
{
string? id;
do
{
Console.Write("Enter student ID: ");
id = Console.ReadLine();
} while (int.TryParse(id, out _) == false);
var student = _sm.GetStudentsByID(int.Parse(id))[0];
while (true)
{
Console.WriteLine("1. Add grade");
Console.WriteLine("2. Remove grade");
Console.WriteLine("3. Finish");
Console.Write("Enter your choice: ");
var choice = Console.ReadLine();
if (choice == "1")
{
string? course, credit, score;
while (true)
{
Console.Write("Enter course name: ");
course = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(course)) break;
Console.WriteLine("The course name cannot be empty. Please try again.");
}
while (true)
{
Console.Write("Enter credit: ");
credit = Console.ReadLine();
if (int.TryParse(credit, out var result) && result >= 0) break;
Console.WriteLine("Invalid credit. Please try again.");
}
while (true)
{
Console.Write("Enter score: ");
score = Console.ReadLine();
if (int.TryParse(score, out var result) && result is >= 0 and <= 100) break;
Console.WriteLine("Invalid score. Please try again.");
}
student.AddGrade(course, credit, score);
}
else if (choice == "2")
{
string? course;
while (true)
{
Console.Write("Enter course name: ");
course = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(course)) break;
Console.WriteLine("The course name cannot be empty. Please try again.");
}
student.RemoveGrade(course);
}
else if (choice == "3")
{
break;
}
}
}
private void GetStudentsByID()
{
string? id;
while (true)
{
Console.Write("Enter student ID: ");
id = Console.ReadLine();
if (int.TryParse(id, out _)) break;
Console.WriteLine("Invalid ID. Please try again.");
}
var students = _sm.GetStudentsByID(int.Parse(id));
foreach (var student in students)
{
Console.WriteLine(student.ToString());
}
}
private void GetStudentsByName()
{
string? name;
while (true)
{
Console.Write("Enter student name: ");
name = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(name)) break;
Console.WriteLine("The student name cannot be empty. Please try again.");
}
var students = _sm.GetStudentsByName(name);
foreach (var student in students)
{
Console.WriteLine(student.ToString());
}
}
private void GetTopStudents()
{
string? count;
while (true)
{
Console.Write("Enter count: ");
count = Console.ReadLine();
if (int.TryParse(count, out var result) && result > 0) break;
Console.WriteLine("Invalid count. Please try again.");
}
var students = _sm.GetTopStudents(int.Parse(count));
foreach (var student in students)
{
Console.WriteLine(student.ToString());
}
}
public void Run()
{
while (true)
{
Menu();
var choice = Console.ReadLine();
switch (choice)
{
case "1":
AddStudent();
break;
case "2":
RemoveStudent();
break;
case "3":
EditStudent();
break;
case "4":
GetStudentsByID();
break;
case "5":
GetStudentsByName();
break;
case "6":
GetTopStudents();
break;
case "7":
return;
default:
Console.WriteLine("Invalid choice");
break;
}
}
}
}