diff --git a/out/production/TestPaperGenerationSystem/Application.class b/out/production/TestPaperGenerationSystem/Application.class index 0177629..e24d313 100644 Binary files a/out/production/TestPaperGenerationSystem/Application.class and b/out/production/TestPaperGenerationSystem/Application.class differ diff --git a/out/production/TestPaperGenerationSystem/SessionManager.class b/out/production/TestPaperGenerationSystem/SessionManager.class index 7637efd..32dc1bc 100644 Binary files a/out/production/TestPaperGenerationSystem/SessionManager.class and b/out/production/TestPaperGenerationSystem/SessionManager.class differ diff --git a/src/Application.java b/src/Application.java index fa307fb..8a36ada 100644 --- a/src/Application.java +++ b/src/Application.java @@ -79,10 +79,10 @@ public class Application { * 处理用户已登录状态下的逻辑。 */ private void handleLoggedInState() throws IOException { - String currentLevelName = sessionManager.getCurrentLevelName(); + String currentLevel = sessionManager.getCurrentLevelName(); System.out.printf( "当前选择为%s出题。请输入生成题目数量(10-30),或输入 '切换为XX',或输入 '-1' 退出登录:%n", - currentLevelName); + currentLevel); String input = scanner.nextLine().trim(); if ("-1".equals(input)) { @@ -90,10 +90,7 @@ public class Application { } else if (isSwitchCommand(input)) { handleSwitchLevel(input); } else if (isNumeric(input)) { - int count = Integer.parseInt(input); - if(validateGenerateCount(count)) { - handleGenerateProblems(count); - } + handleGenerateProblems(input); } else { System.out.println("无效的选项,请重新输入"); } @@ -116,7 +113,8 @@ public class Application { } } - private void handleGenerateProblems(int count) throws IOException { + private void handleGenerateProblems(String input) throws IOException { + int count = Integer.parseInt(input); User currentUser = sessionManager.getCurrentUser(); System.out.println("正在加载历史题目,请稍候..."); Set history = fileService.loadAllProblemHistory(currentUser); @@ -125,11 +123,10 @@ public class Application { System.out.println("正在生成 " + count + " 道不重复的新题目..."); IProblemGenerator generator = sessionManager.getCurrentGenerator(); List newProblems = generator.generate(count, history); - //命令行显示生成题目 + // 命令行显示生成题目 for (int i = 0; i < newProblems.size(); i++) { String problemLine = (i + 1) + ". " + newProblems.get(i).toString(); System.out.println(problemLine); - // 满足题目之间空一行的格式要求 System.out.println(); } @@ -143,15 +140,15 @@ public class Application { } private boolean isNumeric(String str) { - return str != null && str.matches("-?\\d+"); - } - - private boolean validateGenerateCount(int count) { - if (count < 10 || count > 30) { - System.out.println("生成数量必须在10到30之间。"); - return false; + if (str != null && str.matches("\\d+")){ + int count = Integer.parseInt(str); + if (count < 10 || count > 30) { + System.out.println("生成数量必须在10到30之间。"); + return false; + } + return true; } - return true; + return false; } public static void main(String[] args) throws IOException { diff --git a/src/AuthService.java b/src/AuthService.java index 9e358a2..7058d98 100644 --- a/src/AuthService.java +++ b/src/AuthService.java @@ -17,7 +17,7 @@ public class AuthService { * * @param username 用户名 * @param password 密码 - * @return 如果认证成功,返回对应的 User 对象;否则返回一个空的 Optional + * @return 如果认证成功,返回ture,否则返回false */ public Optional login(String username, String password) { Optional userOptional = userRepository.findByUsername(username); diff --git a/src/SessionManager.java b/src/SessionManager.java index cf490c7..25c25e5 100644 --- a/src/SessionManager.java +++ b/src/SessionManager.java @@ -9,6 +9,7 @@ public final class SessionManager { private User currentUser; private IProblemGenerator currentGenerator; + private String currentLevel; private SessionManager() { // 私有构造函数,防止外部实例化 @@ -74,6 +75,8 @@ public final class SessionManager { } private void setGeneratorByLevel(String level) { + String tempLevel = this.currentLevel; + this.currentLevel = level; switch (level) { case "小学": this.currentGenerator = new ElementaryProblemGenerator(); @@ -86,6 +89,7 @@ public final class SessionManager { break; default: System.out.println("错误的难度等级: " + level); + this.currentLevel = tempLevel; } } @@ -95,13 +99,6 @@ public final class SessionManager { * @return 中文级别名称,如 "小学" */ public String getCurrentLevelName() { - if (currentGenerator instanceof HighSchoolProblemGenerator) { - return "高中"; - } else if (currentGenerator instanceof MiddleSchoolProblemGenerator) { - return "初中"; - }else if (currentGenerator instanceof ElementaryProblemGenerator) { - return "小学"; - } - return "未知"; + return currentLevel; } } \ No newline at end of file