diff --git a/src/main/java/com/ybw/mathapp/entity/User.java b/src/main/java/com/ybw/mathapp/entity/User.java
index 13cfb82..fa07684 100644
--- a/src/main/java/com/ybw/mathapp/entity/User.java
+++ b/src/main/java/com/ybw/mathapp/entity/User.java
@@ -1,44 +1,89 @@
package com.ybw.mathapp.entity;
/**
- * 用户实体类
+ * 用户实体类,表示系统中的用户信息。
+ *
+ *
该类包含用户的基本信息,如用户名、密码和学习级别。
+ * 用户级别可以是小学、初中或高中。
+ *
+ * @author 杨博文
+ * @version 1.0
+ * @since 2025
*/
public class User {
+
+ public String getName() {
+ return name;
+ }
+
+ /** 用户名,不可修改。 */
+ private final String name;
+
+ /** 邮箱,不可修改。 */
private final String email;
+
+ /** 用户密码,不可修改。 */
private final String password;
+
+ /** 用户当前的学习级别,可以修改。 */
private String level;
/**
* 构造一个新的用户对象。
*
- * @param email 邮箱,不能为空
+ * @param email 邮箱,不能为空 用户名,不能为空
* @param password 用户密码,不能为空
*/
- public User(String email, String password) {
- this.email = email;
+ public User(String name, String email, String password) {
+ this.name = name;
this.password = password;
- this.level = "小学"; // 默认级别
- }
-
- public String getEmail() {
- return email;
+ this.email = email;
}
+ /**
+ * 获取用户密码。
+ *
+ * @return 用户密码
+ */
public String getPassword() {
return password;
}
+ /**
+ * 获取用户当前的学习级别。
+ *
+ * @return 用户学习级别
+ */
public String getLevel() {
return level;
}
- public void setLevel(String level) {
- this.level = level;
+ /**
+ * 获取用户邮箱。
+ *
+ * @return 用户邮箱
+ */
+ public String getEmail() {
+ return email;
+ }
+
+ /**
+ * 设置用户的学习级别。
+ *
+ * @param newLevel 新的学习级别,支持"小学"、"初中"、"高中"
+ */
+ public void setLevel(String newLevel) {
+ level = newLevel;
}
+ /**
+ * 保存邮箱+密码。
+ *
+ * @return 邮箱+密码
+ */
@Override
public String toString() {
- return email + "," + password;
+ return name + "," + email + "," + password;
}
public static User fromString(String line) {
@@ -46,12 +91,10 @@ public class User {
return null;
}
- String[] parts = line.split(",", 2);
- if (parts.length == 2) {
- return new User(parts[0].trim(), parts[1].trim());
+ String[] parts = line.split(",", 3); // 最多分割成3部分
+ if (parts.length == 3) {
+ return new User(parts[0].trim(), parts[1].trim(), parts[2].trim());
}
return null;
}
-
-
-}
\ No newline at end of file
+}
diff --git a/src/main/java/com/ybw/mathapp/service/MultipleChoiceGenerator.java b/src/main/java/com/ybw/mathapp/service/MultipleChoiceGenerator.java
index f245989..8c5c6ff 100644
--- a/src/main/java/com/ybw/mathapp/service/MultipleChoiceGenerator.java
+++ b/src/main/java/com/ybw/mathapp/service/MultipleChoiceGenerator.java
@@ -140,60 +140,60 @@ public class MultipleChoiceGenerator {
return options;
}
+ // ... (其他类成员和方法保持不变)
+
// --- 表达式分词逻辑 ---
// 将 "3 + 开根号 ( 4 ) 平方 - sin 30" 分割成 ["3", "+", "开根号", "(", "4", ")", "平方", "-", "sin", "30"]
private List tokenizeExpression(String expression) {
List tokens = new ArrayList<>();
- StringBuilder currentToken = new StringBuilder();
- String[] advancedOps = {"平方", "开根号", "sin", "cos", "tan"};
+ int i = 0;
+ while (i < expression.length()) {
+ String token = _findNextToken(expression, i);
+ if (token != null) {
+ tokens.add(token);
+ i += token.length();
+ } else {
+ // 如果找不到匹配的 token,可能是单个字符或未知格式
+ tokens.add(String.valueOf(expression.charAt(i)));
+ i++;
+ }
+ }
+ return tokens;
+ }
- for (int i = 0; i < expression.length(); ) {
- char c = expression.charAt(i);
+ /**
+ * 查找从指定位置开始的下一个 token。
+ * @param expression 表达式字符串
+ * @param startPos 起始查找位置
+ * @return 找到的 token,如果未找到则返回 null
+ */
+ private String _findNextToken(String expression, int startPos) {
+ String[] advancedOps = {"平方", "开根号", "sin", "cos", "tan"};
+ char c = expression.charAt(startPos);
- if (Character.isWhitespace(c)) {
- if (currentToken.length() > 0) {
- tokens.add(currentToken.toString());
- currentToken.setLength(0);
- }
- i++;
- } else if (c == '(' || c == ')') {
- if (currentToken.length() > 0) {
- tokens.add(currentToken.toString());
- currentToken.setLength(0);
- }
- tokens.add(String.valueOf(c));
- i++;
- } else if (Character.isDigit(c) || c == '.') {
- currentToken.append(c);
- i++;
- } else { // 可能是运算符
- // Check for multi-character operators first
- boolean foundOp = false;
- for (String op : advancedOps) {
- if (expression.startsWith(op, i)) {
- if (currentToken.length() > 0) {
- tokens.add(currentToken.toString());
- currentToken.setLength(0);
- }
- tokens.add(op);
- i += op.length();
- foundOp = true;
- break;
- }
- }
- if (!foundOp) { // Not a known multi-char op, treat as single char (likely +, -, *, /)
- if (currentToken.length() > 0) {
- tokens.add(currentToken.toString());
- currentToken.setLength(0);
- }
- currentToken.append(c);
- i++;
- }
+ if (Character.isWhitespace(c)) {
+ return null; // 空格由调用者处理
+ }
+ if (c == '(' || c == ')') {
+ return String.valueOf(c);
+ }
+ if (Character.isDigit(c) || c == '.') {
+ // 查找连续的数字或小数点
+ int j = startPos;
+ while (j < expression.length() && (Character.isDigit(expression.charAt(j)) || expression.charAt(j) == '.')) {
+ j++;
}
+ return expression.substring(startPos, j);
}
- if (currentToken.length() > 0) {
- tokens.add(currentToken.toString());
+
+ // 检查多字符运算符
+ for (String op : advancedOps) {
+ if (expression.startsWith(op, startPos)) {
+ return op;
+ }
}
- return tokens;
+
+ // 如果不是多字符运算符,则认为是单字符运算符 (+, -, *, / 等)
+ return String.valueOf(c);
}
}
\ No newline at end of file
diff --git a/src/main/java/com/ybw/mathapp/system/LogSystem.java b/src/main/java/com/ybw/mathapp/system/LogSystem.java
deleted file mode 100644
index a610d31..0000000
--- a/src/main/java/com/ybw/mathapp/system/LogSystem.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package com.ybw.mathapp.system;
-
-import com.ybw.mathapp.entity.User;
-import java.util.HashMap;
-import java.util.Scanner;
-
-public class LogSystem {
- private final HashMap userHashMap = new HashMap<>();
-
- public void userHashMapInit() {
- // 小学
- userHashMap.put("1798231811@qq.com", new User("1798231811@qq.com", "1234567"));
- }
-
- public User login() {
- System.out.println("请输入用户名和密码,两者之间用空格隔开,用户名为邮箱账号");
- while(true) {
- Scanner scanner = new Scanner(System.in);
- String[] info = scanner.nextLine().split(" ");
- if(info.length != 2) {
- System.out.println("请输入正确格式");
- } else {
- String name = info[0];
- String password = info[1];
- User user = userHashMap.get(name);
- if (user == null) {
- System.out.println("邮箱未注册");
- }
- else if (!user.getPassword().equals(password)) {
- System.out.println("请输入正确的用户名、密码");
- }
- else {
- System.out.println("当前选择为" + user.getLevel() + "出题");
- return user;
- }
- }
- }
- }
-}
diff --git a/src/main/java/com/ybw/mathapp/util/ChangePassword.java b/src/main/java/com/ybw/mathapp/util/ChangePassword.java
new file mode 100644
index 0000000..72af3df
--- /dev/null
+++ b/src/main/java/com/ybw/mathapp/util/ChangePassword.java
@@ -0,0 +1,91 @@
+package com.ybw.mathapp.util;
+
+import static com.ybw.mathapp.util.LoginFileUtils.USER_FILE;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class ChangePassword {
+ static final List lines = new ArrayList<>();
+ static String userLine = null;
+ static int userLineNumber = -1;
+
+ // 前端接口-修改密码
+ public static boolean changePassword(String name, String newPassword) {
+ File file = new File(USER_FILE);
+ if (!file.exists()) {
+ System.out.println("用户文件不存在: " + USER_FILE);
+ return false;
+ }
+
+ // 1. 读取文件,查找用户
+ if(!findUserLine(name, file)) {
+ return false;
+ }
+
+ if (userLine == null || userLineNumber == -1) {
+ // 用户未找到
+ System.out.println("用户 '" + name + "' 不存在,修改失败。");
+ return false;
+ }
+
+ // 2. 更新找到的用户行中的密码
+ String[] parts = userLine.split(",");
+ if (parts.length != 3) {
+ return false;
+ }
+ parts[2] = newPassword; // 假设密码是第三个字段
+ String updatedLine = String.join(",", parts);
+
+ lines.set(userLineNumber, updatedLine); // 替换列表中的旧行
+
+ // 3. 将更新后的内容写回文件
+ if(!writeBack(lines, file)) {
+ return false;
+ }
+
+ return true;
+ }
+
+ public static boolean writeBack(List lines, File file) {
+ try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
+ for (String l : lines) {
+ writer.write(l);
+ writer.newLine();
+ }
+ } catch (IOException e) {
+ System.err.println("写入文件时出错: " + e.getMessage());
+ return false; // 如果写回失败,认为修改未成功
+ }
+ return true;
+ }
+
+ public static boolean findUserLine(String name, File file) {
+ try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
+ String line;
+ int currentLineNum = 0;
+ while ((line = reader.readLine()) != null) {
+ lines.add(line);
+ String[] parts = line.split(",");
+ // 假设格式为: username,email,password
+ if (parts.length >= 3 && parts[0].equals(name)) {
+ userLine = line; // 找到用户行
+ userLineNumber = currentLineNum;
+ break; // 找到后可以退出循环
+ }
+ currentLineNum++;
+ }
+ } catch (IOException e) {
+ System.err.println("读取文件时出错: " + e.getMessage());
+ return false;
+ }
+ return true;
+ }
+
+}
diff --git a/src/main/java/com/ybw/mathapp/util/Login.java b/src/main/java/com/ybw/mathapp/util/Login.java
index 821d19c..83f18c0 100644
--- a/src/main/java/com/ybw/mathapp/util/Login.java
+++ b/src/main/java/com/ybw/mathapp/util/Login.java
@@ -1,13 +1,11 @@
package com.ybw.mathapp.util;
-import java.util.regex.Pattern;
-
public class Login {
// 前端接口-登录成功or失败
- public static boolean login(String email, String password) {
- if (LoginFileUtils.validateUser(email, password)) {
- System.out.println("登录成功!欢迎回来," + email);
+ public static boolean login(String name, String password) {
+ if (LoginFileUtils.validateUser(name, password)) {
+ System.out.println("登录成功!欢迎回来," + name);
return true;
} else {
System.out.println("邮箱或密码错误!");
diff --git a/src/main/java/com/ybw/mathapp/util/LoginFileUtils.java b/src/main/java/com/ybw/mathapp/util/LoginFileUtils.java
index f99a56f..ce1b62b 100644
--- a/src/main/java/com/ybw/mathapp/util/LoginFileUtils.java
+++ b/src/main/java/com/ybw/mathapp/util/LoginFileUtils.java
@@ -6,10 +6,9 @@ import java.util.ArrayList;
import java.util.List;
public class LoginFileUtils {
- private static final String USER_FILE = "users.txt";
+ static final String USER_FILE = "users.txt";
// 读取所有用户
- // FileUtils.java 中的 readUsers 方法(简化版)
public static List readUsers() {
List users = new ArrayList<>();
File file = new File(USER_FILE);
@@ -49,7 +48,7 @@ public class LoginFileUtils {
}
}
- // 检查邮箱是否已注册
+ // 前端接口-检查邮箱是否已注册
public static boolean isEmailRegistered(String email) {
List users = readUsers();
for (User user : users) {
@@ -60,12 +59,24 @@ public class LoginFileUtils {
return false;
}
- // 验证用户登录
- public static boolean validateUser(String email, String password) {
+ // 前端接口-检查用户名是否已注册
+ public static boolean isNameRegistered(String name) {
List users = readUsers();
for (User user : users) {
- if (user.getEmail().equalsIgnoreCase(email) &&
- user.getPassword().equals(password)) {
+ if (user.getName().equalsIgnoreCase(name)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ // 前端接口-验证用户登录
+ public static boolean validateUser(String emailOrName, String password) {
+ List users = readUsers();
+ for (User user : users) {
+ if ((user.getEmail().equalsIgnoreCase(emailOrName)
+ || user.getName().equalsIgnoreCase(emailOrName) )
+ && user.getPassword().equals(password)) {
return true;
}
}
diff --git a/src/main/java/com/ybw/mathapp/util/Register.java b/src/main/java/com/ybw/mathapp/util/Register.java
index 4ad06cd..a79fb51 100644
--- a/src/main/java/com/ybw/mathapp/util/Register.java
+++ b/src/main/java/com/ybw/mathapp/util/Register.java
@@ -6,8 +6,8 @@ import java.util.regex.Pattern;
public class Register {
// 前端接口-完成注册
- public static boolean register(String email, String password1) {
- User user = new User(email, password1);
+ public static boolean register(String name, String email, String password1) {
+ User user = new User(name, email, password1);
LoginFileUtils.saveUser(user);
System.out.println("注册成功!您可以使用邮箱和密码登录了。");
return true;