-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManagerStudentImpl.java
More file actions
222 lines (206 loc) · 7.79 KB
/
ManagerStudentImpl.java
File metadata and controls
222 lines (206 loc) · 7.79 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
package student_manager;
import student_manager.exception.StudentCacheException;
import student_manager.exception.StudentExistException;
import student_manager.exception.StudentNotFoundException;
import student_manager.exception.StudentValidationException;
import student_manager.util.IOUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
/**
* ClassName: ManagerStudentImpl
* Package: student_manager
* Description:
*
* @Author fly
* @Create 2026/2/19 22:23
* @Version 1.0
*/
public class ManagerStudentImpl implements ManagerStudent {
// 用于存储student数据缓存的集合,将student从文件中预加载到此集合
private static List<Student> studentList;
// 用于记录是否初始化过studentList
private static int readCount = 0;
/**
* 向集合中添加学生数据
* 如果集合不存在,先读取文件中的内容,对集合进行初始化
* 如果集合存在,先判断集合中有没有这个学生
* @param student 要添加的学生
* @return 返回生成ID的学生
* @throws student_manager.exception.StudentExistException
*/
@Override
public Student addOne(Student student) {
if (studentList == null) {
if (readCount == 0) {
studentList = IOUtil.readAll();
readCount++;
} else {
throw new StudentCacheException("studentList is empty after reading date");
}
}
if (!studentList.isEmpty() && studentList.contains(student)) {
throw new StudentExistException("Student: " + student.getName() + " already exists");
}
studentList.add(student);
return student;
}
/**
* 向当前缓存中添加所有学生
* @param students List<Student> (非空,且集合中有效数据大于0,并且小于3000)
* @return BatchInsertResult { successCount, failList, failReason }
* @throws StudentValidationException:整体验证失败 size>3000
*/
@Override
public BatchInsertResult addAll(List<Student> students) {
int count = 0;
List<Student> failList = new ArrayList<>();
if (students == null) {
throw new StudentValidationException("list out of range, range is 1 to 3000, but list is null");
}
if (students.isEmpty() || students.size() >= 3000) {
throw new StudentValidationException("list out of range, range is 1 to 3000, but size is " + students.size());
}
if (studentList == null) {
if (readCount == 0) {
studentList = IOUtil.readAll();
readCount++;
} else {
throw new StudentCacheException("studentList is empty after reading date");
}
}
if (studentList == null || studentList.isEmpty()) {
studentList = students;
return new BatchInsertResult<Student>(students.size(), failList, null);
}
if (students.size() == 1) {
addOne(students.get(0));
return new BatchInsertResult<Student>(1, failList, null);
}
StringBuilder builder = new StringBuilder();
for (Student student : students) {
if (studentList.contains(student)) {
builder.append(student.getName());
builder.append(" ");
builder.append(student.getIdCardNumber());
builder.append(" 用户已存在");
count ++;
failList.add(student);
continue;
}
studentList.add(student);
}
return new BatchInsertResult<Student>(students.size() - count, failList, builder.toString());
}
/**
* 根据用户名和身份证查找用户
* 先找用户名,用户名存在在找身份证
* @param name
* @param idCardNumber
* @return
*/
@Override
public Optional<Student> findById(String name, String idCardNumber) {
if (studentList == null || studentList.isEmpty()) {
// 先尝试从文件读取,初始化集合
studentList = IOUtil.readAll();
}
if (idCardNumber == null || idCardNumber.length() != 6) {
throw new StudentValidationException("idCardNumber is not Illegal");
}
return studentList.stream().filter(student -> student.getName().equals(name) && student.getIdCardNumber().equals(idCardNumber)).findFirst();
}
/**
* 查看所有学生
* - 返回不可变列表
* - 空缓存时返回空列表
* @return List<Student>,可能为空列表,不为null
* @throws StudentCacheException(缓存加载失败)
*/
@Override
public List<Student> findAll() {
if (studentList == null && readCount == 0) {
studentList = IOUtil.readAll();
readCount++;
}
if (studentList == null) {
throw new StudentCacheException("studentList is empty after reading date");
}
return studentList;
}
/**
* ID不可修改,其他字段全量覆盖
* @param student Student(非空)
* @return Student (旧的学生信息)
// * @throws StudentValidationException:参数校验失败
* @throws StudentNotFoundException :学生不存在
*/
@Override
public Student updateStudentById(Student student) {
if (studentList == null && readCount == 0) {
studentList = IOUtil.readAll();
}
if (studentList == null || studentList.isEmpty()) {
throw new StudentCacheException("studentList is empty after reading date");
}
for (Student stu : studentList) {
if (stu.getName().equals(student.getName()) && stu.getIdCardNumber().equals(student.getIdCardNumber())) {
// 找到了学生,将信息覆盖
Student s = null;
try {
s = stu.clone();
HashMap<Discipline, Float> map = new HashMap<>(s.getScores());
s.setScores(map);
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
stu.setAge(student.getAge());
stu.setScores(student.getScores());
System.out.println(s.toString());
return s;
}
}
throw new StudentNotFoundException("Student: " + student.getName() + " does not exist");
}
/**
* 删除指定学号的学生
* 物理删除,数据移至delete_student文件
* @param idCardNum String idCardNum(非空,长度6)
* @return Optional<Student> (被删除的学生,不存在返回empty)
* @throws StudentValidationException(ID校验失败)
*/
@Override
public Optional<Student> deleteById(String idCardNum) {
Student student = null;
if (idCardNum.length() != 6) {
throw new StudentValidationException("idCardNumber is not legal");
}
if (studentList == null && readCount == 0) {
studentList = IOUtil.readAll();
}
if (studentList == null || studentList.isEmpty()) {
return Optional.empty();
}
int index = -1;
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).getIdCardNumber().equals(idCardNum)) {
index = i;
student = studentList.get(i);
break;
}
}
if (index >= 0) {
System.out.println("执行remove");
studentList.remove(index);
}
if (student == null) {
return Optional.empty();
}
return Optional.of(student);
}
@Override
public void writeToFile() {
IOUtil.write(studentList);
}
}