-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudent_Management_System.java
177 lines (156 loc) · 6.31 KB
/
Student_Management_System.java
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
import java.io.*;
import java.util.*;
class Student implements Serializable {
private String name;
private int rollNumber;
private String grade;
public Student(String name, int rollNumber, String grade) {
this.name = name;
this.rollNumber = rollNumber;
this.grade = grade;
}
// Getters and setters for the student attributes
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollNumber() {
return rollNumber;
}
public void setRollNumber(int rollNumber) {
this.rollNumber = rollNumber;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Name: " + name + "\nRoll Number: " + rollNumber + "\nGrade: " + grade + "\n";
}
}
class StudentManagementSystem {
private List<Student> students;
public StudentManagementSystem() {
this.students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(int rollNumber) {
students.removeIf(student -> student.getRollNumber() == rollNumber);
}
public List<Student> searchStudent(String query) {
List<Student> result = new ArrayList<>();
for (Student student : students) {
if (student.getName().toLowerCase().contains(query.toLowerCase())) {
result.add(student);
}
}
return result;
}
public List<Student> getAllStudents() {
return students;
}
public void saveToFile(String fileName) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName))) {
oos.writeObject(students);
System.out.println("Students saved to " + fileName);
} catch (IOException e) {
System.err.println("Error saving students: " + e.getMessage());
}
}
public void loadFromFile(String fileName) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName))) {
students = (List<Student>) ois.readObject();
System.out.println("Students loaded from " + fileName);
} catch (IOException | ClassNotFoundException e) {
System.err.println("Error loading students: " + e.getMessage());
}
}
}
public class Student_Management_System {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManagementSystem studentManagementSystem = new StudentManagementSystem();
while (true) {
System.out.println("Student Management System Menu:");
System.out.println("1. Add a Student");
System.out.println("2. Remove a Student");
System.out.println("3. Search for a Student");
System.out.println("4. Display All Students");
System.out.println("5. Save Students to File");
System.out.println("6. Load Students from File");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter Name: ");
String name = scanner.nextLine();
System.out.print("Enter Roll Number: ");
int rollNumber = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter Grade: ");
String grade = scanner.nextLine();
Student student = new Student(name, rollNumber, grade);
studentManagementSystem.addStudent(student);
System.out.println("Student added successfully.");
break;
case 2:
System.out.print("Enter Roll Number to Remove: ");
int rollNumberToRemove = scanner.nextInt();
studentManagementSystem.removeStudent(rollNumberToRemove);
System.out.println("Student removed successfully.");
break;
case 3:
System.out.print("Enter Search Query: ");
String searchQuery = scanner.nextLine();
List<Student> searchResults = studentManagementSystem.searchStudent(searchQuery);
if (searchResults.isEmpty()) {
System.out.println("No matching students found.");
} else {
System.out.println("Matching Students:");
for (Student result : searchResults) {
System.out.println(result);
}
}
break;
case 4:
List<Student> allStudents = studentManagementSystem.getAllStudents();
if (allStudents.isEmpty()) {
System.out.println("Student list is empty.");
} else {
System.out.println("All Students:");
for (Student student1 : allStudents) {
System.out.println(student1);
}
}
break;
case 5:
System.out.print("Enter File Name to Save Students: ");
String saveFileName = scanner.nextLine();
studentManagementSystem.saveToFile(saveFileName);
break;
case 6:
System.out.print("Enter File Name to Load Students: ");
String loadFileName = scanner.nextLine();
studentManagementSystem.loadFromFile(loadFileName);
break;
case 7:
System.out.println("Exiting Student Management System.");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
}
}