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.
58 lines
1.8 KiB
58 lines
1.8 KiB
import java.io.*;
|
|
import java.util.*;
|
|
import java.util.regex.*;
|
|
|
|
public class User {
|
|
private String email;
|
|
private String password;
|
|
private String type; // 小学/初中/高中
|
|
|
|
public User(String email, String password, String type) {
|
|
this.email = email;
|
|
this.password = password;
|
|
this.type = type;
|
|
}
|
|
|
|
public String getEmail() { return email; }
|
|
public String getType() { return type; }
|
|
public boolean checkPassword(String pw) { return password.equals(pw); }
|
|
public void setPassword(String pw) { password = pw; }
|
|
|
|
// 文件保存
|
|
public static void saveUsers(List<User> users, String filename) throws IOException {
|
|
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
|
|
out.writeObject(users);
|
|
out.close();
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public static List<User> loadUsers(String filename) {
|
|
try {
|
|
ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
|
|
List<User> users = (List<User>) in.readObject();
|
|
in.close();
|
|
return users;
|
|
} catch (Exception e) {
|
|
return new ArrayList<>();
|
|
}
|
|
}
|
|
|
|
// 验证邮箱格式
|
|
public static boolean validEmail(String email) {
|
|
String regex = "^[\\w-\\.]+@[\\w-]+(\\.[\\w-]+)+$";
|
|
return Pattern.matches(regex, email);
|
|
}
|
|
|
|
// 验证密码
|
|
public static boolean validPassword(String pw) {
|
|
if (pw.length() < 6 || pw.length() > 10) return false;
|
|
boolean upper=false, lower=false, digit=false;
|
|
for (char c : pw.toCharArray()) {
|
|
if (Character.isUpperCase(c)) upper=true;
|
|
if (Character.isLowerCase(c)) lower=true;
|
|
if (Character.isDigit(c)) digit=true;
|
|
}
|
|
return upper && lower && digit;
|
|
}
|
|
}
|