-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
247 lines (216 loc) · 9.98 KB
/
Program.cs
File metadata and controls
247 lines (216 loc) · 9.98 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
using System;
using System.Collections.Generic;
namespace StudentGradeManagement
{
class Program
{
// ──────────────────────────────────────────────
// Data stores (shared across all methods)
// ──────────────────────────────────────────────
// Maps student ID → student name
static Dictionary<int, string> students = new Dictionary<int, string>();
// Maps student ID → list of (subject, grade) pairs
static Dictionary<int, List<(string Subject, double Grade)>> grades =
new Dictionary<int, List<(string Subject, double Grade)>>();
// ──────────────────────────────────────────────
// Entry point
// ──────────────────────────────────────────────
static void Main(string[] args)
{
bool running = true;
// while loop — keeps the program alive until the user exits
while (running)
{
ShowMenu();
string choice = Console.ReadLine();
// switch — dispatches user choice to the right action
switch (choice)
{
case "1":
AddStudent();
break;
case "2":
AddGrade();
break;
case "3":
DisplayStudents();
break;
case "4":
CalculateAverage();
break;
case "5":
running = false;
Console.WriteLine("\nGoodbye!");
break;
default:
Console.WriteLine("\n[Error] Invalid option. Please enter a number from 1 to 5.\n");
break;
}
}
}
// ──────────────────────────────────────────────
// Method 1 — ShowMenu
// Prints the main menu to the console.
// ──────────────────────────────────────────────
static void ShowMenu()
{
Console.WriteLine("==========================================");
Console.WriteLine(" STUDENT GRADE MANAGEMENT SYSTEM");
Console.WriteLine("==========================================");
Console.WriteLine("1. Add new student");
Console.WriteLine("2. Add grade to a student");
Console.WriteLine("3. Display all students and grades");
Console.WriteLine("4. Calculate and display a student's average");
Console.WriteLine("5. Exit");
Console.Write("Choose an option: ");
}
// ──────────────────────────────────────────────
// Method 2 — AddStudent
// Registers a new student with a unique ID.
// ──────────────────────────────────────────────
static void AddStudent()
{
Console.Write("\nEnter student ID (integer): ");
string idInput = Console.ReadLine();
// Validate that the input is a valid integer
if (!int.TryParse(idInput, out int id))
{
Console.WriteLine("[Error] Invalid ID. Please enter a whole number.\n");
return;
}
// if-else — check for duplicate ID
if (students.ContainsKey(id))
{
Console.WriteLine($"[Error] A student with ID {id} already exists.\n");
}
else
{
Console.Write("Enter student name: ");
string name = Console.ReadLine();
// Validate non-empty name
if (string.IsNullOrWhiteSpace(name))
{
Console.WriteLine("[Error] Name cannot be empty.\n");
return;
}
students[id] = name;
grades[id] = new List<(string, double)>();
Console.WriteLine($"Student \"{name}\" (ID: {id}) added successfully.\n");
}
}
// ──────────────────────────────────────────────
// Method 3 — AddGrade
// Assigns a grade for a subject to an existing student.
// ──────────────────────────────────────────────
static void AddGrade()
{
Console.Write("\nEnter student ID: ");
string idInput = Console.ReadLine();
if (!int.TryParse(idInput, out int id))
{
Console.WriteLine("[Error] Invalid ID. Please enter a whole number.\n");
return;
}
// Check that the student exists
if (!students.ContainsKey(id))
{
Console.WriteLine($"[Error] No student found with ID {id}.\n");
return;
}
Console.Write("Enter subject name: ");
string subject = Console.ReadLine();
if (string.IsNullOrWhiteSpace(subject))
{
Console.WriteLine("[Error] Subject name cannot be empty.\n");
return;
}
Console.Write("Enter grade (0 - 100): ");
string gradeInput = Console.ReadLine();
if (!double.TryParse(gradeInput, out double grade))
{
Console.WriteLine("[Error] Invalid grade. Please enter a number.\n");
return;
}
// if-else — validate grade range
if (grade < 0 || grade > 100)
{
Console.WriteLine("[Error] Grade must be between 0 and 100.\n");
}
else
{
grades[id].Add((subject, grade));
Console.WriteLine($"Grade {grade} in \"{subject}\" added for {students[id]}.\n");
}
}
// ──────────────────────────────────────────────
// Method 4 — DisplayStudents
// Shows every student with their recorded grades.
// ──────────────────────────────────────────────
static void DisplayStudents()
{
Console.WriteLine();
// Handle empty list
if (students.Count == 0)
{
Console.WriteLine("No students registered yet.\n");
return;
}
// foreach loop — iterate over all students
foreach (KeyValuePair<int, string> student in students)
{
Console.WriteLine("------------------------------------------");
Console.WriteLine($" ID: {student.Key} | Name: {student.Value}");
Console.WriteLine("------------------------------------------");
List<(string Subject, double Grade)> studentGrades = grades[student.Key];
if (studentGrades.Count == 0)
{
Console.WriteLine(" (no grades recorded)");
}
else
{
// for loop — print each grade entry
for (int i = 0; i < studentGrades.Count; i++)
{
Console.WriteLine($" {i + 1}. {studentGrades[i].Subject}: {studentGrades[i].Grade}");
}
}
Console.WriteLine();
}
}
// ──────────────────────────────────────────────
// Method 5 — CalculateAverage
// Computes and displays the average grade for a student.
// ──────────────────────────────────────────────
static void CalculateAverage()
{
Console.Write("\nEnter student ID: ");
string idInput = Console.ReadLine();
if (!int.TryParse(idInput, out int id))
{
Console.WriteLine("[Error] Invalid ID. Please enter a whole number.\n");
return;
}
// if-else — check student exists and has grades
if (!students.ContainsKey(id))
{
Console.WriteLine($"[Error] No student found with ID {id}.\n");
}
else if (grades[id].Count == 0)
{
Console.WriteLine($"{students[id]} has no grades to average.\n");
}
else
{
double sum = 0;
List<(string Subject, double Grade)> studentGrades = grades[id];
// for loop — accumulate the sum of all grades
for (int i = 0; i < studentGrades.Count; i++)
{
sum += studentGrades[i].Grade;
}
double average = sum / studentGrades.Count;
Console.WriteLine($"Average grade for {students[id]}: {average:F2}\n");
}
}
}
}