-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManagementSystemApp.java
More file actions
192 lines (174 loc) · 6.24 KB
/
Copy pathStudentManagementSystemApp.java
File metadata and controls
192 lines (174 loc) · 6.24 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import java.io.*;
import java.util.*;
// Class to represent a Student
class Student
{
private String fullName;
private int studentID;
private String grade;
// Constructor to initialize student attributes
public Student(String fullName, int studentID, String grade)
{
this.fullName = fullName;
this.studentID = studentID;
this.grade = grade;
}
// Getter methods for accessing student attributes
public String getFullName()
{
return fullName;
}
public int getStudentID()
{
return studentID;
}
public String getGrade()
{
return grade;
}
// Override toString method to display student information
@Override
public String toString()
{
return "Name: " + fullName + ", Student ID: " + studentID + ", Grade: " + grade;
}
}
// Class to manage the collection of students
class StudentManagementSystem
{
private List<Student> studentList = new ArrayList<>();
private static final String DATA_FILE = "student_data.txt";
// Method to add a student to the system
public void addStudent(Student student)
{
studentList.add(student);
}
// Method to remove a student by student ID
public void removeStudent(int studentID)
{
studentList.removeIf(student -> student.getStudentID() == studentID);
}
// Method to search for a student by student ID
public Student searchStudent(int studentID)
{
for (Student student : studentList)
{
if (student.getStudentID() == studentID)
{
return student;
}
}
return null; // Return null if the student is not found
}
// Method to get a list of all students
public List<Student> getAllStudents()
{
return studentList;
}
// Method to save student data to a file
public void saveDataToFile()
{
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(DATA_FILE)))
{
oos.writeObject(studentList);
System.out.println("Data saved to " + DATA_FILE);
}
catch (IOException e)
{
System.err.println("Saving data to file: " + e.getMessage());
}
}
// Method to load student data from a file
@SuppressWarnings("unchecked")
public void loadDataFromFile()
{
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(DATA_FILE)))
{
studentList = (List<Student>) ois.readObject();
System.out.println("Data loaded from " + DATA_FILE);
}
catch (IOException | ClassNotFoundException e)
{
System.err.println("Error loading data from file: " + e.getMessage());
}
}
}
// Main class for the Student Management Application
public class StudentManagementSystemApp
{
public static void main(String[] args)
{
StudentManagementSystem system = new StudentManagementSystem();
// Load existing student data from file (if any)
system.loadDataFromFile();
Scanner scanner = new Scanner(System.in);
while (true)
{
System.out.println("\nStudent Management System Menu:");
System.out.println("1. Add Student");
System.out.println("2. Remove Student");
System.out.println("3. Search Student");
System.out.println("4. Display All Students");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice)
{
case 1:
System.out.print("Enter student's full name: ");
String fullName = scanner.nextLine();
System.out.print("Enter student's ID: ");
int studentID = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter student's grade: ");
String grade = scanner.nextLine();
Student student = new Student(fullName, studentID, grade);
system.addStudent(student);
break;
case 2:
System.out.print("Enter student ID to remove: ");
int removeStudentID = scanner.nextInt();
scanner.nextLine(); // Consume newline
system.removeStudent(removeStudentID);
break;
case 3:
System.out.print("Enter student ID to search: ");
int searchStudentID = scanner.nextInt();
scanner.nextLine(); // Consume newline
Student foundStudent = system.searchStudent(searchStudentID);
if (foundStudent != null)
{
System.out.println("Student Found:\n" + foundStudent);
}
else
{
System.out.println("Student not found.");
}
break;
case 4:
List<Student> allStudents = system.getAllStudents();
if (!allStudents.isEmpty())
{
System.out.println("All Students:");
for (Student s : allStudents)
{
System.out.println(s);
}
}
else
{
System.out.println("No students found.");
}
break;
case 5:
// Save data to file and exit
system.saveDataToFile();
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}