|
|
|
|
@ -1,11 +1,8 @@
|
|
|
|
|
package com.mathgenerator.service;
|
|
|
|
|
|
|
|
|
|
import com.mathgenerator.generator.JuniorHighSchoolGenerator;
|
|
|
|
|
import com.mathgenerator.generator.PrimarySchoolGenerator;
|
|
|
|
|
import com.mathgenerator.generator.QuestionGenerator;
|
|
|
|
|
import com.mathgenerator.generator.SeniorHighSchoolGenerator;
|
|
|
|
|
import com.mathgenerator.model.Level;
|
|
|
|
|
import com.mathgenerator.auth.User;
|
|
|
|
|
import com.mathgenerator.model.Level;
|
|
|
|
|
import com.mathgenerator.service.strategy.PaperStrategy; // 导入策略接口
|
|
|
|
|
import com.mathgenerator.storage.FileManager;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
@ -14,30 +11,28 @@ import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 业务逻辑核心,负责协调生成和存储。
|
|
|
|
|
* 它现在依赖一个PaperStrategy来决定题目组合方式。
|
|
|
|
|
*/
|
|
|
|
|
public class PaperService {
|
|
|
|
|
private final FileManager fileManager;
|
|
|
|
|
private final PaperStrategy paperStrategy; // 持有策略接口的引用
|
|
|
|
|
|
|
|
|
|
public PaperService(FileManager fileManager) {
|
|
|
|
|
// 通过构造函数注入具体的策略实现
|
|
|
|
|
public PaperService(FileManager fileManager, PaperStrategy paperStrategy) {
|
|
|
|
|
this.fileManager = fileManager;
|
|
|
|
|
this.paperStrategy = paperStrategy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建并保存一份完整的试卷。
|
|
|
|
|
* @param user 用户对象
|
|
|
|
|
* @param count 题目数量
|
|
|
|
|
* @param currentLevel 当前选择的难度等级
|
|
|
|
|
*/
|
|
|
|
|
public void createAndSavePaper(User user, int count, Level currentLevel) {
|
|
|
|
|
QuestionGenerator generator = createGenerator(currentLevel);
|
|
|
|
|
Set<String> existingQuestions = fileManager.loadExistingQuestions(user.username());
|
|
|
|
|
List<String> newPaper = new ArrayList<>();
|
|
|
|
|
Set<String> generatedInSession = new HashSet<>();
|
|
|
|
|
|
|
|
|
|
System.out.println("正在生成题目,请稍候...");
|
|
|
|
|
System.out.println("正在根据策略生成题目,请稍候...");
|
|
|
|
|
while (newPaper.size() < count) {
|
|
|
|
|
String question = generator.generateSingleQuestion();
|
|
|
|
|
// 将选择生成器的逻辑委托给策略对象
|
|
|
|
|
String question = paperStrategy.selectGenerator(currentLevel).generateSingleQuestion();
|
|
|
|
|
|
|
|
|
|
if (!existingQuestions.contains(question) && !generatedInSession.contains(question)) {
|
|
|
|
|
newPaper.add(question);
|
|
|
|
|
generatedInSession.add(question);
|
|
|
|
|
@ -46,18 +41,10 @@ public class PaperService {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
String filePath = fileManager.savePaper(user.username(), newPaper);
|
|
|
|
|
System.out.println("成功!" + count + "道" + currentLevel.getChineseName() + "数学题目已生成。");
|
|
|
|
|
System.out.println("成功!" + count + "道数学题目已生成。");
|
|
|
|
|
System.out.println("文件已保存至: " + filePath);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.err.println("错误:保存文件失败 - " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private QuestionGenerator createGenerator(Level level) {
|
|
|
|
|
return switch (level) {
|
|
|
|
|
case PRIMARY -> new PrimarySchoolGenerator();
|
|
|
|
|
case JUNIOR_HIGH -> new JuniorHighSchoolGenerator();
|
|
|
|
|
case SENIOR_HIGH -> new SeniorHighSchoolGenerator();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|