用户管理系统Java实现

package StudentSystem;

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

public class usersTest {
    //main函数
    public static void main(String[] args) {
        //存放信息的总容器
        ArrayList<Users> list = new ArrayList<>();
        loop:
        while (true) {
            Users u = new Users();
            System.out.println("-----------欢迎来到学生管理系统-----------");
            System.out.println("1:>注册");
            System.out.println("2:>登录");
            System.out.println("3:>忘记密码");
            System.out.println("4:>查阅数据库");
            System.out.println("0:>退出");
            Scanner sc = new Scanner(System.in);
            System.out.println("请选择,你要进行的功能");
            String choose = sc.next();
            switch (choose) {
                case "1" -> newName(list, u);
                case "2" -> login(list);
                case "3" -> slip(list);
                case "0" -> {
                    System.out.println("退出程序");
                    break loop;
                }
                case "4" -> {
                    for (int i = 0; i < list.size(); i++) {
                        Users us = list.get(i);
                        System.out.println(us.getId() + "," + us.getNumber() + "," + us.getPassword() + "," + us.getId());
                    }
                }
                default -> System.out.println("选择错误,请重新选择");
            }
        }


    }

    //注册用户名的方法主体
    public static void newName(ArrayList<Users> list, Users u) {
        //用户信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String name = sc.next();
        if (judgment(list, name)) {
            if (judgmentName(name)) {

                System.out.println("用户名合法,设置成功");
                u.setName(name);
                newPassword(list, u);//设置密码
                judgmentId(list, u);//存放id

            } else {
                System.out.println("用户名不合法,请重新输入");
            }
        } else {
            System.out.println("用户名重复,请重新输入");
        }


    }

    //判断用户名是否唯一
    public static boolean judgment(ArrayList<Users> list, String name) {
        for (int i = 0; i < list.size(); i++) {
            Users u = list.get(i);
            String name1 = u.getName();
            if (name1.equals(name)) {
                return false;
            }
        }
        return true;


    }

    //判断用户名是否符合规定
    public static boolean judgmentName(String name) {
        int count = 0;//用来计算字母的个数
        if (name.length() >= 3 && name.length() < +15) {

            for (int i = 0; i < name.length(); i++) {
                char c = name.charAt(i);
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {

                    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                        count++;
                    }


                } else {
                    return false;
                }

            }


        } else {
            return false;
        }


        if (count == 0) {
            return false;
        }
        return true;
    }

    //设置密码
    public static void newPassword(ArrayList<Users> list, Users u) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请设置密码");
        String password = sc.next();
        System.out.println("请确定密码");
        String password1 = sc.next();
        if (password1.equals(password)) {
            System.out.println("密码设置成功");
            u.setPassword(password);
        } else {
            System.out.println("两次密码不一样");
        }


    }

    //判断id是否合法
    public static void judgmentId(ArrayList<Users> list, Users u) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入身份证");
        String id = sc.next();
        if (id.length() == 18) {
            char c = id.charAt(0);
            if (c != '0') {
                for (int i = 0; i < id.length(); i++) {

                    char ch = id.charAt(i);
                    if (i < 17) {
                        if (ch >= '0' && ch <= '9') {

                        } else {
                            System.out.println("身份证格式错误,前十七位必须是数字");
                        }

                    } else {
                        if ((ch >= '0' && ch <= '9') || (ch == 'x') || (ch == 'X')) {
                            System.out.println("身份证格式正确,绑定成功");
                            u.setId(id);
                            judgmentNumber(list, u);//存放手机号

                        } else {
                            System.out.println("身份证格式错误");
                        }

                    }

                }
            } else {
                System.out.println("身份证格式不对,首字母不能为0");
            }


        } else {
            System.out.println("身份证长度不正确");
        }


    }

    //绑定手机号,并且判断手机号是否合法
    public static void judgmentNumber(ArrayList<Users> list, Users u) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入手机号");
        String number = sc.next();
        if (number.length() == 11) {
            char c = number.charAt(0);
            if (c != '0') {
                for (int i = 0; i < number.length(); i++) {
                    if (c >= '0' && c <= '9') {

                    } else {
                        System.out.println("手机号格式不正确,每一位只能为数字");

                    }

                }
                System.out.println("手机号格式正确");
                u.setNumber(number);
                list.add(u);
                System.out.println("恭喜你注册成功");


            } else {
                System.out.println("手机号格式不正确,第一位不能是0");

            }


        } else {
            System.out.println("手机号格式不正确,长度应该为11");
        }


    }

    //登录功能的方法主体
    public static void login(ArrayList<Users> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String name = sc.next();
        int index = query(list, name);
        if (index >= 0) {
            System.out.println("用户名存在,请输入密码");
            String password = sc.next();
            String Upassword = list.get(index).getPassword();
            if (password.equals(Upassword)) {
                while (true) {
                    String random = random();
                    System.out.println("识别码为:" + random);
                    System.out.println("请输入识别码");
                    String str = sc.next();
                    if (str.equals(random)) {
                        System.out.println("恭喜你登录成功");
                        break;
                    } else {
                        System.out.println("验证码错误,请重新输入");
                    }

                }


            } else {
                System.out.println("密码不正确");
            }
        } else {
            System.out.println("该用户名尚未注册");
        }

    }

    //在数据库里面查询用户名
    public static int query(ArrayList<Users> list, String name) {

        for (int i = 0; i < list.size(); i++) {
            Users us = list.get(i);
            String Usname = us.getName();
            if (Usname.equals(name)) {
                return i;
            }

        }


        return -1;
    }

    //生成识别码码
    public static String random() {
        StringBuilder sb = new StringBuilder();
        char[] c = new char[62];
        //生成一个含有大小写字母和数字的数组
        for (int i = 0; i < c.length; i++) {
            if (i < 26) {
                c[i] = (char) ('a' + i);
            } else if (i >= 26 && i < 52) {
                int b = 1;
                c[i] = (char) ('A' + b);
                b++;
            } else {
                int a = 1;
                c[i] = (char) ('0' + a);
                a++;
            }
        }

        for (int i = 0; i < 5; i++) {
            Random r = new Random();
            int index = r.nextInt(c.length);
            sb.append(c[index]);
        }
        String str = sb.toString();
        return str;
    }

    //忘记密码的函数主体
    public static void slip(ArrayList<Users> list) {
        System.out.println("请输入用户名");
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        int index = query(list, name);
        if (index >= 0) {
            System.out.println("请输入身份证");
            String id = sc.next();
            String Uid = list.get(index).getId();
            if(id.equals(Uid)){
                System.out.println("请输入手机号");
                String number= sc.next();
                String Unumber = list.get(index).getNumber();
                if(number.equals(Unumber)){
                    System.out.println("请输入新的密码");
                    String newPassword = sc.next();
                    list.get(index).setPassword(newPassword);
                    System.out.println("修改成功");
                }else{
                    System.out.println("信息不匹配,修改失败");

                }

            }else{
                System.out.println("信息不匹配,修改失败");
            }

        } else {
            System.out.println("未注册,修改失败");
        }


    }


}
  • 18
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值