|
|
|
|
@ -0,0 +1,253 @@
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class Main {
|
|
|
|
|
private static String[][] USERS = {
|
|
|
|
|
{"张三1", "123", "小学"},
|
|
|
|
|
{"张三2", "123", "小学"},
|
|
|
|
|
{"张三3", "123", "小学"},
|
|
|
|
|
{"李四1", "123", "初中"},
|
|
|
|
|
{"李四2", "123", "初中"},
|
|
|
|
|
{"李四3", "123", "初中"},
|
|
|
|
|
{"王五1", "123", "高中"},
|
|
|
|
|
{"王五2", "123", "高中"},
|
|
|
|
|
{"王五3", "123", "高中"}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static Scanner scanner = new Scanner(System.in);
|
|
|
|
|
private static String currentUserName = null;
|
|
|
|
|
private static String currentUserType = null;
|
|
|
|
|
private static QuestionGenerator questionGenerator = null; // 添加题目生成器实例
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
startSystem();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void startSystem() {
|
|
|
|
|
while (true) {
|
|
|
|
|
System.out.println("===中小学数学卷子自动生成程序===");
|
|
|
|
|
System.out.println("1.登录");
|
|
|
|
|
System.out.println("2.退出系统");
|
|
|
|
|
|
|
|
|
|
String input = scanner.nextLine().trim();
|
|
|
|
|
switch (input) {
|
|
|
|
|
case "1":
|
|
|
|
|
if (loginIn()) {
|
|
|
|
|
startQuestionGeneration();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "2":
|
|
|
|
|
System.out.println("感谢使用,再见!");
|
|
|
|
|
scanner.close();
|
|
|
|
|
return;
|
|
|
|
|
default:
|
|
|
|
|
System.out.println("请输入有效指令");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean loginIn() {
|
|
|
|
|
System.out.println("请输入用户名和密码(用空格隔开)");
|
|
|
|
|
while (true) {
|
|
|
|
|
String input = scanner.nextLine().trim();
|
|
|
|
|
String[] parts = input.split(" ");
|
|
|
|
|
|
|
|
|
|
if (parts.length < 2) {
|
|
|
|
|
System.out.println("输入格式错误,请重新输入:");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (String[] USER : USERS) {
|
|
|
|
|
if (parts[0].equals(USER[0]) && parts[1].equals(USER[1])) {
|
|
|
|
|
currentUserName = USER[0];
|
|
|
|
|
currentUserType = USER[2];
|
|
|
|
|
System.out.println("当前选择为" + currentUserType + "出题");
|
|
|
|
|
|
|
|
|
|
// 根据用户类型创建对应的题目生成器实例
|
|
|
|
|
questionGenerator = createQuestionGenerator(currentUserType);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
System.out.println("请输入正确的用户名和密码");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 工厂方法:根据用户类型创建对应的题目生成器
|
|
|
|
|
private static QuestionGenerator createQuestionGenerator(String userType) {
|
|
|
|
|
switch (userType) {
|
|
|
|
|
case "小学":
|
|
|
|
|
return new PrimaryQuestionGenerator();
|
|
|
|
|
case "初中":
|
|
|
|
|
return new JuniorQuestionGenerator();
|
|
|
|
|
case "高中":
|
|
|
|
|
return new SeniorQuestionGenerator();
|
|
|
|
|
default:
|
|
|
|
|
return new PrimaryQuestionGenerator();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void startQuestionGeneration() {
|
|
|
|
|
System.out.println("准备生成 " + currentUserType + " 数学题目,请输入生成题目数量(输入-1将退出当前用户,重新登录):");
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
String input = scanner.nextLine().trim();
|
|
|
|
|
|
|
|
|
|
if (input.startsWith("切换为")) {
|
|
|
|
|
handleSwitchCommand(input);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input.equals("-1")) {
|
|
|
|
|
questionGenerator = null; // 清理生成器实例
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
int quantity = Integer.parseInt(input);
|
|
|
|
|
if (quantity >= 10 && quantity <= 30) {
|
|
|
|
|
executeQuestionGeneration(quantity);
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("错误:题目数量必须在10-30之间,请重新输入:");
|
|
|
|
|
}
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
System.out.println("错误:请输入有效的数字(10-30)或-1返回主菜单");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void handleSwitchCommand(String input) {
|
|
|
|
|
String target = input.substring(3).trim();
|
|
|
|
|
if (target.equals("小学") || target.equals("初中") || target.equals("高中")) {
|
|
|
|
|
currentUserType = target;
|
|
|
|
|
// 切换类型时重新创建题目生成器实例
|
|
|
|
|
questionGenerator = createQuestionGenerator(currentUserType);
|
|
|
|
|
System.out.println("已切换为" + currentUserType + "出题模式");
|
|
|
|
|
System.out.println("准备生成 " + currentUserType + " 数学题目,请输入生成题目数量");
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("错误:请输入小学,初中和高中三个选项中的一个");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void executeQuestionGeneration(int quantity) {
|
|
|
|
|
File userDir = new File(currentUserName);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 1. 创建文件夹
|
|
|
|
|
if (!userDir.exists()) {
|
|
|
|
|
boolean created = userDir.mkdir();
|
|
|
|
|
if (!created) {
|
|
|
|
|
System.out.println("文件夹创建失败");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
System.out.println("创建用户文件夹:" + userDir.getAbsolutePath());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. 加载历史题目用于查重
|
|
|
|
|
Set<String> existingQuestions = loadExistingQuestions(userDir);
|
|
|
|
|
System.out.println("已加载" + existingQuestions.size() + "道历史题目用于查重");
|
|
|
|
|
|
|
|
|
|
// 3. 生成新题目(使用题目生成器实例)
|
|
|
|
|
List<String> newQuestions = generateQuestions(quantity, existingQuestions);
|
|
|
|
|
|
|
|
|
|
// 4. 保存题目
|
|
|
|
|
saveQuestionsToFile(newQuestions, userDir);
|
|
|
|
|
System.out.println("成功生成" + newQuestions.size() + "道题目!");
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.out.println("生成题目过程中发生错误;" + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 修改生成题目的方法,使用题目生成器实例
|
|
|
|
|
private static List<String> generateQuestions(int quantity, Set<String> existingQuestions) {
|
|
|
|
|
List<String> newQuestions = new ArrayList<>();
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
int attempts = 0;
|
|
|
|
|
int maxAttempts = quantity * 10;
|
|
|
|
|
|
|
|
|
|
while (newQuestions.size() < quantity && attempts < maxAttempts) {
|
|
|
|
|
attempts++;
|
|
|
|
|
|
|
|
|
|
// 使用题目生成器实例生成题目
|
|
|
|
|
int operandCount = random.nextInt(5) + 1;
|
|
|
|
|
String question = questionGenerator.generateQuestion(operandCount, random);
|
|
|
|
|
|
|
|
|
|
if (!existingQuestions.contains(question) && !newQuestions.contains(question)) {
|
|
|
|
|
newQuestions.add(question);
|
|
|
|
|
existingQuestions.add(question);
|
|
|
|
|
|
|
|
|
|
if (newQuestions.size() % 5 == 0) {
|
|
|
|
|
System.out.println("已生成 " + newQuestions.size() + " 道题目");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (attempts >= maxAttempts && newQuestions.size() < quantity) {
|
|
|
|
|
System.out.println("警告: 由于重复题目较多,只生成了 " + newQuestions.size() + " 道不重复题目");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newQuestions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 以下文件操作方法保持不变
|
|
|
|
|
private static Set<String> loadExistingQuestions(File userDir) {
|
|
|
|
|
Set<String> questions = new HashSet<>();
|
|
|
|
|
|
|
|
|
|
if (!userDir.exists() || !userDir.isDirectory()) {
|
|
|
|
|
return questions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File[] txtFiles = userDir.listFiles(new FilenameFilter() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean accept(File dir, String name) {
|
|
|
|
|
return name.toLowerCase().endsWith(".txt");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (txtFiles == null) {
|
|
|
|
|
return questions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (File file : txtFiles) {
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
if (line.contains(".") && !line.trim().isEmpty()) {
|
|
|
|
|
String question = line.substring(line.indexOf(".") + 1).trim();
|
|
|
|
|
if (!question.isEmpty()) {
|
|
|
|
|
questions.add(question);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.out.println("读取文件失败: " + file.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return questions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void saveQuestionsToFile(List<String> questions, File userDir) throws IOException {
|
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
|
|
|
|
|
String timestamp = dateFormat.format(new Date());
|
|
|
|
|
String filename = timestamp + ".txt";
|
|
|
|
|
|
|
|
|
|
File outputFile = new File(userDir, filename);
|
|
|
|
|
|
|
|
|
|
try (PrintWriter writer = new PrintWriter(new FileWriter(outputFile))) {
|
|
|
|
|
for (int i = 0; i < questions.size(); i++) {
|
|
|
|
|
writer.print((i + 1) + ". ");
|
|
|
|
|
writer.println(questions.get(i));
|
|
|
|
|
|
|
|
|
|
if (i < questions.size() - 1) {
|
|
|
|
|
writer.println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("题目已保存到: " + outputFile.getAbsolutePath());
|
|
|
|
|
}
|
|
|
|
|
}
|