Repository navigation
Expand file tree
/
Copy pathStudentService.java
More file actions
203 lines (156 loc) · 7.2 KB
/
Copy pathStudentService.java
File metadata and controls
203 lines (156 loc) · 7.2 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
package Student_Management_System;
import java.io.*;
import java.util.*;
public class StudentService {
private static final String FILE_NAME = "students.dat";
static Scanner sc = new Scanner(System.in);
public static ArrayList<Student> readStudents() {
ArrayList<Student> list = new ArrayList<>();
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
list = (ArrayList<Student>) ois.readObject();
} catch (Exception e) {
// first run
}
return list;
}
// WRITE
public static void writeStudents(ArrayList<Student> list) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
oos.writeObject(list);
} catch (Exception e) {
System.out.println("Error saving file");
}
}
// ADD
public static void addStudent() {
ArrayList<Student> list = readStudents();
int id;
while (true) {
System.out.print("Enter ID: ");
id = sc.nextInt();
boolean exists = false;
for (Student s : list) {
if (s.getId() == id) {
exists = true;
System.out.println("ID already exists!");
break;
}
}
if (!exists)
break;
}
sc.nextLine();
String name;
while (true) {
System.out.print("Enter Name: ");
name = sc.nextLine();
if (!name.trim().isEmpty())
break;
System.out.println("Invalid name!");
}
float marks;
while (true) {
System.out.print("Enter Marks: ");
marks = sc.nextFloat();
if (marks >= 0 && marks <= 100)
break;
System.out.println("Invalid marks!");
}
list.add(new Student(id, name, marks));
writeStudents(list);
System.out.println("✅ Student Added!");
}
// VIEW
public static void viewStudents() {
ArrayList<Student> list = readStudents();
if (list.isEmpty()) {
System.out.println("No records found!");
return;
}
System.out.printf("%-10s %-20s %-10s\n", "ID", "NAME", "MARKS");
for (Student s : list) {
System.out.println(s);
}
System.out.println("--------------------------------------------------");
}
// SEARCH
public static void searchStudent() {
ArrayList<Student> list = readStudents();
System.out.print("Enter ID: ");
int id = sc.nextInt();
for (Student s : list) {
if (s.getId() == id) {
System.out.println("Found: " + s);
return;
}
}
System.out.println("Student not found");
}
// UPDATE (temp logic)
public static void updateStudent() {
ArrayList<Student> list = readStudents();
System.out.print("Enter ID: ");
int id = sc.nextInt();
sc.nextLine();
for (Student s : list) {
if (s.getId() == id) {
System.out.print("New Name: ");
s.setName(sc.nextLine());
System.out.print("New Marks: ");
s.setMarks(sc.nextFloat());
writeStudents(list);
System.out.println("Updated!");
return;
}
}
System.out.println("Not found");
}
// DELETE
public static void deleteStudent() {
ArrayList<Student> list = readStudents();
System.out.print("Enter ID: ");
int id = sc.nextInt();
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
if (s.getId() == id) {
it.remove();
writeStudents(list);
System.out.println("Deleted!");
return;
}
}
System.out.println("Not found");
}
// SORT
public static void sortStudents() {
ArrayList<Student> list = readStudents();
list.sort((a, b) -> Float.compare(b.getMarks(), a.getMarks()));
viewStudents();
}
// TOPPER
public static void showTopper() {
ArrayList<Student> list = readStudents();
if (list.isEmpty())
return;
Student top = Collections.max(list, Comparator.comparing(Student::getMarks));
System.out.println("Topper: " + top);
}
// COUNT
public static void countStudents() {
System.out.println("Total Students: " + readStudents().size());
}
// EXPORT
public static void exportData() {
ArrayList<Student> list = readStudents();
try (BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt"))) {
for (Student s : list) {
bw.write(s.toString());
bw.newLine();
}
System.out.println("Exported!");
} catch (Exception e) {
System.out.println("Export error");
}
}
}