-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManagementSystem.java
More file actions
249 lines (207 loc) · 9.17 KB
/
StudentManagementSystem.java
File metadata and controls
249 lines (207 loc) · 9.17 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
import java.util.Scanner;
/*
This code is about simple student management system using java it uses 1D and 2D array to store data , static functions to make code re-usable and modular.
*/
public class StudentManagementSystem{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
// [10] is number of rows and [5] is number of columns in marks array.
int studentCount = 0;
int[] avg = new int[10];
int[][] marks = new int[10][5];
int[] totalMarks = new int[10];
int[] rollNumbers = new int[10];
// Keep track whether to run the while loop for student management system or not.
boolean run = true;
// Below string array can hold total 10 strings(names).
String[] names = new String[10];
String[] grades = new String[10];
// This loop will be executed until user selects 5th option.
while(run){
System.out.println("===== Student Management System =====");
System.out.println("1. Add Student");
System.out.println("2. Display All Students");
System.out.println("3. Search Student");
System.out.println("4. Show Topper");
System.out.println("5. Exit");
System.out.print("\nEnter you choice: ");
if(!sc.hasNextInt()){
System.out.println("Enter a number between 1 to 5.");
sc.next();
continue;
}
int processId = sc.nextInt();
switch(processId){
case 1:
studentCount = addStudents(names , grades , rollNumbers , marks , studentCount , totalMarks , sc);
break;
case 2:
showAllStudents(names , grades , rollNumbers , marks , studentCount , totalMarks);
break;
case 3:
SearchStudent(rollNumbers , names , grades , totalMarks , sc);
break;
case 4:
TopperStudent(totalMarks, names , rollNumbers);
break;
case 5:
run = false;
System.out.println("Exiting... Thank you!");
break;
default:
System.out.println("Enter a number between 1 to 5");
}
}
sc.close();
}
// static function to add students and to make code more modular.
static int addStudents(String[] names,String[] grades,int[] rollNumbers,int[][] marks,int studentCount,int[] totalMarks,Scanner sc){
boolean addMore = true;
// Execute loop if user select 1st option and when user types y or Y to enter another student's marks.
while(addMore){
System.out.print("Enter student name: ");
String name = sc.next();
for(int i = 0 ; i < names.length;i++){
if(null == names[i]){
names[i] = name;
break;
}
}
// Don't exit the loop until the students enter the correct marks for all subjects.
while(true){
System.out.print("Enter roll number: ");
if(!sc.hasNextInt()){
System.out.println("Please enter a number.");
sc.next();
continue;
}
int rollNo = sc.nextInt();
if(0 >= rollNo){
System.out.println("Roll number cannot be less than or equal to 0");
continue;
}
for(int i = 0 ; i < rollNumbers.length ; i++){
if(rollNo == rollNumbers[i]){
System.out.println("Students cannot have duplicate roll numbers.");
rollNo = -1;
break;
}
if(0 == rollNumbers[i]){
rollNumbers[i] = rollNo;
break;
}
}
if(-1 == rollNo){
continue;
}else{
break;
}
}
for(int col = 0; col < marks[studentCount].length ;){
System.out.print("Enter marks of subject " + (col + 1) + ": ");
if(!sc.hasNextInt()){
System.out.println("Enter a number between 0 to 100.");
sc.next();
continue;
}
marks[studentCount][col] = sc.nextInt();
if(marks[studentCount][col] < 0 || marks[studentCount][col] > 100){
System.out.println("Enter valid marks.");
continue;
}
totalMarks[studentCount] += marks[studentCount][col];
col++;
}
float percentage = (float)((totalMarks[studentCount] * 100) / 500);
if(percentage >= 90 && percentage <= 100){
grades[studentCount] = "Grade A+";
}else if(percentage >= 80 && percentage < 90){
grades[studentCount] = "Grade A";
}else if(percentage >= 70 && percentage < 80){
grades[studentCount] = "Grade B";
}else if(percentage >= 60 && percentage < 70){
grades[studentCount] = "Grade C";
}else if(percentage >= 50 && percentage < 60){
grades[studentCount] = "Grade D";
}else {
grades[studentCount] = "Fail!!";
}
System.out.println("Student added successfully!");
System.out.print("\nDo you want to add another student? (y/n): ");
char choice = sc.next().trim().toLowerCase().charAt(0);
switch(choice){
case 'y':
case 'Y':
addMore = true;
++studentCount;
break;
case 'n':
case 'N':
default:
addMore = false;
break;
}
}
return ++studentCount;
}
// static function to show all students
static void showAllStudents(String[] names,String[] grades,int[] rollNumbers,int[][] marks,int studentCount,int[] totalMarks){
if(0 == studentCount){
System.out.println("\nFirst add student's data.\n");
return;
}
// This is used to format string properly so that it's structure stay as it is.
System.out.printf("%-8s | %-15s | %-12s | %-10s | %-8s%n",
"Roll No", "Name", "Total marks", "Average", "Grade");
System.out.println("-------------------------------------------------------------");
for(int i = 0; i < studentCount; i++){
System.out.printf("%-8d | %-15s | %-12d | %-10d | %-8s%n",
rollNumbers[i], names[i], totalMarks[i], totalMarks[i]/5, grades[i]);
}
System.out.println("");
}
static void TopperStudent(int[] totalMarks,String[] names,int[] rollNumbers){
if(null == names[0]){
System.out.println("First add the student's details to see the topper student.");
return;
}
int highestMarks = 0;
int idx = 0;
for(int i = 0 ; i < totalMarks.length ; i++){
if(highestMarks < totalMarks[i]){
highestMarks = totalMarks[i];
idx = i;
}
}
System.out.println("Topper: " + names[idx] + " (Roll no: " + rollNumbers[idx] + ") - Total Marks: " + highestMarks + "\n");
}
// static function to search student using student's id.
static void SearchStudent(int[] rollNumbers,String[] names,String[] grades,int[] totalMarks ,Scanner sc){
while(true){
System.out.print("Enter the Roll number of student: ");
if(!sc.hasNextInt()){
System.out.println("Please enter a number.");
sc.next();
continue;
}
int rollNumber = sc.nextInt();
if(0 >= rollNumber){
System.out.println("Roll number cannot be less than or equal to 0");
continue;
}
for(int i = 0 ; i < rollNumbers.length ; i++){
if(rollNumber == rollNumbers[i]){
System.out.println("\n===== These are the student details of given roll number =====\n");
System.out.println("Roll number: " + rollNumber);
System.out.println("Name: " + names[i]);
System.out.println("Total marks: " + totalMarks[i]);
System.out.println("Average marks: " + (totalMarks[i] / 5));
System.out.println("Grades : " + grades[i] + "\n");
return;
}
}
System.out.println("Student with roll number " + rollNumber + " does not exists.\n");
continue;
}
}
}