package Student;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class studentManageTest {
public static void main(String[] args) {
/*
为学生管理系统书写一个登陆、注册、忘记密码的功能。
只有用户登录成功之后,才能进入到学生管理系统中进行增删改查操作。*/
ArrayList<User> list_1 = new ArrayList<>();
Scanner sc = new Scanner(System.in);
loop:
while (true) {
System.out.println("-----------欢迎来到皮皮虾系统注册界面-----------");
System.out.println("1.登录");
System.out.println("2.注册");
System.out.println("3.忘记密码");
System.out.println("4.退出");
System.out.println("5.查询用户");//程序员要求加上的
System.out.println("请选择操作:");
String input = sc.nextLine();
switch (input) {
case "登录" -> {
if(login(list_1)){
studentManage();
break loop;
}else {
System.out.println("登录失败");
}
} // 登录方法
case "注册" -> register(list_1);
case "忘记密码" -> forgetPassWord(list_1);
case "查询用户" -> queryDataBase(list_1);
case "退出" -> {
break loop;
}
default -> System.out.println("无对应操作,请重试");
}
}
}//main方法 , 主入口
public static void forgetPassWord(ArrayList<User> list){
//忘记密码方法
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String name = sc.nextLine();
int index = Index(name, list);
if(index >= 0){
System.out.println("请输入身份证号");
String idNumber = sc.nextLine();
System.out.println("请输入手机号码");
String phoneNumber = sc.nextLine();
if(list.get(index).getIdNumber().equals(idNumber) && list.get(index).getPhoneNumber().equals(phoneNumber)){
System.out.println("输入新密码");
list.get(index).setPassword(getPassword());
System.out.println("更改密码成功");
}else {
System.out.println("抱歉,身份证和手机号不一致");
}
}else {
System.out.println("抱歉,您未注册");
}
}//忘记密码方法
public static boolean login(ArrayList<User> list) {
//登录总方法
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入用户名");
String userName = sc.nextLine();
int index = Index(userName, list);
if (index >= 0) {
String authCode = authCode();
System.out.println(authCode);
System.out.println("请输入验证码");
String inputAuthCode = sc.nextLine();
if (inputAuthCode.equals(authCode)) {
for (int i = 3; i > 0; ) {
System.out.println("请输入密码");
String password = sc.nextLine();
if (list.get(index).getPassword().equals(password)) {
System.out.println("登录成功");
return true;
}else {
i--;
if(i == 0){
System.out.println("失败次数过多, 账户" + userName + "已锁定");
return false;
}else {
System.out.println("抱歉,密码错误,你还剩下" + i + "次机会");
}
}
}
} else {
System.out.println("验证码输入错误");
}
} else {
System.out.println("抱歉,您未注册");
return false;
}
}
}//登录总方法
public static String authCode() {
ArrayList<Character> list = new ArrayList<>();
Random r = new Random();
list_1(list);
StringBuilder authCode = new StringBuilder();
for (int i = 0; i < 4; i++) {
int random = r.nextInt(list.size());
authCode.append(list.get(random));
}
int number = r.nextInt(10);
char[] arr = authCode.append(number).toString().toCharArray();
int random = r.nextInt(arr.length);
char temp = arr[random];
arr[random] = arr[arr.length - 1];
arr[arr.length - 1] = temp;
return new String(arr);
}//验证码
public static void list_1(ArrayList<Character> list) {
for (int i = 0 ; i < 26 ; i++) {
list.add((char)('a' + i));
list.add((char)('A' + i));
}
}//录入'a' - 'z'和'A' - 'Z'
public static int Index(String name, ArrayList<User> list) {
for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
if (u.getUserName().equals(name)) {
return i;
}
}
return -1;
}//判断用户名是否存在
public static void register(ArrayList<User> list) {
Scanner sc = new Scanner(System.in);
//注册方法(register)注册
User u = new User();
u.setUserName(getUserName(list));
u.setPassword(getPassword());
u.setIdNumber(getIdNumber());
u.setPhoneNumber(getPhoneNumber());
String authCode = authCode();
System.out.println(authCode);
System.out.println("请输入验证码确认你不是机器人");
String inputAuthCode = sc.nextLine();
if(inputAuthCode.equals(authCode)) {
System.out.println("注册成功!");
list.add(u);
}else {
System.out.println("验证码输入错误,返回界面");
}
}//注册总方法
public static String getUserName(ArrayList<User> list) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请录入用户名");
String userName = sc.nextLine();
int len = userName.length();
if (len >= 3 && len <= 15) {
//不要写成userName.length() >= 3,因为调用两次方法
if (determineInput(userName)) {
//用户名字母+数字的判断
//浪费资源的操作要放在最后面
if (userNameUnique(list, userName)) {
//用户名唯一判断
System.out.println("用户名:" + userName + "可用" );
return userName;
} else {
System.out.println("用户名重复,请重新输入");
}
} else {
System.out.println("用户名输入不规范,应是只能是字母加数字的组合,但是不能是纯数字,也不能是纯英文");
}
} else {
System.out.println("字符串长度错误(长度需要大于等于3,小于等于15");
}
}
}//获取用户名
public static boolean userNameUnique(ArrayList<User> list, String userName) {
//用户名唯一
for (int i = 0; i < list.size(); i++) {
String name = list.get(i).getUserName();
if (name.equals(userName)) {
return false;
}
}
return true;
}//用户名唯一
public static boolean determineInput(String userName) {
//判断除了大写小写字母以及数字后是否还有其他
for (int i = 0; i < userName.length(); i++) {
char c = userName.charAt(i);
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '