You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
projectone/src/UserManager.java

58 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import java.util.HashMap;
import java.util.Map;
/**
* 用户管理类,负责用户验证和账户管理
*/
public class UserManager {
private Map<String, User> users;
public UserManager() {
initializeUsers();
}
/**
* 初始化预设用户账户
*/
private void initializeUsers() {
users = new HashMap<>();
// 小学账户
users.put("张三1", new User("张三1", "123", "小学"));
users.put("张三2", new User("张三2", "123", "小学"));
users.put("张三3", new User("张三3", "123", "小学"));
// 初中账户
users.put("李四1", new User("李四1", "123", "初中"));
users.put("李四2", new User("李四2", "123", "初中"));
users.put("李四3", new User("李四3", "123", "初中"));
// 高中账户
users.put("王五1", new User("王五1", "123", "高中"));
users.put("王五2", new User("王五2", "123", "高中"));
users.put("王五3", new User("王五3", "123", "高中"));
}
/**
* 验证用户登录
* @param username 用户名
* @param password 密码
* @return 验证成功返回用户对象失败返回null
*/
public User authenticate(String username, String password) {
User user = users.get(username);
if (user != null && user.getPassword().equals(password)) {
return user;
}
return null;
}
/**
* 检查用户类型是否有效
* @param userType 用户类型
* @return 是否有效
*/
public boolean isValidUserType(String userType) {
return "小学".equals(userType) || "初中".equals(userType) || "高中".equals(userType);
}
}