parent
4ef4cba912
commit
177a9efeae
@ -0,0 +1,94 @@
|
||||
import java.io.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class QuestionManager {
|
||||
private QuestionGenerator generator;
|
||||
private String currentRole;
|
||||
private String username;
|
||||
private static final String QUESTIONS_DIR = "questions";
|
||||
|
||||
public QuestionManager(String username, String role) {
|
||||
this.username = username;
|
||||
setRole(role);
|
||||
createQuestionsDirectory();
|
||||
}
|
||||
|
||||
public void setRole(String role) {
|
||||
this.currentRole = role;
|
||||
switch (role) {
|
||||
case "primary":
|
||||
generator = new PrimaryQuestionGenerator();
|
||||
break;
|
||||
case "junior":
|
||||
generator = new JuniorQuestionGenerator();
|
||||
break;
|
||||
case "senior":
|
||||
generator = new SeniorQuestionGenerator();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 创建questions目录
|
||||
private void createQuestionsDirectory() {
|
||||
File questionsDir = new File(QUESTIONS_DIR);
|
||||
if (!questionsDir.exists()) {
|
||||
questionsDir.mkdir();
|
||||
}
|
||||
|
||||
// 创建用户子目录
|
||||
File userDir = new File(questionsDir, username);
|
||||
if (!userDir.exists()) {
|
||||
userDir.mkdir();
|
||||
}
|
||||
}
|
||||
|
||||
public void generateQuestions(int count) {
|
||||
if (count < 10 || count > 30) {
|
||||
System.out.println("题目数量应在10~30之间");
|
||||
return;
|
||||
}
|
||||
|
||||
// 使用questions目录下的用户文件夹
|
||||
File userDir = new File(QUESTIONS_DIR, username);
|
||||
if (!userDir.exists()) {
|
||||
userDir.mkdir();
|
||||
}
|
||||
|
||||
// 生成文件名
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
|
||||
String fileName = sdf.format(new Date()) + ".txt";
|
||||
File outputFile = new File(userDir, fileName);
|
||||
|
||||
try (PrintWriter writer = new PrintWriter(new FileWriter(outputFile))) {
|
||||
for (int i = 1; i <= count; i++) {
|
||||
String question = generator.generateQuestion();
|
||||
writer.println(i + ". " + question);
|
||||
if (i < count) {
|
||||
writer.println(); // 题目之间空一行
|
||||
}
|
||||
}
|
||||
System.out.println("题目已生成到: " + outputFile.getPath());
|
||||
} catch (IOException e) {
|
||||
System.out.println("生成题目时出错: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public String getCurrentRoleChinese() {
|
||||
switch (currentRole) {
|
||||
case "primary": return "小学";
|
||||
case "junior": return "初中";
|
||||
case "senior": return "高中";
|
||||
default: return "未知";
|
||||
}
|
||||
}
|
||||
|
||||
// 获取指定用户的所有题目文件(用于查重功能)
|
||||
public static File[] getUserQuestionFiles(String username) {
|
||||
File userDir = new File(QUESTIONS_DIR, username);
|
||||
if (userDir.exists() && userDir.isDirectory()) {
|
||||
return userDir.listFiles((dir, name) -> name.endsWith(".txt"));
|
||||
}
|
||||
return new File[0];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue