import java.io.IOException; import java.util.*; import java.util.regex.Pattern; import Generator.*; class MathExamGenerator { private static List accounts = new ArrayList<>(); private static Scanner scanner = new Scanner(System.in); private static QuestionGenerator QuestionGenerator = new PrimaryGenerator(); private static FileManager fileManager = new FileManager(); private static Account currentAccount = null; // 初始化账户数据 static { // 小学账户 accounts.add(new Account("张三1", "123", "小学")); accounts.add(new Account("张三2", "123", "小学")); accounts.add(new Account("张三3", "123", "小学")); // 初中账户 accounts.add(new Account("李四1", "123", "初中")); accounts.add(new Account("李四2", "123", "初中")); accounts.add(new Account("李四3", "123", "初中")); // 高中账户 accounts.add(new Account("王五1", "123", "高中")); accounts.add(new Account("王五2", "123", "高中")); accounts.add(new Account("王五3", "123", "高中")); } public static void main(String[] args) { System.out.println("=== 中小学数学卷子自动生成程序 ==="); while (true) { if (currentAccount == null) { login(); } else { generateExam(); } } } // 登录功能 private static void login() { while (true) { System.out.print("请输入用户名和密码(用空格隔开,输入exit退出程序): "); String input = scanner.nextLine().trim(); if (input.equalsIgnoreCase("exit")) { System.exit(0); } String[] credentials = input.split(" "); if (credentials.length != 2) { System.out.println("请输入正确的用户名、密码格式(用户名 密码)"); continue; } String username = credentials[0]; String password = credentials[1]; for (Account account : accounts) { if (account.getUsername().equals(username) && account.getPassword().equals(password)) { currentAccount = account; if( account.getType().equals("小学") ) { QuestionGenerator = new PrimaryGenerator(); } else if( account.getType().equals("初中") ) { QuestionGenerator = new middleGenerator(); } else if( account.getType().equals("高中") ) { QuestionGenerator = new highGenerator(); } // QuestionGenerator = new QuestionGenerator(account.getType()); System.out.println("当前选择为 " + QuestionGenerator.getType() + " 出题"); return; } } System.out.println("请输入正确的用户名、密码"); } } // 生成试卷功能 private static void generateExam() { while (true) { System.out.print("准备生成 " + QuestionGenerator.getType() + " 数学题目,请输入生成题目数量(10-30,输入-1退出当前用户,输入'switch'切换类型): "); String input = scanner.nextLine().trim(); if ( input.equals("-1") ) { currentAccount = null; return; } if ( input.startsWith("切换为") ) { handleSwitchType(input); continue; } if ( input.equalsIgnoreCase("switch" )) { System.out.print("请输入要切换的类型(小学/初中/高中): "); String typeInput = scanner.nextLine().trim(); handleSwitchType("切换为 " + typeInput); continue; } try { int count = Integer.parseInt(input); if (count >= 10 && count <= 30) { generateQuestions(count); } else { System.out.println("题目数量必须在10-30之间"); } } catch (NumberFormatException e) { System.out.println("请输入有效的数字(10-30)或-1退出"); } } } // 处理类型切换 private static void handleSwitchType(String input) { Pattern pattern = Pattern.compile("切换为\\s*(小学|初中|高中)"); var matcher = pattern.matcher(input); if ( matcher.matches() ) { String newType = matcher.group(1); if( newType.equals("小学") ) { QuestionGenerator = new PrimaryGenerator(); } else if( newType.equals("初中") ) { QuestionGenerator = new middleGenerator(); } else if( newType.equals("高中") ) { QuestionGenerator = new highGenerator(); } //else{ QuestionGenerator = new highGenerator();} // QuestionGenerator.setType(newType); // currentAccount.SetType(newType); System.out.println("已切换为 " + QuestionGenerator.getType() + " 出题"); //System.out.print("准备生成 " + newType + " 数学题目,请输入生成题目数量: "); } else { System.out.println("请输入小学、初中和高中三个选项中的一个"); } } // 生成题目 private static void generateQuestions(int count) { List questions = new ArrayList<>(); Set generatedQuestions = new HashSet<>(); for (int i = 0; i < count; i++) { String question; int attempt = 0; // 尝试生成不重复的题目 do { question = QuestionGenerator.generateQuestion(); attempt++; // 防止无限循环 if ( attempt > 100 ) { System.out.println("警告:无法生成足够的不重复题目,可能重复率较高"); break; } } while ( generatedQuestions.contains(question ) || fileManager.isQuestionDuplicate(currentAccount.getUsername(), question)); generatedQuestions.add(question); questions.add(question); } try { fileManager.saveQuestions(currentAccount.getUsername(), questions); System.out.println("成功生成 " + count + " 道题目"); } catch ( IOException e ) { System.out.println("保存文件时出错: " + e.getMessage()); } } }