试卷组合策略

pull/3/head
smallbailangui 7 months ago
parent 20a441ab24
commit 78d01033ba

@ -2,24 +2,24 @@ package com.mathgenerator;
import com.mathgenerator.auth.Authenticator;
import com.mathgenerator.service.PaperService;
import com.mathgenerator.service.strategy.MixedDifficultyStrategy; // 导入具体策略
import com.mathgenerator.service.strategy.PaperStrategy; // 导入策略接口
import com.mathgenerator.storage.FileManager;
import com.mathgenerator.ui.ConsoleUI; // 导入新的UI类
import com.mathgenerator.ui.ConsoleUI;
/**
*
*
*/
public class Application {
public static void main(String[] args) {
// 1. 创建所有核心服务组件
Authenticator authenticator = new Authenticator();
FileManager fileManager = new FileManager();
PaperService paperService = new PaperService(fileManager);
// 2. 创建UI组件并将服务注入其中
// 1. 创建一个具体的策略实例
PaperStrategy strategy = new MixedDifficultyStrategy();
// 2. 将策略实例注入到 PaperService 中
PaperService paperService = new PaperService(fileManager, strategy);
ConsoleUI consoleUI = new ConsoleUI(authenticator, paperService);
// 3. 运行UI
consoleUI.run();
}
}

@ -14,7 +14,7 @@ public class Authenticator {
private static final Map<String, User> USER_DATABASE = initializeUsers();
/**
*
*
*/
private static Map<String, User> initializeUsers() {
return Stream.of(

@ -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();
};
}
}

@ -1,7 +1,6 @@
package com.mathgenerator.storage;
import java.io.IOException;
//TODO:这里不能导入所有类
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

Loading…
Cancel
Save