-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentGradingSystem.java
More file actions
147 lines (125 loc) · 4.91 KB
/
StudentGradingSystem.java
File metadata and controls
147 lines (125 loc) · 4.91 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Grade {
private String subject;
private double score;
public Grade(String subject, double score) {
this.subject = subject;
this.score = score;
}
public String getSubject() {
return subject;
}
public double getScore() {
return score;
}
}
class Student {
private String name;
private int id;
private List<Grade> grades;
private static final double GRADE_A_THRESHOLD = 90.0;
private static final double GRADE_B_THRESHOLD = 80.0;
private static final double GRADE_C_THRESHOLD = 70.0;
public Student(String name, int id) {
this.name = name;
this.id = id;
this.grades = new ArrayList<>();
}
public int getId() {
return id;
}
public void addGrade(String subject, double score) {
grades.add(new Grade(subject, score));
System.out.println("Grade added: " + subject + " - " + score);
}
public double calculateAverage() {
double total = 0;
for (Grade grade : grades) {
total += grade.getScore();
}
return grades.isEmpty() ? 0 : total / grades.size();
}
public String determineOverallGrade() {
double average = calculateAverage();
if (average >= GRADE_A_THRESHOLD) {
return "A";
} else if (average >= GRADE_B_THRESHOLD) {
return "B";
} else if (average >= GRADE_C_THRESHOLD) {
return "C";
} else {
return "D";
}
}
public void displayGradesAndPerformance() {
System.out.println("\nStudent: " + name + " (ID: " + id + ")");
System.out.println("Grades:");
for (Grade grade : grades) {
System.out.println("Subject: " + grade.getSubject() + ", Score: " + grade.getScore());
}
System.out.println("Average Grade: " + calculateAverage());
System.out.println("Overall Grade: " + determineOverallGrade());
}
}
public class StudentGradingSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Student> students = new ArrayList<>();
try {
while (true) {
System.out.println("\n1. Add Student\n2. Add Grade\n3. Display Student Performance\n4. Exit");
System.out.print("Choose an option: ");
int option = Integer.parseInt(scanner.nextLine());
if (option == 1) {
System.out.print("Enter student name: ");
String name = scanner.nextLine();
System.out.print("Enter student ID: ");
int id = Integer.parseInt(scanner.nextLine());
students.add(new Student(name, id));
System.out.println("Student added: " + name);
} else if (option == 2) {
System.out.print("Enter student ID to add grade: ");
int id = Integer.parseInt(scanner.nextLine());
Student student = findStudentById(students, id);
if (student != null) {
System.out.print("Enter subject name: ");
String subject = scanner.nextLine();
System.out.print("Enter grade score: ");
double score = Double.parseDouble(scanner.nextLine());
student.addGrade(subject, score);
} else {
System.out.println("Student ID not found.");
}
} else if (option == 3) {
System.out.print("Enter student ID to display performance: ");
int id = Integer.parseInt(scanner.nextLine());
Student student = findStudentById(students, id);
if (student != null) {
student.displayGradesAndPerformance();
} else {
System.out.println("Student ID not found.");
}
} else if (option == 4) {
System.out.println("Exiting the system. Thank you!");
break;
} else {
System.out.println("Invalid option. Please try again.");
}
}
} catch (NumberFormatException e) {
System.out.println("Invalid input! Please enter the correct data type.");
} finally {
scanner.close();
}
}
private static Student findStudentById(List<Student> students, int id) {
for (Student student : students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
}