1 #7

Merged
hnu202326010422 merged 9 commits from yuxiuhui_branch into develop 4 months ago

@ -1,2 +0,0 @@
# partner_project

@ -0,0 +1,8 @@
import ui.MainApplication;
public class Main {
public static void main(String[] args) {
// R1: 启动图形化界面应用 (JavaFX)
MainApplication.launch(MainApplication.class, args);
}
}

@ -0,0 +1,206 @@
package auth;
import util.ModifyUtils;
import util.EmailService;
import java.io.*;
import java.util.*;
import java.util.regex.Pattern;
public class AuthService {
private static final String USER_FILE_PATH = "user.txt";
private final EmailService emailService = new EmailService();
public AuthService() {
// 检查用户文件是否存在
File userFile = new File(USER_FILE_PATH);
if (!userFile.exists()) {
System.err.println("用户文件不存在: " + USER_FILE_PATH);
try {
// 尝试创建文件
if (userFile.createNewFile()) {
System.out.println("已创建用户文件: " + USER_FILE_PATH);
}
} catch (IOException e) {
System.err.println("创建用户文件失败: " + e.getMessage());
}
}
}
// --- 校验和辅助方法 ---
private static final Pattern EMAIL_PATTERN =
Pattern.compile("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}$");
// R3: 密码校验: 6-10 位, 必须含大小写字母和数字
public static boolean isPasswordValid(String password) {
if (password == null || password.length() < 6 ||
password.length() > 10) {
return false;
}
Pattern pattern =
Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{6,10}$");
return pattern.matcher(password).matches();
}
public boolean validateEmail(String email) {
if (email == null){
return false;
}
return EMAIL_PATTERN.matcher(email).matches();
}
// --- 用户加载和保存 ---
private User parseLine(String line, int lineNumber) {
line = line.trim();
if (line.isEmpty()) {
return null;
}
String[] parts = line.split("\\s+");
if (parts.length != 4) {
System.out.println("警告: 第" + lineNumber +
"行数据格式不正确需要4个数据。");
return null;
}
return new User(parts[0], parts[1], parts[2], parts[3]);
}
public List<User> loadUsers() {
List<User> users = new ArrayList<>();
int lineNumber = 1;
try (BufferedReader br = new BufferedReader(new FileReader(USER_FILE_PATH))) {
String line;
while ((line = br.readLine()) != null) {
User user = parseLine(line, lineNumber);
if (user != null) {
users.add(user);
}
lineNumber++;
}
} catch (IOException e) {
System.out.println("读取文件时发生错误: " + e.getMessage());
}
return users;
}
// --- 认证和注册 ---
public User login(String username, String password) {
List<User> users = loadUsers();
for (User user : users) {
if ((user.getUsername().equals(username) || user.getEmail().equals(username)) &&
user.getPassword().equals(password)) {
return user;
}
}
return null;
}
public boolean isUsernameOrEmailTaken(String username, String email) {
List<User> users = loadUsers();
for (User user : users) {
if ((username != null && user.getUsername().equals(username)) ||
(email != null && user.getEmail().equals(email))) {
return true;
}
}
return false;
}
// R2: 实际发送验证邮件
public String sendVerificationCode(String email) {
if (!validateEmail(email) || isUsernameOrEmailTaken(null, email)) {
return null;
}
String code = emailService.generateVerificationCode();
if (emailService.sendVerificationEmail(email, code)) {
return code;
}
return null;
}
private void writeUserData(String userData) throws IOException {
try (BufferedWriter writer =
new BufferedWriter(new FileWriter(USER_FILE_PATH, true))) {
writer.write(userData);
writer.newLine();
}
}
// R2: 注册用户
public boolean registerUser(String username, String password, String email) {
if (isUsernameOrEmailTaken(username, email)) {
return false;
}
// 格式: username password type email
String userData = username + " " + password + " 小学 " + email;
try {
writeUserData(userData);
return true;
} catch (IOException e) {
System.out.println("写入文件时发生错误: " + e.getMessage());
return false;
}
}
// --- 用户修改 ---
public boolean updatePassword(String username, String newPassword) {
// 索引 1 是密码
int result = ModifyUtils.modifyFileField(USER_FILE_PATH, username, 1, newPassword);
return result != -1;
}
public boolean updateDifficulty(String username, String newDifficulty) {
List<String> lines = new ArrayList<>();
boolean found = false;
try (BufferedReader br = new BufferedReader(new FileReader(USER_FILE_PATH))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.trim().split("\\s+");
if (parts.length >= 4 && parts[0].equals(username)) {
// 找到目标用户,更新难度
parts[2] = newDifficulty;
line = String.join(" ", parts);
found = true;
}
lines.add(line);
}
} catch (IOException e) {
System.err.println("读取用户文件错误: " + e.getMessage());
return false;
}
if (!found) {
System.err.println("未找到用户: " + username);
return false;
}
// 写回文件
try (BufferedWriter bw = new BufferedWriter(new FileWriter(USER_FILE_PATH))) {
for (String line : lines) {
bw.write(line);
bw.newLine();
}
return true;
} catch (IOException e) {
System.err.println("写入用户文件错误: " + e.getMessage());
return false;
}
}
}

@ -0,0 +1,20 @@
package auth;
public class User {
private String username;
private String password;
private String type;
private String email;
public User(String username, String password, String type, String email) {
this.username = username;
this.password = password;
this.type = type;
this.email = email;
}
public String getUsername() { return username; }
public String getPassword() { return password; }
public String getType() { return type; }
public String getEmail() { return email; }
}

@ -0,0 +1,13 @@
package auth;
public class UserManager {
private final AuthService authService;
public UserManager() {
this.authService = new AuthService();
}
public User login(String username, String password) {
return authService.login(username, password);
}
}

@ -0,0 +1,28 @@
package generator;
import util.ExpressionUtils;
import java.util.*;
public class HighGenerator implements ProblemGenerator {
@Override
public List<Problem> generateProblems(int count) {
List<Problem> problems = new ArrayList<>();
int attempts = 0;
final int maxAttempts = count * 3; // 最多尝试3倍数量
while (problems.size() < count && attempts < maxAttempts) {
Problem problem = ExpressionUtils.generateHighExpr();
if (problem != null) {
problems.add(problem);
}
attempts++;
}
// 如果无法生成足够题目,用简单题目填充
while (problems.size() < count) {
problems.add(ExpressionUtils.createProblem("sin(30) + " + (problems.size() + 1)));
}
return problems;
}
}

@ -0,0 +1,15 @@
package generator;
import util.ExpressionUtils;
import java.util.*;
public class MiddleGenerator implements ProblemGenerator {
@Override
public List<Problem> generateProblems(int count) {
List<Problem> problems = new ArrayList<>();
for (int i = 0; i < count; i++) {
problems.add(ExpressionUtils.generateMiddleExpr());
}
return problems;
}
}

@ -0,0 +1,15 @@
package generator;
import util.ExpressionUtils;
import java.util.*;
public class PrimaryGenerator implements ProblemGenerator {
@Override
public List<Problem> generateProblems(int count) {
List<Problem> problems = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
problems.add( ExpressionUtils.generatePrimaryExpr());
}
return problems;
}
}

@ -0,0 +1,61 @@
package generator;
import java.util.List;
import java.util.Collections;
public class Problem {
private final String expression;
private final double result;
private final List<String> options;
private final String correctAnswerOption;
public Problem(String expression, double result, List<String> options,
String correctAnswerOption) {
this.expression = expression;
this.result = result;
this.options = options;
this.correctAnswerOption = correctAnswerOption;
}
public String getQuestionText() {
return "计算: " + expression + " = ?";
}
public String getExpression() {
return expression;
}
public List<String> getOptions() {
return Collections.unmodifiableList(options);
}
public String getCorrectAnswerOption() {
return formatResult(result);
}
private String formatResult(double result) {
if (Double.isNaN(result)) {
return "Error";
}
if (Math.abs(result - Math.round(result)) < 0.0001) {
return String.valueOf((int) Math.round(result));
}
return String.format("%.2f", result);
}
public double getResult() {
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getQuestionText()).append("\n");
char optionChar = 'A';
for (String option : options) {
sb.append(optionChar++).append(". ").append(option).append(" ");
}
sb.append("\n正确答案: ").append(correctAnswerOption);
return sb.toString();
}
}
Loading…
Cancel
Save