java学生管理系统升级版

学生管理系统升级版

需求:

​ 为学生管理系统书写一个登陆、注册、忘记密码的功能。

​ 只有用户登录成功之后,才能进入到学生管理系统中进行增删改查操作。

分析:

登录界面:

System.out.println("欢迎来到学生管理系统");
System.out.println("请选择操作1登录 2注册 3忘记密码");

用户类:

​ 属性:用户名、密码、身份证号码、手机号码

注册功能:

​ 1,用户名需要满足以下要求:

​ 验证要求:

​ 用户名唯一

​ 用户名长度必须在3~15位之间

​ 只能是字母加数字的组合,但是不能是纯数字

​ 2,密码键盘输入两次,两次一致才可以进行注册。

​ 3,身份证号码需要验证。

​ 验证要求:

​ 长度为18位

​ 不能以0为开头

​ 前17位,必须都是数字

​ 最为一位可以是数字,也可以是大写X或小写x

​ 4,手机号验证。

​ 验证要求:

​ 长度为11位

​ 不能以0为开头

​ 必须都是数字

登录功能:

​ 1,键盘录入用户名

​ 2,键盘录入密码

​ 3,键盘录入验证码

验证要求:

​ 用户名如果未注册,直接结束方法,并提示:用户名未注册,请先注册

​ 判断验证码是否正确,如不正确,重新输入

​ 再判断用户名和密码是否正确,有3次机会

忘记密码:

​ 1,键盘录入用户名,判断当前用户名是否存在,如不存在,直接结束方法,并提示:未注册

​ 2,键盘录入身份证号码和手机号码

​ 3,判断当前用户的身份证号码和手机号码是否一致,

​ 如果一致,则提示输入密码,进行修改。

​ 如果不一致,则提示:账号信息不匹配,修改失败。

验证码规则:

​ 长度为5

​ 由4位大写或者小写字母和1位数字组成,同一个字母可重复

​ 数字可以出现在任意位置

比如:

​ aQa1K

为上述的需求分析画个草图便于自己分析

 思路:创建User类和UserLogin类。

 

在User类中定义用户的属性:用户名、密码、身份证号码、手机号码

 在UserLogin中定义各种方法

 

User完整代码如下:

package StudentSystem;

public class User {
    //1.创建用户类属性
    private String userId;
    private String password;
    private String idCard;
    private String phoneNumber;

    //2.创建空参构造和带全部参数的构造
    public User() {
    }

    public User(String userId, String password, String idCard, String phoneNumber) {
        this.userId = userId;
        this.password = password;
        this.idCard = idCard;
        this.phoneNumber = phoneNumber;
    }

    /**
     * 获取
     * @return userId
     */
    public String getUserId() {
        return userId;
    }

    /**
     * 设置
     * @param userId
     */
    public void setUserId(String userId) {
        this.userId = userId;
    }

    /**
     * 获取
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * 设置
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 获取
     * @return idCard
     */
    public String getIdCard() {
        return idCard;
    }

    /**
     * 设置
     * @param idCard
     */
    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    /**
     * 获取
     * @return phoneNumber
     */
    public String getPhoneNumber() {
        return phoneNumber;
    }

    /**
     * 设置
     * @param phoneNumber
     */
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}

UserLogin完整代码如下

package StudentSystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class UserLogin {
    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 choice = sc.next();
            switch (choice) {
                case "1":userLogin(list);break;
                case "2":userRegister(list);break;
                case "3":forgetPassword(list);break;
                case "4":{
                    System.out.println("退出系统");
                    System.exit(0);
                }
            }
        }
    }

    //1.用户注册
    public static void userRegister(ArrayList<User> list)  {
        //键盘录入用户名
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入用户名(用户名长度3-15且只能由数字和字母组合,不能是纯数字:)");
        String userName = sc.next();

        //1.先判断用户名的格式是否符合要求,不符合则重新录入(先判断格式的目的是减轻服务器压力)
        //判断用户名长度
        int userNameLength = userName.length();
        if (userNameLength < 3 || userNameLength > 15){
            System.out.println("用户名长度超出范围,请重新输入用户名");
        } else {

            //判断用户名是不是字母和数字的组合
            boolean userNameFlag = checkUserName(userName);
            if (userNameFlag){

                    //代码执行到这里代表范围在数字和字母区间内
                    //判断用户名是否是纯数字
                    int countLetter = countLetter(userName);
                    if (countLetter > 0){

                        //用户名符合规范
                        //2.查询用户名是否已存在,已存在则报错
                        int index = getUserIndex(list,userName);
                        if (index != -1){

                            //如果用户已存在则报错
                            System.out.println("当前用户已存在,请重新输入您要注册的用户名");
                        } else {

                            //用户名符合要求,输入密码
                            System.out.print("请输入用户密码:");
                            String password1 = sc.next();
                            System.out.print("请再次输入用户密码:");
                            String password2 = sc.next();
                            if (password1.equals(password2)){

                                //用户名和密码符合要求,可以开始录入身份证号码
                                System.out.print("请输入身份证号码:");
                                String idCard = sc.next();

                                //判断身份证号码长度是否符合要求
                                if (idCard.length() == 18){
                                    //判断身份证首位是否是0
                                    char cardStart = idCard.charAt(0);
                                    if (cardStart == '0'){
                                        System.out.println("身份证首位不能为0");
                                    } else {

                                        //身份证长度和首位都符合要求
                                        //判断身份证前17位是否都是数字
                                        boolean flag = countIdCard(idCard);
                                        if (flag){
                                            //判断身份证末位是否是X x 或0-9
                                            char idCardLast = idCard.charAt(17);
                                            if ((idCardLast >= '0' && idCardLast <= '9') || idCardLast == 'X' || idCardLast == 'x'){

                                                //身份证符合要求,输入手机号
                                                System.out.print("请输入您的手机号:");
                                                String phoneNumber = sc.next();
                                                //判断手机号长度
                                                if (phoneNumber.length() == 11){
                                                    char phoneNumberStart = phoneNumber.charAt(0);
                                                    if (phoneNumberStart == '0'){
                                                        System.out.println("手机号首位不为0");
                                                    }else {

                                                        //判断手机号是否都为数字
                                                        int countPhoneNumber = countNumber(phoneNumber);
                                                        if (countPhoneNumber == 11){

                                                            //用户名 密码 身份证 手机号均符合要求,将其存入用户对象
                                                            User user = new User();
                                                            user.setUserId(userName);
                                                            user.setPassword(password2);
                                                            user.setIdCard(idCard);
                                                            user.setPhoneNumber(phoneNumber);

                                                            //将用户对象存入集合
                                                            list.add(user);
                                                        }else {
                                                            System.out.println("手机号必须为11位数字");
                                                        }
                                                    }
                                                }else {
                                                    System.out.println("手机号长度必须为11位");
                                                }
                                            }else {
                                                System.out.println("身份证末位必须是X或x或介于0-9之间的数字");
                                            }
                                        }else {
                                            System.out.println("身份证前17位必须为数字");
                                        }
                                    }
                                }else {
                                    System.out.println("身份证号码长度输入错误");
                                }
                            }else {
                                System.out.println("您两次输入的密码不相同");
                            }
                        }
                    } else {
                        System.out.println("用户名不能是纯数字");
                    }
                } else {
                    System.out.println("用户名只能是数字加字母的组合,不能出现其它字符");
                }
            }
        }

    //2.用户登录
    public static void userLogin(ArrayList<User> list) {
        int loginCount = 3;
        while (loginCount > 0) {
            //1.输入用户名和密码
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入您的用户名:");
            String userName = sc.next();
            System.out.print("请输入您的密码:");
            String password = sc.next();

            //2.生成验证码并输入验证码
            StringBuilder verificationCode1 = getVerificationCode();
            System.out.println("验证码为:" + verificationCode1);
            String verificationCode2 = verificationCode1.toString();
            System.out.print("请输入验证码:");
            String verificationCode3 = sc.next();

            //这里对比验证码不区分大小写
            if (verificationCode3.equalsIgnoreCase(verificationCode2)){

                //输入的验证码和给出的验证码一致
                //3.判断用户名长度是否符合要求
                if (userName.length() >=3 && userName.length() <= 15){

                    //4.判断用户名是否是数字和字母的组合
                    boolean userNameFlag = checkUserName(userName);
                    if (userNameFlag){

                        //5.判断用户名是否是纯数字
                        int count = countLetter(userName);
                        if (count == userName.length()){
                            System.out.println("用户名输入错误");
                            loginCount--;
                        } else {

                            //6.查询用户名是否存在
                            int index = getUserIndex(list,userName);
                            if (index != -1) {
                                //根据用户名查询密码
                                String userPassword = list.get(index).getPassword();
                                if (password.equals(userPassword)){

                                    //用户名和密码都正确
                                    System.out.println("恭喜您,登陆成功,欢迎来到学生管理系统");
                                }else {
                                    System.out.println("密码输入错误,请重新尝试");
                                    loginCount--;
                                }
                            }else {
                                System.out.println("用户名不存在,请先注册");
                                loginCount--;
                            }
                        }
                    }else {
                        System.out.println("用户名输入错误");
                        loginCount--;
                    }
                } else {
                    System.out.println("用户输入错误");
                    loginCount--;
                }
            }else {
                System.out.println("验证码输入错误,请重新输入");
            }
        }
        if (loginCount == 0){
            System.out.println("您的用户名或密码已输入错误3次,请联系客服解封,电话:xxx-xx");
        }
    }

    //3.忘记密码
    public static void forgetPassword(ArrayList<User> list){
        //键盘录入用户名
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入你的用户名:");
        String userName = sc.next();

        //判断用户名是否合法
        //用户名长度是否介于3-15之间(闭区间)
        if (userName.length() >= 3 && userName.length() <= 15){
            int count = countLetter(userName);

            //判断用户名是否有特殊字符
            boolean flag = checkUserName(userName);
            if (flag){

                //用户名是由字母和数字组成
                //判断用户名是不是纯数字
                if (count > 0){

                    //用户名不是纯数字
                    System.out.print("请输入身份证号码:");
                    String inputIdCard = sc.next();
                    System.out.print("请输入手机号码:");
                    String inputPhoneNumber = sc.next();
                    //判断身份证格式是否正确
                    if (inputIdCard.length() == 18){
                        //判断身份证第一位是不是0
                        char inputIdCardStart = inputIdCard.charAt(0);
                        if (inputIdCardStart == '0'){
                            System.out.println("账号信息不匹配,修改失败");
                        }else {
                            boolean inputIdCardFlag = countIdCard(inputIdCard);
                            if (inputIdCardFlag){
                                char inputIdCardEnd = inputIdCard.charAt(17);
                                if ((inputIdCardEnd == 'X' )|| (inputIdCardEnd == 'x') || (inputIdCardEnd >= '0' && inputIdCardEnd <= '9')){
                                    //身份证格式符合要求
                                    //判断手机号格式是否正确
                                    if (inputPhoneNumber.length() == 11){
                                        //判断首位是不是0,是0则报错
                                        char inputPhoneNumberStart = inputPhoneNumber.charAt(0);
                                        if (inputPhoneNumberStart == '0'){
                                            System.out.println("账号信息不匹配,修改失败");
                                        }else {
                                            //判断手机号是否都为数字
                                            int countPhoneNumber = countNumber(inputPhoneNumber);
                                            if (countPhoneNumber == 11){
                                                //用户名、身份证、手机号格式均匹配
                                                //根据用户名获取user在集合中的索引
                                                int index = getUserIndex(list,userName);

                                                //根据索引获取身份证号码和手机号码做对比
                                                if (inputIdCard.equals(list.get(index).getIdCard())){
                                                    if (inputPhoneNumber.equals(list.get(index).getPhoneNumber())){
                                                        //用户名,身份证,手机号均正确,输入要修改的密码
                                                        System.out.print("请输入您要修改的密码:");
                                                        String password1 = sc.next();
                                                        System.out.print("请再次输入您要修改的密码:");
                                                        String password2 = sc.next();
                                                        //判断两次输入密码是否相同
                                                        if (password1.equals(password2)){
                                                            list.get(index).setPassword(password2);
                                                            System.out.println("恭喜您,密码修改成功!");
                                                        }else {
                                                            System.out.println("两次输入的密码不匹配");
                                                        }
                                                    }else {
                                                        System.out.println("账号信息不匹配,修改失败");
                                                    }
                                                }else {
                                                    System.out.println("账号信息不匹配,修改失败");
                                                }
                                            }else {
                                                System.out.println("账号信息不匹配,修改失败");
                                            }
                                        }
                                    }else {
                                        System.out.println("账号信息不匹配,修改失败");
                                    }
                                }else {
                                    System.out.println("账号信息不匹配,修改失败");
                                }
                            }
                            else {
                                System.out.println("账号信息不匹配,修改失败");
                            }
                        }
                    }else {
                        System.out.println("账号信息不匹配,修改失败");
                    }
                }else {
                    System.out.println("用户名输入错误");
                }
            } else {
                System.out.println("用户名输入错误");
            }
        }else {
            System.out.println("用户名输入错误");
        }
    }

    //判断用户名是否是数字和字母的组合
    public static boolean checkUserName(String userName) {
        int count = 0;
        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 <= '9')) {
                count++;
            }
        }
        if (count == userName.length()){
            return true;
        }else {
            return false;
        }
    }

    //获取验证码
    public static StringBuilder getVerificationCode() {
        //1.创建一个长度为52的字符型数组,将26个大小写字母存入其中
        char[] chs = new char[52];
        for (int i = 0; i < 52; i++) {
            if (i < 26){
                chs[i] = (char)(97 + i);
            }else {
                chs[i] = (char) (65 + i - 26);
            }
        }

        //2.循环五次创建随机索引,从52个大小写字母中抽出5个字母
        Random r = new Random();
        char[] newChs = new char[5];
        for (int i = 0; i < 5; i++) {
            int index = r.nextInt(52);
            newChs[i] = chs[index];
        }

      //3.在0-9之间随机生成一个数字,将其转换成字符型
        int number = r.nextInt(9) + 1;
        char numberToChar = (char) (number + 48);
        //在0-4之间随机生成一个索引,将字符型数字随机插入索引中
        int index = r.nextInt(5);
        newChs[index] = numberToChar;
        //遍历数组
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < newChs.length; i++) {
            sb.append(newChs[i]);
        }
        return sb;
    }

    //在集合中查找是否存在该用户名,存在返回索引号i,不存在则返回-1
    public static int getUserIndex(ArrayList<User> list,String userId) {
        for (int i = 0; i < list.size(); i++) {
            if (userId.equals(list.get(i).getUserId())){
                return i;
            }
        }
        return -1;
    }

    //创建方法统计字母的数量
    public static int countLetter(String userName) {
        int count = 0;
        for (int i = 0; i < userName.length(); i++) {
            char c = userName.charAt(i);
            if ((c >= 'a' && c <= 'z') ||(c >= 'A' && c <= 'Z')){
                count++;
            }
        }
        return count;
    }

    //判断身份证前17位是否都是数字
    public static boolean countIdCard(String idCard) {
        //截取前17位
        String newIdCard = idCard.substring(0,17);
        int count  = countNumber(newIdCard);
        if (count == 17){
            return true;
        } else {
            return false;
        }
    }

    //统计字符串中数字的数量
    public static int countNumber(String number) {
        int count = 0;
        for (int i = 0; i < number.length(); i++) {
            char c = number.charAt(i);
            if (c >= '0' && c <= '9'){
                count++;
            }
        }
        return count;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值