import java.io.*; import java.nio.file.*; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; /** * 文件管理类,负责题目的保存和历史题目的管理 */ public class FileManager { private static final String BASE_DIR = "math_papers"; private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"); /** * 保存题目到文件 * @param user 用户信息 * @param problems 题目列表 * @param difficulty 难度级别(当前选择) * @return 保存的文件路径 */ public String savePaperToFile(User user, List problems, String difficulty) throws IOException { // 创建用户文件夹 String userDir = BASE_DIR + File.separator + user.getUsername(); Files.createDirectories(Paths.get(userDir)); // 生成文件名 String timestamp = LocalDateTime.now().format(formatter); String fileName = timestamp + ".txt"; String filePath = userDir + File.separator + fileName; // 写入题目到文件 try (PrintWriter writer = new PrintWriter(new FileWriter(filePath, StandardCharsets.UTF_8))) { writer.println("数学题目卷子"); writer.println("用户: " + user.getUsername()); writer.println("类型: " + difficulty); writer.println("生成时间: " + timestamp.replace("-", "/").replace("-", ":")); writer.println("题目数量: " + problems.size()); writer.println("=" + "=".repeat(50)); writer.println(); for (int i = 0; i < problems.size(); i++) { writer.println((i + 1) + ". " + problems.get(i).toString()); writer.println(); // 每题之间空一行 } } return filePath; } /** * 获取用户历史题目,用于重复检测 * @param username 用户名 * @return 历史题目集合 */ public Set getUserHistoryProblems(String username) { Set historyProblems = new HashSet<>(); String userDir = BASE_DIR + File.separator + username; try { Path userPath = Paths.get(userDir); if (!Files.exists(userPath)) { return historyProblems; } // 遍历用户文件夹中的所有txt文件 Files.walk(userPath) .filter(path -> path.toString().endsWith(".txt")) .forEach(path -> { try { List lines = Files.readAllLines(path, StandardCharsets.UTF_8); for (String line : lines) { // 提取题目表达式(格式:数字. 表达式 = ?) if (line.matches("^\\d+\\. .+ = \\?$")) { String expression = line.substring(line.indexOf(". ") + 2, line.lastIndexOf(" = ?")); historyProblems.add(expression); } } } catch (IOException e) { System.err.println("读取文件失败: " + path); } }); } catch (IOException e) { System.err.println("访问用户目录失败: " + userDir); } return historyProblems; } /** * 检查题目是否重复 * @param username 用户名 * @param problem 要检查的题目 * @return 是否重复 */ public boolean isDuplicateProblem(String username, MathProblem problem) { Set historyProblems = getUserHistoryProblems(username); return historyProblems.contains(problem.getExpression()); } /** * 生成不重复的题目列表 * @param user 用户信息 * @param generator 题目生成器 * @param count 题目数量 * @param difficulty 难度级别 * @return 不重复的题目列表 */ public List generateUniqueProblems(User user, MathProblemGenerator generator, int count, String difficulty) { List problems = new ArrayList<>(); Set historyProblems = getUserHistoryProblems(user.getUsername()); Set currentProblems = new HashSet<>(); int attempts = 0; int maxAttempts = count * 10; // 最多尝试次数,避免无限循环 while (problems.size() < count && attempts < maxAttempts) { MathProblem problem = generator.generateProblem(difficulty); String expression = problem.getExpression(); // 检查是否与历史题目或当前题目重复 if (!historyProblems.contains(expression) && !currentProblems.contains(expression)) { problems.add(problem); currentProblems.add(expression); } attempts++; } if (problems.size() < count) { System.out.println("警告: 只生成了 " + problems.size() + " 道题目,可能存在重复限制。"); } return problems; } }