parent
daeec9438e
commit
d429dcb895
@ -0,0 +1,98 @@
|
||||
package utils;
|
||||
import generator.IQuestionGenerator;
|
||||
import model.Level;
|
||||
import model.User;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class TestPaperService {
|
||||
|
||||
public void generateAndSave(User user, Level level, int count) {
|
||||
Set<String> existingQuestions = loadExistingQuestions(user.getUsername());
|
||||
System.out.println("检测到历史题目 " + existingQuestions.size() + " 道。");
|
||||
|
||||
// IQuestionGenerator generator = createGenerator(level);
|
||||
Set<String> newQuestions = new HashSet<>();
|
||||
|
||||
// 生成新的、与历史和本次不重复的题目
|
||||
for (int i = 0; i < count; i++) {
|
||||
Set<String> used = new HashSet<>(existingQuestions);
|
||||
used.addAll(newQuestions);
|
||||
|
||||
// String question = generator.generate(used);
|
||||
// newQuestions.add(question);
|
||||
}
|
||||
|
||||
saveToFile(user.getUsername(), newQuestions);
|
||||
}
|
||||
|
||||
private Set<String> loadExistingQuestions(String username) {
|
||||
Set<String> existingQuestions = new HashSet<>();
|
||||
File userDir = new File(username);
|
||||
if (!userDir.exists() || !userDir.isDirectory()) {
|
||||
return existingQuestions;
|
||||
}
|
||||
|
||||
File[] files = userDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".txt"));
|
||||
if (files == null) {
|
||||
return existingQuestions;
|
||||
}
|
||||
|
||||
for (File file : files) {
|
||||
try {
|
||||
Files.lines(file.toPath()).forEach(line -> {
|
||||
if (line.matches("^\\d+\\.\\s.*")) {
|
||||
String question = line.substring(line.indexOf(' ') + 1).trim();
|
||||
existingQuestions.add(question);
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
System.err.println("错误:读取历史文件失败: " + file.getName());
|
||||
}
|
||||
}
|
||||
return existingQuestions;
|
||||
}
|
||||
|
||||
private void saveToFile(String username, Set<String> questions) {
|
||||
File userDir = new File(username);
|
||||
if (!userDir.exists()) {
|
||||
userDir.mkdirs();
|
||||
}
|
||||
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
|
||||
String fileName = LocalDateTime.now().format(formatter) + ".txt";
|
||||
Path filePath = userDir.toPath().resolve(fileName);
|
||||
|
||||
try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(filePath))) {
|
||||
int questionNumber = 1;
|
||||
for (String question : questions) {
|
||||
writer.println(questionNumber + ". " + question);
|
||||
writer.println();
|
||||
questionNumber++;
|
||||
}
|
||||
System.out.println("成功生成卷子,已保存至: " + filePath.toAbsolutePath());
|
||||
} catch (IOException e) {
|
||||
System.err.println("错误:保存文件时出错: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// private IQuestionGenerator createGenerator(Level level) {
|
||||
// switch (level) {
|
||||
// case JUNIOR_HIGH:
|
||||
// return new JuniorHighGenerator();
|
||||
// case SENIOR_HIGH:
|
||||
// return new SeniorHighGenerator();
|
||||
// case ELEMENTARY:
|
||||
// default:
|
||||
// return new ElementarySchoolGenerator();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
Loading…
Reference in new issue