Merge branch 'ybw_branch' of https://bdgit.educoder.net/ppy4sjqvf/pairedProject
# Conflicts: # src/main/java/com/ybw/mathapp/entity/User.java # src/main/java/com/ybw/mathapp/system/LogSystem.javapull/2/head
commit
2e85231da8
@ -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<String, User> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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<String> 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<String> 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;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue