You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
4.1 KiB
120 lines
4.1 KiB
package com.mathapp.services;
|
|
|
|
import com.mathapp.models.Question;
|
|
import com.mathapp.models.TestPaper;
|
|
import com.mathapp.models.User;
|
|
|
|
import java.io.BufferedWriter;
|
|
import java.io.IOException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.StandardOpenOption;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.List;
|
|
import java.util.stream.Stream;
|
|
|
|
public class DataPersistence {
|
|
private static final Path USER_FILE_PATH = Paths.get("data", "users.txt");
|
|
|
|
// 静态初始化块,确保目录存在
|
|
static {
|
|
try {
|
|
Files.createDirectories(USER_FILE_PATH.getParent());
|
|
if (!Files.exists(USER_FILE_PATH)) {
|
|
Files.createFile(USER_FILE_PATH);
|
|
}
|
|
} catch (IOException e) {
|
|
System.err.println("初始化用户数据文件失败: " + e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static void saveUser(User user) {
|
|
String userData = user.getEmail() + "::" + user.getHashedPassword() + System.lineSeparator();
|
|
try {
|
|
Files.writeString(USER_FILE_PATH, userData, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static User findUserByEmail(String email) {
|
|
try (Stream<String> lines = Files.lines(USER_FILE_PATH, StandardCharsets.UTF_8)) {
|
|
return lines.filter(line -> line.startsWith(email + "::"))
|
|
.map(line -> {
|
|
String[] parts = line.split("::");
|
|
// 构造函数会处理密码哈希
|
|
return new User(parts[0], parts[1], true);
|
|
})
|
|
.findFirst()
|
|
.orElse(null);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static boolean updatePassword(String email, String newPassword) {
|
|
try {
|
|
List<String> lines = Files.readAllLines(USER_FILE_PATH, StandardCharsets.UTF_8);
|
|
for (int i = 0; i < lines.size(); i++) {
|
|
if (lines.get(i).startsWith(email + "::")) {
|
|
User tempUser = new User(email, newPassword); // 用来生成新密码的哈希值
|
|
lines.set(i, email + "::" + tempUser.getHashedPassword());
|
|
Files.write(USER_FILE_PATH, lines, StandardCharsets.UTF_8);
|
|
return true;
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
public static void saveTestPaper(TestPaper testPaper) {
|
|
if (testPaper.username() == null) {
|
|
System.err.println("无法保存试卷:用户名为空。");
|
|
return;
|
|
}
|
|
|
|
String username = testPaper.username().split("@")[0]; // 使用邮箱前缀作为目录名
|
|
|
|
try {
|
|
Path userDir = Paths.get("data", username);
|
|
Files.createDirectories(userDir);
|
|
|
|
String timestamp = testPaper.timestamp().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"));
|
|
Path filePath = userDir.resolve(timestamp + ".txt");
|
|
|
|
try (BufferedWriter writer = Files.newBufferedWriter(filePath, StandardCharsets.UTF_8)) {
|
|
writer.write("用户: " + testPaper.username());
|
|
writer.newLine();
|
|
writer.write("时间: " + timestamp);
|
|
writer.newLine();
|
|
writer.write(String.format("得分: %d / %d", testPaper.score(), testPaper.questions().size()));
|
|
writer.newLine();
|
|
writer.write("====================================");
|
|
writer.newLine();
|
|
|
|
for (int i = 0; i < testPaper.questions().size(); i++) {
|
|
Question q = testPaper.questions().get(i);
|
|
writer.write(String.format("题目 %d: %s", (i + 1), q.problemStatement()));
|
|
writer.newLine();
|
|
writer.write("选项: " + String.join(" | ", q.options()));
|
|
writer.newLine();
|
|
writer.write("你的答案: " + testPaper.userAnswers().get(i));
|
|
writer.newLine();
|
|
writer.write("正确答案: " + q.correctAnswer());
|
|
writer.newLine();
|
|
writer.write("--------------------");
|
|
writer.newLine();
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
} |