新增需求
这是在昨天的基础初代版本上面新增一个登录注册忘记密码的功能
需求分析
注册
登录
忘记密码
user类代码呈现
package StudentSystem;public class User {private String username;private String password;private String personID;private String phoneNumber;public User() {}public User(String username, String password, String personID, String phoneNumber) {this.username = username;this.password = password;this.personID = personID;this.phoneNumber = phoneNumber;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getPersonID() {return personID;}public void setPersonID(String personID) {this.personID = personID;}public String getPhoneNumber() {return phoneNumber;}public void setPhoneNumber(String phoneNumber) {this.phoneNumber = phoneNumber;}
}
登陆注册页面代码呈现:
package StudentSystem;import com.sun.jdi.DoubleType;import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;public class App {public static void main(String[] args) {ArrayList<User> list = new ArrayList<>();Scanner sc = new Scanner(System.in);while (true) {System.out.println("欢迎来到学生管理系统");System.out.println("请选择: 1. 登录 2.注册 3.忘记密码 4.退出系统");String choose = sc.nextLine();switch (choose) {case "1" ->login(list);case "2" ->register(list);case "3" ->forgetPassword(list);case "4" ->{System.out.println("谢谢使用,再见");System.exit(0);}default -> System.out.println("没有这个选项");}}}//登录private static void login(ArrayList<User> list) {Scanner sc = new Scanner(System.in);for (int i = 0; i < 3; i++) {System.out.println("请输入用户名");String username = sc.next();//判断用户名是否存在boolean flag = contains(list,username);if (!flag) {System.out.println("用户名未注册" + username + "请先注册再登录");return;}System.out.println("请输入密码");String password = sc.next();while (true) {String rightCode = getCode();System.out.println("当前正确验证码为:" + rightCode);System.out.println("请输入验证码");String code = sc.next();if (code.equalsIgnoreCase(rightCode)){System.out.println("验证码正确");break;}else {System.out.println("验证码错误请重新输入");continue;}}//用户名和密码是否正确(集合中是否包含用户名和密码)//定义一个方法验证用户名和密码是否正确//封装思想的理由,可以把一些零散的数据封装成一个对象User userInfo = new User(username,password,null,null);boolean result = checkUserInfo(list,userInfo);if (result) {System.out.println("账号密码输入正确,开始使用学生管理系统");//创建对象启动学生管理系统StudentSystem ss = new StudentSystem();ss.starStudentSystem();break;}else {System.out.println("登录失败,用户名或密码错误");if ( i == 2){System.out.println("当前账号"+username+"已被锁定请联系客服处理");return;}else {System.out.println("用户名或密码错误,还剩下"+(2-i)+"次机会");}}}}private static boolean checkUserInfo(ArrayList<User> list,User userInfo) {for (int i = 0; i < list.size(); i++) {User user = list.get(i);if (user.getUsername().equals(userInfo.getUsername()) && user.getPassword().equals(userInfo.getPassword())) {return true;}}return false;}//注册private static void register(ArrayList<User> list) {//将用户信息添加到集合中//将用户的用户名 密码 身份证号 手机号放入用户对象中//将用户的对象加入到集合中Scanner sc = new Scanner(System.in);String username;String Password;String personID;String phoneNumber;//1.键盘录入用户名while (true) {System.out.println("请输入用户名");username = sc.next();//先验证格式是否正确再验证是否唯一,因为数据都存储在数据库中如果要校验,需要用到网络资源//用户名唯一boolean flag1 = checkUsername(username);if (!flag1) {System.out.println("用户名不满足条件需要重新输入");continue;}//校验用户名是否唯一//username到用户名中判断是否存在boolean flag2 = contains(list,username);if (flag2) {//用户名已存在,那么当前用户名已存在要重新输入System.out.println("用户名已存在,请重新输入");}else {//不存在,当前用户名可用,可以录入其他数据System.out.println("用户名"+username+"可用");break;}}//2.键盘录入密码//要输入两次,两次一样才可以进行注册while (true) {System.out.println("请输入密码");Password = sc.next();System.out.println("请再次输入密码");String againPassword = sc.next();if (!Password.equals(againPassword)) {System.out.println("两次输入密码不一致,请重新输入");continue;}else {System.out.println("两次密码输入一致,请录入其他数据");break;}}//3.键盘录入身份证号码while (true) {System.out.println("请输入身份证号码");personID = sc.next();boolean flag = checkpersonID(personID);if (flag){System.out.println("身份证号码输入正确");break;}else {System.out.println("身份证号码格式输入有误,请重新输入");continue;}}//4.键盘录入手机号码while (true) {System.out.println("请输入手机号码");phoneNumber = sc.next();boolean flag = checkphoneNumber(phoneNumber);if (flag){System.out.println("手机号码格式正确");break;}else {System.out.println("手机号码格式不正确请重新输入");continue;}}//将用户名密码身份证号码手机号码放入用户对象中User u = new User(username,Password,personID,phoneNumber);//将用户对象添加到集合中list.add(u);System.out.println("注册成功");//遍历集合printList(list);}private static void printList(ArrayList<User> list) {for (int i = 0; i < list.size(); i++) {//i表示索引User user = list.get(i);System.out.println(user.getUsername()+" "+user.getPassword()+" "+user.getPersonID()+" "+user.getPhoneNumber());}}private static boolean checkphoneNumber(String phoneNumber) {//长度11位if (phoneNumber.length() != 11){return false;}//不能以0开头boolean flag = phoneNumber.startsWith("0");if (flag){return false;}//必须都是数字for (int i = 0; i < phoneNumber.length(); i++) {char c = phoneNumber.charAt(i);if (!(c >= '0' && c <= '9')){return false;}}//循环结束之后每一个字符都在0-9之间return true;}private static boolean checkpersonID(String personID) {//长度为18if (personID.length() != 18) {return false;}//不能以0开头(0索引不能为0)//char c = personID.charAt(0);boolean flag = personID.startsWith("0");if (flag){return false;}//前17位必须为数字for (int i = 0; i <= personID.length() - 1; i++) {char c = personID.charAt(i);if (!(c >= '0' && c <= '9')) {return false;}}//最后一位可以是数字,也可以是大写x或者小写xchar endChar = personID.charAt(personID.length() - 1);if ((endChar >= '0' && endChar <= '9')||(endChar == 'X')||(endChar == 'x')) {return true;}else {return false;}}private static boolean contains(ArrayList<User> list, String username) {//循环遍历得到每一个对象//username到集合中判断是否存在for (int i = 0; i < list.size(); i++) {//i表示索引User user = list.get(i);String rightUsername = user.getUsername();if (rightUsername.equals(username)){return true;}}//当循环结束集合里面所有的用户都比较完毕,还没有一样的则返回falsereturn false;}private static boolean checkUsername(String username) {//用户名长度3-15位int len = username.length();if (len < 3 || len > 15) {return false;}//当代码执行到这边表示代码长度符合要求//继续校验 只能字母+数字//循环得到username里面的每一个字符for (int i = 0; i < username.length(); i++) {//i索引char c = username.charAt(i);if (!((c>='a' && c<='z')||(c>='A'&& c<='Z')||(c>='0'&& c<='9'))){return false;}}//当代码运行至此 代码满足长度和内容需求//不能是纯数字(统计用户名中有多少字母)int count = 0;for (int i = 0; i < username.length(); i++) {//i索引char c = username.charAt(i);if ((c>='a' && c<='z')||(c>='A'&& c<='Z')){count++;break;}return count > 0;}return true;}//生成一个验证码private static String getCode(){//1.创建一个集合添加所有的大写小写字母ArrayList<Character> list = new ArrayList<>();for (int i = 0; i < 26; i++) {list.add((char)('a'+i));list.add((char)('A'+i));}StringBuilder sb = new StringBuilder();//2.随机抽取四个字符Random r = new Random();for (int i = 0; i < 4; i++) {//获取随机索引int index = r.nextInt(list.size());//利用随机的索引获取字符char c = list.get(index);//将随机字符添加到sb中sb.append(c);}//把一个随机数字添加到末尾int number = r.nextInt(10);sb.append(number);//如果要修改字符串的内容需要遍历得到每一个字符数组,再数组中修改,在创建一个新的字符串char[] arr = sb.toString().toCharArray();//拿着最后一个索引跟随机索引进行交换int randomIndex = r.nextInt(arr.length);//最大索引指向的元素 跟随机索引指向的元素进行交换char temp = arr[randomIndex];//临时变量为随机索引arr[randomIndex] = arr[arr.length-1];//将最大索引赋值给随机索引arr[arr.length-1] = temp;//将临时变量赋值给最大索引return new String(arr);}//忘记密码private static void forgetPassword(ArrayList<User> list) {Scanner sc = new Scanner(System.in);System.out.println("请输入用户名");String username = sc.next();boolean flag = contains(list,username);if (!flag){System.out.println("当前用户"+ username+"未注册,请先注册");return;}//键盘录入身份证号码和手机号码System.out.println("请输入身份证号码");String personID = sc.next();System.out.println("请输入手机号码");String phoneNumber = sc.next();//比较身份证和手机 都正确则修改密码//需要将用户对象先获取出来Userint index = findIndex(list,username);User user = list.get(index);//比较用户对象中的手机号码和身份证号码if (!(user.getPersonID().equalsIgnoreCase(personID) && user.getPhoneNumber().equals(phoneNumber))){System.out.println("身份证号码或手机号码输入有误,不能修改密码");return;}//当代码执行到这边,表示输入的数据全部验证成功,直接修改即可String password;while (true) {System.out.println("请输入新的密码");password = sc.next();System.out.println("请再次输入新的密码");String againPassword = sc.next();if (password.equals(againPassword)){System.out.println("两次输入密码一致");break;}else {System.out.println("两次输入的密码不一致,请重新输入");continue;}}//直接修改即可user.setPassword(password);System.out.println("密码修改成功");}private static int findIndex(ArrayList<User> list, String username) {for (int i = 0; i < list.size(); i++) {User user = list.get(i);if (user.getUsername().equals(username)){return i;}}return -1;}
}