-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
442 lines (352 loc) · 18.3 KB
/
Copy pathProgram.cs
File metadata and controls
442 lines (352 loc) · 18.3 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
namespace SimpleStudentManagementProjectCSharpProject1
{
internal class Program
{
static double[] marks = new double[10];
static int[] ages = new int[10];
static string[] names = new string[10];
static DateTime[] dates = new DateTime[10];
static int StudentCounter = 0;
static int maxStudent = 10;
static void Main(string[] args)
{
int choice = 0; //Declare and initialize choice
while (true)
{
try //handle the exception if the user enter invalid input
{
//Menu System
Console.Clear();
Console.WriteLine("\nSelect a Program:");
Console.WriteLine("1. Add a new student");
Console.WriteLine("2. View all students");
Console.WriteLine("3. Find a student");
Console.WriteLine("4. Calculate the class average");
Console.WriteLine("5. Find the top-performing student");
Console.WriteLine("6. Sort students by marks");
Console.WriteLine("7. Delete a student record ");
Console.WriteLine("8. Exit");
Console.Write("Enter your choice : ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1: AddNewStudent(); break;
case 2: ViewAllStudent(); break;
case 3: FindStudent(); break;
case 4: CalculateAverage(); break;
case 5: FindTopPerformingStudent(); break;
case 6: SortStudents(); break;
case 7: DeleteStudent(); break;
case 8: return;
default: Console.WriteLine("Invalid Choice! Try again."); break;
}
Console.WriteLine("Press any key "); //ask user to press any key to continue
Console.ReadLine(); //read the user input
}
catch (Exception e)//show exception message if the user enter invalid input
{
Console.WriteLine(e.Message);
Console.WriteLine("Invalid Choice! Try again.");
Console.WriteLine("Press any key "); //ask user to press any key to continue
Console.ReadLine(); //read the user inputConsole.ReadLine();
}
}
}
//Add a new student record (Name, Age, Marks)
static void AddNewStudent()
{
char doAgain;
do
{
//ask user for number of student .
Console.WriteLine("\nEnter Number of students to add:");
int numberOfStudent = int.Parse(Console.ReadLine());
// check the total student if less than or equal the maxStudent (10) .
if (numberOfStudent + StudentCounter <= maxStudent)
{
// start looping from student counter until the total of student counter and number of student witch the user entered
for (int i = StudentCounter; i < StudentCounter + numberOfStudent; i++)
{
string stringName = ""; //Declare and initialize stringName
bool found = false; //Declare and initialize found
do
{
//ask for student name
Console.WriteLine("Enter Student Name:"); //ask user for student name
names[i] = Console.ReadLine();
stringName = names[i]; //assign the name to stringName
if (string.IsNullOrWhiteSpace(stringName)) //show message if the name is empty
{
Console.WriteLine("Name cannot be empty. Please try again.");
found = false;
}
else if (string.Format(stringName).Length < 3) //show message if the name less than 3 characters
{
Console.WriteLine("Name should be at least 3 characters long. Please try again.");
found = false;
}
else if (int.TryParse(stringName, out int result)) //show message if the name is number
{
Console.WriteLine("Invalid input! Please enter a string");
}
else //if the name is valid
{
found = true;
names[i] = stringName;//assign the name to names array
break;
}
} while (!found); //keep asking user for name until the name is valid
do //keep ask user for student marks until the user take number in range of (0-100).
{
try //handle the exception if the user enter invalid input
{
//ask for student marks
Console.WriteLine("Enter Student Marks: ");
marks[i] = int.Parse(Console.ReadLine());
if (marks[i] < 0 || marks[i] > 100) //show message if The mark out of range .
{
Console.WriteLine("invalid marks.The mark should be between ( 0 - 100 ).");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
} while (marks[i] < 0 || marks[i] > 100);
do
{
try//handle the exception if the user enter invalid input
{
Console.WriteLine("Enter Student Age : ");
ages[i] = int.Parse(Console.ReadLine());
if (ages[i] < 21) // show message if ages[i] less than 21
{
Console.WriteLine("you cont add age less than 21 .");
}
}
catch (Exception e) //show message if the user enter invalid input
{
Console.WriteLine(e.Message);
}
} while (ages[i] < 21);//keep ask user for student Ages until the user take number less than 21.
//insert date direct in dates array.
dates[i] = DateTime.Now;
// the massege show to user if the all name ,marks and age saved in arrays.
Console.WriteLine("Student Added Successfully .");
}
//Student counter add the number of student that we asked in the first of program
StudentCounter = StudentCounter + numberOfStudent;
}
else if (StudentCounter == maxStudent) //Maximum number of students reached
{
Console.WriteLine("Maximum number of students reached. No more students can be added.");
}
else //Show the remaining space if user enter number greater than the remaining space
{
Console.WriteLine($"Invalid input . the remaining space is {maxStudent - StudentCounter}");
}
//keep ask user for adding more student until the user want to exit the first Requirement(Add new student).
Console.WriteLine("\n Do you want to add more students? (y/n) \n");
doAgain = Console.ReadKey().KeyChar;
} while (doAgain == 'y' || doAgain == 'Y'); //if the user enter y or Y keep asking for adding more student
}
//View all students (names,Ages, marks enrollment date)
static void ViewAllStudent()
{
if (StudentCounter == 0) // if student counter == 0 show massege "No student record found.".
{
Console.WriteLine("No student record found.");
}
else // show the student detailes from all arrays.
{
for (int i = 0; i < StudentCounter; i++) //start looping from 0 until reach all student that are available in array
{
Console.WriteLine("Student Name : " + names[i]);
Console.WriteLine("Student Mark : " + marks[i]);
Console.WriteLine("Student Age : " + ages[i]);
Console.WriteLine("Student Enrllment Date : " + dates[i]);
}
}
}
//Find a student by name
static void FindStudent()
{
bool found; // flag to know if there is a result or not
do // keep asking user for name until found.
{
//ask user for student name .
Console.Write("Enter student name: ");
string searchName = Console.ReadLine();
searchName.ToLower(); // convert the student name in lowercase
found = false; //initialize found
if (StudentCounter == 0) // if student counter == 0 show massege "No student record found.".
{
Console.WriteLine("No student record found.");
found = false;
break;
}
else if (string.IsNullOrWhiteSpace(searchName))//show message if the name is empty
{
Console.WriteLine("Name cannot be empty. Please try again.");
found = false;
}
else if (int.TryParse(searchName, out int result))//show message if the name is number
{
Console.WriteLine("Invalid input! Please enter a string");
}
else //if the name is valid
{
for (int i = 0; i < StudentCounter; i++) //start looping from 0 until reach all student that are available in array
{
string LowerNames = names[i].ToLower();// convert the student name in lowercase ( names array)
if (LowerNames == searchName) //compar between user entered name and the name in array (if are match )
{
//show the students detailes
Console.WriteLine("Student Name : " + names[i]);
Console.WriteLine("Student Mark : " + marks[i]);
Console.WriteLine("Student Age : " + ages[i]);
Console.WriteLine("Student Enrllment Date : " + dates[i]);
found = true;
break;
}
}
}
if (!found) //Show "Not found. Try again." if names are not match
{
Console.WriteLine("Not found. Try again.");
}
} while (!found);
}
//Calculate the class average
static void CalculateAverage()
{
if (StudentCounter == 0) // if student counter == 0 show massege "No student record found.".
{
Console.WriteLine("No student record found.");
}
double sum = 0; //Declare and initialize sum
for (int i = 0; i < StudentCounter; i++) //start looping from 0 until reach all student that are available in array
{
sum = sum + marks[i]; // sum the marks
}
double average = sum / StudentCounter; //Declare and assign value to Average
Console.WriteLine("The class average is: " + Math.Round(average, 2));
}
//Find the top-performing student
static void FindTopPerformingStudent()
{
if (StudentCounter == 0) // if student counter == 0 show massege "No student record found.".
{
Console.WriteLine("No students available.");
}
int topIndex = 0;//Declare and initialize topIndex to store the index of the highest student mark (topIndex =0)
for (int i = 1; i < StudentCounter; i++) //start looping from 1 until reach all student that are available in array
{
// 1 > 0 at the first
if (marks[i] > marks[topIndex]) // compare between the first mark i=1 with marks[topIndex] (topIndex = 0)
{
topIndex = i; // take the large mark index
}
}
//show the top-performing students detailes
Console.WriteLine(names[topIndex]);
Console.WriteLine(marks[topIndex]);
Console.WriteLine(ages[topIndex]);
Console.WriteLine(dates[topIndex]);
}
//Sort students by marks (Descending Order)
static void SortStudents()
{
//Declare new arrays to sort data
string[] sortedName = new string[StudentCounter];
double[] sortedMarks = new double[StudentCounter];
int[] sortedAges = new int[StudentCounter];
DateTime[] sortedDates = new DateTime[StudentCounter];
for (int i = 0; i < StudentCounter; i++)//start looping from 0 until reach all student that are available in array
{
//Copy the value from the orginal array(ex:names[]) to the new array (ex:sortedNames[])
sortedName[i] = names[i];
sortedMarks[i] = marks[i];
sortedAges[i] = ages[i];
sortedDates[i] = dates[i];
}
for (int i = 0; i < StudentCounter; i++) //start looping from 0 until reach all student that are available in array
{
for (int j = i + 1; j < StudentCounter; j++) //start looping from 1 until reach all student that are available in array to compare
{
if (sortedMarks[i] < sortedMarks[j]) //Compare the first index with the second index start from sortedMarks[0] < sortedMarks[1]
{
//swap marks
double mark = sortedMarks[i];
sortedMarks[i] = sortedMarks[j];
sortedMarks[j] = mark;
//swap names
string Name = sortedName[i];
sortedName[i] = sortedName[j];
sortedName[j] = Name;
//swap ages
int Age = sortedAges[i];
sortedAges[i] = sortedAges[j];
sortedAges[j] = Age;
//swap enrollment dates
DateTime Date = sortedDates[i];
sortedDates[i] = sortedDates[j];
sortedDates[j] = Date;
}
}
}
for (int i = 0; i < StudentCounter; i++) //start looping from 0 until reach all student that are available in array to view the sorted arrays
{
Console.WriteLine("Student Name : " + sortedName[i]);
Console.WriteLine("Student Mark : " + sortedMarks[i]);
Console.WriteLine("Student Age : " + sortedAges[i]);
Console.WriteLine("Student Enrllment Date : " + sortedDates[i]);
}
}
//Delete a student record
static void DeleteStudent()
{
int indexToDelete;// declare variable indexToDelete
do // keep asking user for name to deleted until found.
{
Console.Write("Enter student name to delete: ");
string deleteName = Console.ReadLine();
deleteName.ToLower(); //convert the user input to lowercase
indexToDelete = -1; //initialize
if (string.IsNullOrWhiteSpace(deleteName)) //show message if the name is empty
{
Console.WriteLine("Name cannot be empty. Please try again.");
}
else if (int.TryParse(deleteName, out int result)) //show message if the name is number
{
Console.WriteLine("Invalid input! Please enter a string");
}
else
{
for (int i = 0; i < StudentCounter; i++)//start looping from 0 until reach all student that are available in array
{
string LowerNames = names[i].ToLower();//convert the names in names array to lowercase
if (LowerNames == deleteName)
{
indexToDelete = i; //assign index of name in indexToDelete
}
else //(indexToDelete == -1)
{
Console.WriteLine("Student not found.");
}
}
}
} while (indexToDelete == -1);
for (int i = indexToDelete; i < StudentCounter - 1; i++) //start looping from indexToDelete until (reach all student that are available in array -1)
{
// Shift all elements to left
names[i] = names[i + 1];
ages[i] = ages[i + 1];
marks[i] = marks[i + 1];
dates[i] = dates[i + 1];
}
StudentCounter = StudentCounter - 1; //decrese Student Counter by 1
Console.WriteLine("Student deleted successfully.");
}
}
}