-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentGradeManager.java
More file actions
70 lines (61 loc) · 1.86 KB
/
StudentGradeManager.java
File metadata and controls
70 lines (61 loc) · 1.86 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
import java.util.*;
class Student {
String name;
ArrayList<Double> grades;
Student(String name) {
this.name = name;
this.grades = new ArrayList<>();
}
void addGrade(double grade) {
grades.add(grade);
}
double average() {
double sum = 0;
for (double g : grades) sum += g;
return grades.size() == 0 ? 0 : sum / grades.size();
}
double highest() {
return Collections.max(grades);
}
double lowest() {
return Collections.min(grades);
}
}
public class StudentGradeManager {
static ArrayList<Student> students = new ArrayList<>();
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("1 Add Student");
System.out.println("2 Add Grade");
System.out.println("3 View Report");
System.out.println("4 Exit");
int choice = sc.nextInt();
sc.nextLine();
if (choice == 1) addStudent();
else if (choice == 2) addGrade();
else if (choice == 3) report();
else break;
}
}
static void addStudent() {
String name = sc.nextLine();
students.add(new Student(name));
}
static void addGrade() {
String name = sc.nextLine();
double grade = sc.nextDouble();
for (Student s : students) {
if (s.name.equalsIgnoreCase(name)) {
s.addGrade(grade);
return;
}
}
}
static void report() {
for (Student s : students) {
if (s.grades.size() == 0) continue;
System.out.println(s.name + " Avg: " + s.average() + " High: " + s.highest() + " Low: " + s.lowest());
}
}
}