forked from CodeGraphContext/CodeGraphContext
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstudentMS.java
More file actions
136 lines (109 loc) · 4.32 KB
/
Copy pathstudentMS.java
File metadata and controls
136 lines (109 loc) · 4.32 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
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;
// Data class representing a student
class Student {
private String id;
private String name;
private List<Integer> grades;
public Student(String id, String name, List<Integer> grades) {
this.id = id;
this.name = name;
this.grades = grades;
}
public String getId() { return id; }
public String getName() { return name; }
public List<Integer> getGrades() { return grades; }
public double getAverageGrade() {
return grades.stream()
.mapToInt(Integer::intValue)
.average()
.orElse(0.0);
}
@Override
public String toString() {
return String.format("%s (%s): Avg Grade = %.2f", name, id, getAverageGrade());
}
}
// Custom exception for invalid student data
class InvalidStudentDataException extends Exception {
public InvalidStudentDataException(String message) {
super(message);
}
}
// Generic repository to manage any type of entity
class Repository<T> {
private List<T> items = new ArrayList<>();
public void add(T item) { items.add(item); }
public List<T> getAll() { return Collections.unmodifiableList(items); }
public void forEach(Consumer<T> action) {
for (T item : items) action.accept(item);
}
public List<T> filter(Predicate<T> predicate) {
return items.stream().filter(predicate).collect(Collectors.toList());
}
}
// Main program
public class StudentManagementSystem {
// Reads students from a text file
private static List<Student> readStudents(String filePath) throws IOException, InvalidStudentDataException {
List<String> lines = Files.readAllLines(Paths.get(filePath));
List<Student> students = new ArrayList<>();
for (String line : lines) {
String[] parts = line.split(",");
if (parts.length < 3) {
throw new InvalidStudentDataException("Invalid data: " + line);
}
String id = parts[0].trim();
String name = parts[1].trim();
List<Integer> grades = new ArrayList<>();
for (int i = 2; i < parts.length; i++) {
try {
grades.add(Integer.parseInt(parts[i].trim()));
} catch (NumberFormatException e) {
throw new InvalidStudentDataException("Invalid grade for " + name + ": " + parts[i]);
}
}
students.add(new Student(id, name, grades));
}
return students;
}
// Display statistics using streams
private static void displayStatistics(List<Student> students) {
System.out.println("\n=== Student Statistics ===");
// Average of all students
double overallAvg = students.stream()
.mapToDouble(Student::getAverageGrade)
.average()
.orElse(0.0);
System.out.printf("Overall Average Grade: %.2f%n", overallAvg);
// Top performer
students.stream()
.max(Comparator.comparingDouble(Student::getAverageGrade))
.ifPresent(top -> System.out.println("Top Performer: " + top));
// Students above average
System.out.println("\nStudents above average:");
students.stream()
.filter(s -> s.getAverageGrade() > overallAvg)
.forEach(System.out::println);
}
public static void main(String[] args) {
System.out.println("=== Student Management System ===");
String filePath = "students.txt"; // Example input file
try {
List<Student> students = readStudents(filePath);
Repository<Student> repo = new Repository<>();
students.forEach(repo::add);
System.out.println("\nAll Students:");
repo.forEach(System.out::println);
displayStatistics(repo.getAll());
} catch (FileNotFoundException e) {
System.err.println("Error: File not found.");
} catch (InvalidStudentDataException e) {
System.err.println("Error: " + e.getMessage());
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
}
}
}