|
|
import java.io.*;
|
|
|
import java.nio.file.*;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 题目管理器
|
|
|
* 负责题目重复检查和文件保存
|
|
|
*/
|
|
|
public class QuestionManager {
|
|
|
private Map<String, Set<String>> userQuestions; // 存储每个用户的历史题目
|
|
|
|
|
|
public QuestionManager() {
|
|
|
this.userQuestions = new HashMap<>();
|
|
|
loadExistingQuestions();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加载已存在的题目文件,用于重复检查
|
|
|
*/
|
|
|
private void loadExistingQuestions() {
|
|
|
try {
|
|
|
// 为每个用户类型创建目录
|
|
|
createDirectoryIfNotExists("小学");
|
|
|
createDirectoryIfNotExists("初中");
|
|
|
createDirectoryIfNotExists("高中");
|
|
|
|
|
|
// 扫描已存在的文件并加载题目
|
|
|
scanExistingFiles("小学");
|
|
|
scanExistingFiles("初中");
|
|
|
scanExistingFiles("高中");
|
|
|
} catch (Exception e) {
|
|
|
System.err.println("加载历史题目时出错: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建目录
|
|
|
*/
|
|
|
private void createDirectoryIfNotExists(String accountType) throws IOException {
|
|
|
Path path = Paths.get(accountType);
|
|
|
if (!Files.exists(path)) {
|
|
|
Files.createDirectories(path);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 扫描已存在的文件
|
|
|
*/
|
|
|
private void scanExistingFiles(String accountType) {
|
|
|
try {
|
|
|
Path accountDir = Paths.get(accountType);
|
|
|
if (Files.exists(accountDir)) {
|
|
|
Files.walk(accountDir)
|
|
|
.filter(Files::isRegularFile)
|
|
|
.filter(path -> path.toString().endsWith(".txt"))
|
|
|
.forEach(this::loadQuestionsFromFile);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
System.err.println("扫描文件时出错: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从文件加载题目
|
|
|
*/
|
|
|
private void loadQuestionsFromFile(Path filePath) {
|
|
|
try {
|
|
|
String accountType = filePath.getParent().getFileName().toString();
|
|
|
Set<String> questions = userQuestions.computeIfAbsent(accountType, k -> new HashSet<>());
|
|
|
|
|
|
List<String> lines = Files.readAllLines(filePath);
|
|
|
for (String line : lines) {
|
|
|
line = line.trim();
|
|
|
if (line.matches("\\d+\\..*")) { // 题号格式:1. 题目内容
|
|
|
String question = line.substring(line.indexOf('.') + 1).trim();
|
|
|
questions.add(question);
|
|
|
}
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
System.err.println("读取文件时出错: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查题目是否重复
|
|
|
* @param accountType 账户类型
|
|
|
* @param newQuestions 新题目列表
|
|
|
* @return 去重后的题目列表
|
|
|
*/
|
|
|
public List<MathQuestion> checkAndRemoveDuplicates(String accountType, List<MathQuestion> newQuestions) {
|
|
|
Set<String> existingQuestions = userQuestions.computeIfAbsent(accountType, k -> new HashSet<>());
|
|
|
List<MathQuestion> uniqueQuestions = new ArrayList<>();
|
|
|
|
|
|
for (MathQuestion question : newQuestions) {
|
|
|
if (!existingQuestions.contains(question.getExpression())) {
|
|
|
uniqueQuestions.add(question);
|
|
|
existingQuestions.add(question.getExpression());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return uniqueQuestions;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 保存题目到文件
|
|
|
* @param accountType 账户类型
|
|
|
* @param questions 题目列表
|
|
|
* @return 保存的文件名
|
|
|
*/
|
|
|
public String saveQuestionsToFile(String accountType, List<MathQuestion> questions) {
|
|
|
try {
|
|
|
// 生成文件名:年-月-日-时-分-秒.txt
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
|
|
|
String fileName = now.format(formatter) + ".txt";
|
|
|
|
|
|
// 创建文件路径
|
|
|
Path accountDir = Paths.get(accountType);
|
|
|
Path filePath = accountDir.resolve(fileName);
|
|
|
|
|
|
// 写入文件
|
|
|
try (BufferedWriter writer = Files.newBufferedWriter(filePath)) {
|
|
|
for (int i = 0; i < questions.size(); i++) {
|
|
|
writer.write((i + 1) + ". " + questions.get(i).getExpression());
|
|
|
writer.newLine();
|
|
|
if (i < questions.size() - 1) {
|
|
|
writer.newLine(); // 题目之间空一行
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return fileName;
|
|
|
} catch (IOException e) {
|
|
|
System.err.println("保存文件时出错: " + e.getMessage());
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成指定数量的不重复题目
|
|
|
* @param accountType 账户类型
|
|
|
* @param requestedCount 请求的题目数量
|
|
|
* @param generator 题目生成器
|
|
|
* @return 实际生成的题目列表
|
|
|
*/
|
|
|
public List<MathQuestion> generateUniqueQuestions(String accountType, int requestedCount, MathQuestionGenerator generator) {
|
|
|
List<MathQuestion> allQuestions = new ArrayList<>();
|
|
|
int maxAttempts = requestedCount * 3; // 最多尝试3倍数量
|
|
|
int attempts = 0;
|
|
|
|
|
|
while (allQuestions.size() < requestedCount && attempts < maxAttempts) {
|
|
|
List<MathQuestion> newQuestions = generator.generateQuestions(accountType, requestedCount - allQuestions.size());
|
|
|
List<MathQuestion> uniqueQuestions = checkAndRemoveDuplicates(accountType, newQuestions);
|
|
|
allQuestions.addAll(uniqueQuestions);
|
|
|
attempts++;
|
|
|
}
|
|
|
|
|
|
return allQuestions;
|
|
|
}
|
|
|
}
|