-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManagementSystem.java
More file actions
80 lines (72 loc) · 2.6 KB
/
Copy pathStudentManagementSystem.java
File metadata and controls
80 lines (72 loc) · 2.6 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
import java.io.*;
import java.util.*;
public class StudentManagementSystem {
private List<Student> students = new ArrayList<>();
private final String filePath = "students.dat"; // File for saving data
// Constructor: Load students from file
public StudentManagementSystem() {
loadFromFile();
}
// Add a new student
public void addStudent(Student student) {
students.add(student);
System.out.println("Student added successfully!");
saveToFile();
}
// Remove a student by roll number
public boolean removeStudent(int rollNumber) {
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
if (student.getRollNumber() == rollNumber) {
iterator.remove();
System.out.println("Student removed successfully!");
saveToFile();
return true;
}
}
System.out.println("Student with roll number " + rollNumber + " not found.");
return false;
}
// Search for a student by roll number
public Student searchStudent(int rollNumber) {
for (Student student : students) {
if (student.getRollNumber() == rollNumber) {
return student;
}
}
System.out.println("Student with roll number " + rollNumber + " not found.");
return null;
}
// Display all students
public void displayAllStudents() {
if (students.isEmpty()) {
System.out.println("No students found.");
} else {
System.out.println("=== Student List ===");
for (Student student : students) {
System.out.println(student);
}
}
}
// Save student list to a file
private void saveToFile() {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
oos.writeObject(students);
} catch (IOException e) {
System.out.println("Error saving student data: " + e.getMessage());
}
}
// Load student list from a file
@SuppressWarnings("unchecked")
private void loadFromFile() {
File file = new File(filePath);
if (file.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
students = (List<Student>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
System.out.println("Error loading student data: " + e.getMessage());
}
}
}
}