业务服务模块

pull/1/head
smallbailangui 7 months ago
parent 3070afeff5
commit 43dac2f9b7

@ -0,0 +1,63 @@
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.storage.FileManager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
*
*/
public class PaperService {
private final FileManager fileManager;
public PaperService(FileManager fileManager) {
this.fileManager = fileManager;
}
/**
*
* @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("正在生成题目,请稍候...");
while (newPaper.size() < count) {
String question = generator.generateSingleQuestion();
if (!existingQuestions.contains(question) && !generatedInSession.contains(question)) {
newPaper.add(question);
generatedInSession.add(question);
}
}
try {
String filePath = fileManager.savePaper(user.username(), newPaper);
System.out.println("成功!" + count + "道" + currentLevel.getChineseName() + "数学题目已生成。");
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();
};
}
}
Loading…
Cancel
Save