-
Notifications
You must be signed in to change notification settings - Fork 0
/
Student_Management_System.java
165 lines (145 loc) · 5.93 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
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
class Student {
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;
}
public String getName() {
return name;
}
public int getRollNumber() {
return rollNumber;
}
public String getGrade() {
return grade;
}
@Override
public String toString() {
return "Name: " + name + "\nRoll Number: " + rollNumber + "\nGrade: " + grade + "\n";
}
}
class StudentManagementSystem {
private ArrayList<Student> students;
public StudentManagementSystem() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(int rollNumber) {
students.removeIf(student -> student.getRollNumber() == rollNumber);
}
public Student searchStudent(int rollNumber) {
for (Student student : students) {
if (student.getRollNumber() == rollNumber) {
return student;
}
}
return null;
}
public void displayAllStudents() {
for (Student student : students) {
System.out.println(student);
}
}
public void saveStudentDataToFile(String filename) {
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) {
for (Student student : students) {
writer.println(student.getName() + "," + student.getRollNumber() + "," + student.getGrade());
}
System.out.println("Student data saved to " + filename);
} catch (IOException e) {
System.out.println("Error saving student data to file: " + e.getMessage());
}
}
public void loadStudentDataFromFile(String filename) {
students.clear();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) {
String name = parts[0];
int rollNumber = Integer.parseInt(parts[1]);
String grade = parts[2];
students.add(new Student(name, rollNumber, grade));
}
}
System.out.println("Student data loaded from " + filename);
} catch (IOException e) {
System.out.println("Error loading student data from file: " + e.getMessage());
}
}
}
public class Student_Management_System{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManagementSystem system = new StudentManagementSystem();
while (true) {
System.out.println("Student Management System");
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. Save Student Data");
System.out.println("6. Load Student Data");
System.out.println("7. Exit");
System.out.print("Select an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter student 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();
system.addStudent(new Student(name, rollNumber, grade));
break;
case 2:
System.out.print("Enter roll number of student to remove: ");
int rollNumberToRemove = scanner.nextInt();
system.removeStudent(rollNumberToRemove);
break;
case 3:
System.out.print("Enter roll number of student to search: ");
int rollNumberToSearch = scanner.nextInt();
Student searchedStudent = system.searchStudent(rollNumberToSearch);
if (searchedStudent != null) {
System.out.println("Student found:\n" + searchedStudent);
} else {
System.out.println("Student not found.");
}
break;
case 4:
System.out.println("All Students:");
system.displayAllStudents();
break;
case 5:
System.out.print("Enter a filename to save student data: ");
String saveFilename = scanner.nextLine();
system.saveStudentDataToFile(saveFilename);
break;
case 6:
System.out.print("Enter a filename to load student data: ");
String loadFilename = scanner.nextLine();
system.loadStudentDataFromFile(loadFilename);
break;
case 7:
System.out.println("Exiting Student Management System");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please select a valid option.");
}
}
}
}