Repository navigation
Expand file tree
/
Copy pathstudent_management.java
More file actions
227 lines (186 loc) · 6.17 KB
/
Copy pathstudent_management.java
File metadata and controls
227 lines (186 loc) · 6.17 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Student Management System using file handling
import java.io.*;
import java.util.*;
class Student {
int id;
String name;
int age;
String grade;
public Student(int id, String name, int age, String grade) {
this.id = id;
this.name = name;
this.age = age;
this.grade = grade;
}
public String toFileString() {
return id + "," + name + "," + age + "," + grade;
}
public void display() {
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age + ", Grade: " + grade);
}
}
public class Main {
static final String FILE_NAME = "students.txt";
static ArrayList<Student> studentList = new ArrayList<>();
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
loadFile();
int choice;
do {
System.out.println("\n--- Student Management System ---");
System.out.println("1. Add Student");
System.out.println("2. View Students");
System.out.println("3. Update Student");
System.out.println("4. Delete Student");
System.out.println("5. Search Student");
System.out.println("6. Exit");
System.out.print("Enter choice: ");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1 : addStudent(); break;
case 2 : viewStudents(); break;
case 3 : updateStudent(); break;
case 4 : deleteStudent(); break;
case 5 : searchStudent(); break;
case 6 : {
saveToFile();
System.out.println("Exiting... Data saved.");
} break;
default : System.out.println("Invalid choice!");
}
} while (choice != 6);
}
static void loadFile() {
try (BufferedReader br = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
if (data.length != 4) {
System.out.println("Invalid record skipped: " + line);
continue;
}
try {
Student s = new Student(
Integer.parseInt(data[0]),
data[1],
Integer.parseInt(data[2]),
data[3]
);
studentList.add(s);
} catch (NumberFormatException e) {
System.out.println("Invalid number format. Record skipped: " + line);
}
}
} catch (FileNotFoundException e) {
System.out.println("No existing file found. Starting fresh.");
} catch (IOException e) {
System.out.println("Error reading file.");
}
}
// Save
static void saveToFile() {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILE_NAME))) {
for (Student s : studentList) {
bw.write(s.toFileString());
bw.newLine();
}
} catch (IOException e) {
System.out.println("Error saving file.");
}
}
// Add
static void addStudent() {
System.out.print("Enter ID: ");
int id = sc.nextInt();
sc.nextLine();
for (Student s : studentList) {
if (s.id == id) {
System.out.println("Student with this ID already exists!");
return;
}
}
System.out.print("Enter Name: ");
String name = sc.nextLine();
System.out.print("Enter Age: ");
int age = sc.nextInt();
sc.nextLine();
System.out.print("Enter Grade: ");
String grade = sc.nextLine();
studentList.add(new Student(id, name, age, grade));
System.out.println("Student added successfully!");
}
// View
static void viewStudents() {
if (studentList.isEmpty()) {
System.out.println("No records found.");
return;
}
for (Student s : studentList) {
s.display();
}
}
// Update
static void updateStudent() {
System.out.print("Enter ID to update: ");
int id = sc.nextInt();
sc.nextLine();
for (Student s : studentList) {
if (s.id == id) {
System.out.print("Enter new Name: ");
s.name = sc.nextLine();
System.out.print("Enter new Age: ");
s.age = sc.nextInt();
sc.nextLine();
System.out.print("Enter new Grade: ");
s.grade = sc.nextLine();
System.out.println("Student updated successfully!");
return;
}
}
System.out.println("Student not found!");
}
// Delete
static void deleteStudent() {
System.out.print("Enter ID to delete: ");
int id = sc.nextInt();
sc.nextLine();
for (Student s : studentList) {
if (s.id == id) {
studentList.remove(s);
System.out.println("Student deleted successfully!");
return;
}
}
System.out.println("Student not found!");
}
// Search
static void searchStudent() {
System.out.println("Search by: 1. Name 2. Grade");
int option = sc.nextInt();
sc.nextLine();
boolean found = false;
if (option == 1) {
System.out.print("Enter Name: ");
String name = sc.nextLine();
for (Student s : studentList) {
if (s.name.equalsIgnoreCase(name)) {
s.display();
found = true;
}
}
} else if (option == 2) {
System.out.print("Enter Grade: ");
String grade = sc.nextLine();
for (Student s : studentList) {
if (s.grade.equalsIgnoreCase(grade)) {
s.display();
found = true;
}
}
}
if (!found) {
System.out.println("No matching records found.");
}
}
}