feat: 完善登录后题目处理逻辑

develop
peterpan 1 week ago
parent 232c548d77
commit fb4d0a064c

@ -5,6 +5,7 @@ import model.User;
import java.util.Scanner;
public class Main {
private static final AuthService authService = new AuthService();
private static final AuthService.TestPaperService testPaperService = new AuthService.TestPaperService();
private static User currentUser = null;
private static Level currentLevel = null;
@ -37,4 +38,29 @@ public class Main {
System.out.println("输入格式不正确,请重新输入。");
}
}
private static void handleQuestionGeneration(Scanner scanner) {
System.out.printf("准备生成%s数学题目, 请输入生成题目数量(输入-1将退出当前用户, 重新登录):%n", currentLevel.getName()); // 生成提示
String input = scanner.nextLine();
// if (input.startsWith("切换为")) { // 切换类型功能
// handleLevelSwitch(input);
// return;
// }
try {
int count = Integer.parseInt(input);
if (count == -1) {
currentUser = null;
currentLevel = null;
System.out.println("已退出,请重新登录。");
} else if (count >= 10 && count <= 30) { // 题目数量的有效输入范围是“10-30”
// testPaperService.generateAndSave(currentUser, currentLevel, count);
} else {
System.out.println("输入无效请输入10-30之间的数字或-1。");
}
} catch (NumberFormatException e) {
System.out.println("输入无效,请输入一个数字。");
}
}
}

@ -3,8 +3,13 @@ package auth;
import model.Level;
import model.User;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Stream;
public class AuthService {
private final List<User> users;
@ -22,4 +27,41 @@ public class AuthService {
users.add(new User("王五2", "123", Level.SENIOR_HIGH));
users.add(new User("王五3", "123", Level.SENIOR_HIGH));
}
public Optional<User> login(String username, String password) {
return users.stream()
.filter(user -> user.getUsername().equals(username) && user.getPassword().equals(password))
.findFirst();
}
public static class TestPaperService {
public Set<String> loadExistingQuestions(String username) {
Set<String> questions = new HashSet<>();
File userDir = new File(username);
if (!userDir.exists() || !userDir.isDirectory()) {
return questions;
}
try (Stream<Path> paths = Files.walk(Paths.get(username))) {
paths.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".txt"))
.forEach(path -> {
try {
Files.lines(path).forEach(line -> {
// 简单的题目提取逻辑,假设每行都是一个题目
if (line.matches("^\\d+\\.\\s.*")) {
questions.add(line.substring(line.indexOf(' ') + 1).trim());
}
});
} catch (IOException e) {
System.err.println("读取历史文件失败: " + path);
}
});
} catch (IOException e) {
System.err.println("加载历史题目时出错: " + e.getMessage());
}
return questions;
}
}
}

Loading…
Cancel
Save