|
|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
package com.student.mathquiz;// src/com.student.mathquiz.Main.java
|
|
|
|
|
package com.student.mathquiz; // src/com/student/mathquiz/Main.java
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
@ -6,21 +6,19 @@ import java.util.Scanner;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*主程序命令行输入面板.
|
|
|
|
|
* 主函数入口.
|
|
|
|
|
*/
|
|
|
|
|
public class Main {
|
|
|
|
|
|
|
|
|
|
private static Scanner scanner = new Scanner(System.in);
|
|
|
|
|
private static String currentUsername = null;
|
|
|
|
|
private static UserType currentUserType = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 主程序.
|
|
|
|
|
|
|
|
|
|
* @param args 程序入口
|
|
|
|
|
* 程序主入口.
|
|
|
|
|
*
|
|
|
|
|
* @param args 命令行参数
|
|
|
|
|
*/
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
System.out.println("--- Main method started. If you see this, the JAR is running! ---");
|
|
|
|
|
System.out.println("--- 中小学数学卷子自动生成程序 ---");
|
|
|
|
|
while (true) {
|
|
|
|
|
login();
|
|
|
|
|
@ -30,6 +28,9 @@ public class Main {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理用户登录逻辑.
|
|
|
|
|
*/
|
|
|
|
|
private static void login() {
|
|
|
|
|
currentUsername = null;
|
|
|
|
|
currentUserType = null;
|
|
|
|
|
@ -42,7 +43,7 @@ public class Main {
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String[] credentials = input.split("\\s+");
|
|
|
|
|
final String[] credentials = input.split("\\s+");
|
|
|
|
|
|
|
|
|
|
if (credentials.length != 2) {
|
|
|
|
|
System.out.println("输入格式不正确,请重新输入。");
|
|
|
|
|
@ -63,6 +64,9 @@ public class Main {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理登录后的用户交互循环.
|
|
|
|
|
*/
|
|
|
|
|
private static void handleLoggedInUser() {
|
|
|
|
|
while (true) {
|
|
|
|
|
System.out.println("\n准备生成 " + currentUserType.name() + " 数学题目,请输入生成题目数量(输入-1将退出当前用户,重新登录):");
|
|
|
|
|
@ -86,11 +90,17 @@ public class Main {
|
|
|
|
|
System.out.println("题目数量的有效输入范围是“10-30”(或-1退出登录),请重新输入。");
|
|
|
|
|
}
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
System.out.println("输入无效,请输入数字或'切换为 XX'命令。");
|
|
|
|
|
System.out.println("错误:请输入一个有效的数字!");
|
|
|
|
|
}
|
|
|
|
|
// 修改
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理切换题目类型的命令.
|
|
|
|
|
*
|
|
|
|
|
* @param input 用户输入的命令
|
|
|
|
|
*/
|
|
|
|
|
private static void handleSwitchType(String input) {
|
|
|
|
|
String typeStr = input.substring("切换为".length()).trim();
|
|
|
|
|
UserType newType = UserType.fromString(typeStr);
|
|
|
|
|
@ -102,19 +112,44 @@ public class Main {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =================================================================
|
|
|
|
|
// ★★★★★★★★★★★★★ 看这里!重构的艺术! ★★★★★★★★★★★★★
|
|
|
|
|
// =================================================================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*调用父类Object构造方法,完成初始化.
|
|
|
|
|
* 生成并保存卷子的总调度方法 (项目经理).
|
|
|
|
|
* 它的职责是调用专家,并处理结果.
|
|
|
|
|
*
|
|
|
|
|
* @param count 要生成的题目数量
|
|
|
|
|
*/
|
|
|
|
|
public Main() {
|
|
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void generateAndSaveQuiz(int count) {
|
|
|
|
|
System.out.println("正在生成 " + count + " 道不重复的 " + currentUserType.name() + " 题目,请稍候...");
|
|
|
|
|
|
|
|
|
|
// 任务1: 派发给“造题专家”去造题
|
|
|
|
|
List<String> newQuestions = createNewUniqueQuestions(count);
|
|
|
|
|
|
|
|
|
|
// 任务2: 检查“专家”的工作成果
|
|
|
|
|
if (newQuestions.size() < count) {
|
|
|
|
|
System.out.println("警告:未能生成足够数量的不重复题目。");
|
|
|
|
|
System.out.println("本次成功生成 " + newQuestions.size() + " 道题目。");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 任务3: 派发给“文件管理员”去保存
|
|
|
|
|
if (!newQuestions.isEmpty()) {
|
|
|
|
|
QuizManager.saveQuizToFile(currentUsername, newQuestions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建不重复的新题目的辅助方法.
|
|
|
|
|
*
|
|
|
|
|
* @param count 要生成的题目数量
|
|
|
|
|
* @return 生成的新题目列表
|
|
|
|
|
*/
|
|
|
|
|
private static List<String> createNewUniqueQuestions(int count) {
|
|
|
|
|
Set<String> existingQuestions = QuizManager.loadExistingQuestions(currentUsername);
|
|
|
|
|
List<String> newQuestions = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
int maxTries = count * 200; // 增加尝试次数,因为题目随机性更强
|
|
|
|
|
int maxTries = count * 200;
|
|
|
|
|
int tries = 0;
|
|
|
|
|
|
|
|
|
|
while (newQuestions.size() < count && tries < maxTries) {
|
|
|
|
|
@ -124,14 +159,6 @@ public class Main {
|
|
|
|
|
}
|
|
|
|
|
tries++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newQuestions.size() < count) {
|
|
|
|
|
System.out.println("警告:未能生成足够数量的不重复题目。可能需要更多尝试。");
|
|
|
|
|
System.out.println("本次成功生成 " + newQuestions.size() + " 道题目。");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!newQuestions.isEmpty()) {
|
|
|
|
|
QuizManager.saveQuizToFile(currentUsername, newQuestions);
|
|
|
|
|
}
|
|
|
|
|
return newQuestions;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|