You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
projectone/MathPaperGenerator.java

161 lines
5.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import java.io.IOException;
import java.util.List;
import java.util.Scanner;
/**
* 数学卷子自动生成程序主类
*/
public class MathPaperGenerator {
private UserManager userManager;
private MathProblemGenerator problemGenerator;
private FileManager fileManager;
private Scanner scanner;
public MathPaperGenerator() {
this.userManager = new UserManager();
this.problemGenerator = new MathProblemGenerator();
this.fileManager = new FileManager();
this.scanner = new Scanner(System.in);
}
/**
* 程序主入口
*/
public static void main(String[] args) {
MathPaperGenerator generator = new MathPaperGenerator();
generator.run();
}
/**
* 运行主程序
*/
public void run() {
System.out.println("欢迎使用中小学数学卷子自动生成程序!");
System.out.println("=" + "=".repeat(40));
while (true) {
User currentUser = login();
if (currentUser != null) {
handleUserSession(currentUser);
}
}
}
/*用户登录*/
private User login() {
while (true) {
System.out.print("请输入用户名和密码(用空格隔开): ");
String input = scanner.nextLine().trim();
if (input.isEmpty()) {
System.out.println("输入不能为空,请重新输入。");
continue;
}
String[] parts = input.split("\\s+");
String username = parts[0];
String password = parts[1];
User user = userManager.authenticate(username, password);
if (user != null) {
System.out.println("登录成功!当前选择为 " + user.getUserType() + " 出题");
return user;
} else {
System.out.println("请输入正确的用户名、密码");
}
}
}
/**
* 处理用户会话
* @param user 当前登录用户
*/
private void handleUserSession(User user) {
String currentDifficulty = user.getUserType();
while (true) {
System.out.println("准备生成 " + currentDifficulty + " 数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录");
String input = scanner.nextLine().trim();
// 检查是否要切换类型
if (input.startsWith("切换为")) {
String newType = input.substring(3).trim();
if (userManager.isValidUserType(newType)) {
currentDifficulty = newType;
System.out.println("已切换为 " + newType + " 出题模式");
continue;
} else {
System.out.println("请输入小学、初中和高中三个选项中的一个");
continue;
}
}
try {
int count = Integer.parseInt(input);
if (count == -1) {
System.out.println("退出当前用户,重新登录...");
System.out.println();
return;
}
if (count < 10 || count > 30) {
System.out.println("题目数量必须在10-30之间请重新输入。");
continue;
}
// 生成题目
generateAndSavePaper(user, currentDifficulty, count);
} catch (NumberFormatException e) {
System.out.println("请输入有效的数字。");
}
}
}
/**
* 生成并保存题目
* @param user 用户
* @param difficulty 难度
* @param count 题目数量
*/
private void generateAndSavePaper(User user, String difficulty, int count) {
try {
System.out.println("正在生成 " + count + " 道 " + difficulty + " 数学题目...");
// 生成不重复的题目
List<MathProblem> problems = fileManager.generateUniqueProblems(
user, problemGenerator, count, difficulty);
if (problems.size() < count) {
System.out.println("注意:由于重复限制,实际生成了 " + problems.size() + " 道题目。");
}
// 保存到文件
String filePath = fileManager.savePaperToFile(user, problems, difficulty);
System.out.println("题目生成完成!");
System.out.println("文件已保存至: " + filePath);
System.out.println("题目预览:");
System.out.println("-" + "-".repeat(30));
// 显示前5道题目作为预览
int previewCount = Math.min(5, problems.size());
for (int i = 0; i < previewCount; i++) {
System.out.println((i + 1) + ". " + problems.get(i).toString());
}
if (problems.size() > 5) {
System.out.println("... 还有 " + (problems.size() - 5) + " 道题目");
}
System.out.println("-" + "-".repeat(30));
System.out.println();
} catch (IOException e) {
System.err.println("保存文件时出错: " + e.getMessage());
} catch (Exception e) {
System.err.println("生成题目时出错: " + e.getMessage());
}
}
}