记录第一次java小项目---黑马项目---学生管理系统

学生管理系统升级版

需求:

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

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

分析:

登录界面:
System.out.println("欢迎来到学生管理系统");
System.out.println("请选择操作1登录 2注册 3忘记密码 4.退出");
用户类:

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

注册功能:

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

​ 验证要求:

​ 用户名唯一

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

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

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

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

​ 验证要求:

​ 长度为18位

​ 不能以0为开头

​ 前17位,必须都是数字

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

​ 4,手机号验证。

​ 验证要求:

​ 长度为11位

​ 不能以0为开头

​ 必须都是数字

登录功能:

​ 1,键盘录入用户名

​ 2,键盘录入密码

​ 3,键盘录入验证码

验证要求:

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

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

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

忘记密码:

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

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

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

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

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

验证码规则:

​ 长度为5

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

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

实现完整代码

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

public class UserTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<User> list = new ArrayList<>();
        while (true) {
            System.out.println("----------欢迎来到扶苏的学生管理系统----------");
            System.out.println("请按提示选择操作:  1.登录    2.注册    3.忘记密码   4.退出");
            String number = sc.next();
            switch (number) {
                case "1":
                    login(list);
                    break;
                case "2":
                    registration(list);
                    break;
                case "3":
                    modification(list);
                    break;
                case "4":
                    System.exit(0);
                default:
                    System.out.println("没有这个选项");
                    break;
            }
        }
    }

    //登录
    public static void login(ArrayList<User> list) {
        int count = 3;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名");
            String userName = sc.next();
            System.out.println("请输入密码");
            String password = sc.next();
            String realVerfication = getVerficationCode();
            System.out.println("验证码:" + realVerfication);
            System.out.println("请输入验证码");
            String verfcation = sc.next();
            int index = getIndex(list, userName);
            //判断用户名是否存在,大于0说明存在
            if (index >= 0) {
                //判断密码和验证码是否都相等
                if ((list.get(index).getPassword().equals(password)) && (verfcation.equalsIgnoreCase(realVerfication))) {
                    //密码和验证码都相等
                    break;
                } else {
                    System.out.println("密码和验证码出现错误");
                }
            } else {
                System.out.println("用户名未注册");
                count--;
            }
        }
        if (count == 0) {
            System.out.println("三次机会已用完,请联系管理员");
        } else {
            System.out.println("登陆成功");
        }
    }

    //注册
    public static void registration(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        //1.用户名验证
        System.out.println("请输入用户名");
        String userName = sc.next();
        boolean flag = contains(list, userName);
        if (flag) {
            System.out.println("用户名已存在,请重新输入");
        } else {
            if (userName.length() >= 3 && userName.length() <= 15) {
                boolean flag2 = letterOrNumber(userName);
                if (flag2) {
                    int countLetter = getLetter(userName);
                    if (countLetter > 0) {
                        //用户名符合规范
                        //2.密码验证
                        System.out.println("请输入注册密码");
                        String password = sc.next();
                        System.out.println("请再次输入密码");
                        String rePassword = sc.next();
                        if (password.equals(rePassword)) {
                            //3.身份证验证
                            System.out.println("请输入身份证号码");
                            String personId = sc.next();
                            int IdLength = personId.length();
                            if (IdLength == 18) {
                                //长度是否为18
                                if ((personId.charAt(0)) != '0') {
                                    //首位不为0
                                    int count = 0;
                                    for (int i = 0; i < personId.length() - 1; i++) {
                                        //判断前17位全都是数字
                                        char c = personId.charAt(i);
                                        if (c >= '0' && c <= '9') {
                                            count++;
                                        }
                                    }
                                    //如果count等于17,说明全都是数字
                                    if (count == 17) {
                                        //末位为数字或者X或者x
                                        char lastChar = personId.charAt(personId.length() - 1);
                                        if ((lastChar >= '0' && lastChar <= '9') || lastChar == 'X' || lastChar == 'x') {
                                            //手机号验证条件满足
                                            //4.手机号验证
                                            System.out.println("请输入手机号码");
                                            String phoneNumber = sc.next();
                                            //验证手机号长度
                                            if (phoneNumber.length() == 11) {
                                                //不能以0起头
                                                if (phoneNumber.charAt(0) != '0') {
                                                    for (int i = 0; i < phoneNumber.length(); i++) {
                                                        //得到字符在0~9的数量
                                                        int num = getNumber(phoneNumber);
                                                        if (num == 11) {
                                                            User user = new User(userName, password, personId, phoneNumber);
                                                            list.add(user);
                                                        } else {
                                                            System.out.println("手机号存在非法字符,注册失败");
                                                        }
                                                    }
                                                    System.out.println("注册成功");
                                                } else {
                                                    System.out.println("手机号首位不能为0");
                                                }

                                            } else {
                                                System.out.println("手机号长度不正确");
                                            }
                                        } else {
                                            System.out.println("身份证末位不合理");
                                        }
                                    } else {
                                        System.out.println("身份证存在不合理的字符,应全是数字");
                                    }
                                } else {
                                    System.out.println("身份证首位不能为0");
                                }
                            } else {
                                System.out.println("身份证长度不正确");
                            }
                        } else {
                            System.out.println("密码不一致");
                        }
                    } else {
                        System.out.println("用户名应该包含至少一个字母");
                    }
                } else {
                    System.out.println("存在不规范的字符");
                }
            } else {
                System.out.println("用户名长度不符合规范");
            }
        }

    }

    //忘记密码
    public static void modification(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String userName = sc.next();
        int index = getIndex(list, userName);
        if (index >= 0) {
            System.out.println("请输入注册时的身份证号码");
            String personId = sc.next();
            System.out.println("请输入注册时的手机号码");
            String phoneNumber = sc.next();
            if ((personId.equals(list.get(index).getPersonId())) || (phoneNumber.equals(list.get(index).getPhoneNumber()))) {
                System.out.println("请输入新密码");
                String newPassword = sc.next();
                list.get(index).setPassword(newPassword);
                System.out.println("密码修改成功");
            } else {
                System.out.println("账号信息不匹配,修改失败");
            }
        } else {
            System.out.println("该用户未注册");
        }
    }

    //判断用户名唯一
    public static boolean contains(ArrayList<User> list, String userName) {
        int index = getIndex(list, userName);
        return index >= 0;
    }

    //判断唯一并获取索引
    public static int getIndex(ArrayList<User> list, String userName) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            String uN = user.getUserName();
            if (uN.equals(userName)) {
                return i;
            }
        }
        return -1;
    }

    //判断字符串中是否为字母或数字a~z A~Z 0~9
    public static boolean letterOrNumber(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++;
            }
        }
        return count == userName.length();
    }

    //判断字符串中字母的个数
    public static int getLetter(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;
    }

    //判断字符串中字符为0~9的个数
    public static int getNumber(String userName) {
        int count = 0;
        for (int i = 0; i < userName.length(); i++) {
            char c = userName.charAt(i);
            if (c >= '0' && c <= '9') {
                count++;
            }
        }
        return count;
    }

    //获取验证码
    public static String getVerficationCode() {
        String code = "";
        //1.创建长度为52的字符数组,包括全部大大小写字母
        char[] ch = new char[52];
        for (int i = 0; i < ch.length; i++) {
            if (i < 26) {
                ch[i] = (char) (65 + i);
            } else {
                ch[i] = (char) (97 + i - 26);
            }
        }
        //2.循环获取5个字母字符
        Random r = new Random();
        char[] newCH = new char[5];
        for (int i = 0; i < newCH.length; i++) {
            int index = r.nextInt(52);
            newCH[i] = ch[index];
        }
        //3.随机生成一个数字字符,随机插入
        int number = r.nextInt(9) + 1;
        char tochar = (char) (number + 48);
        int random = r.nextInt(5);
        newCH[random] = tochar;
        return new String(newCH);
    }
}
package cn.com.dao.chivementdao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import cn.com.util.DBConnection; import cn.com.util.DBSql; import cn.com.vo.chivementvo.ChivementVo; public class ExamDao { private Connection conn = DBConnection.getConnectionOracle(); private ChivementVo examVo; public ExamDao() { } public ExamDao(ChivementVo examVo) { super(); this.examVo = examVo; } /** * 全部查询 */ public Object[][] selectAll() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_ALL); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumberAll(DBSql.SELECT_ALL_COUNT)][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据学号查询 */ public Object[][] selectBySid() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_S_ID); ps.setInt(1, examVo.getS_id()); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumber(DBSql.SELECT_BY_S_ID_COUNT, examVo .getS_id())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据组号查询 */ public Object[][] selectByGid() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_G_ID); ps.setInt(1, examVo.getG_id()); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumber(DBSql.SELECT_BY_G_ID_COUNT, examVo .getG_id())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据课程号查询 */ public Object[][] selectByCid() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_C_ID); ps.setInt(1, examVo.getC_id()); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumber(DBSql.SELECT_BY_C_ID_COUNT, examVo .getC_id())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { // System.out.println( examVo.getG_id()); date[i][j] = rs.getObject(j+1); } i++; } // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据姓名模糊查询 * * @return */ public Object[][] selectByName() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_S_NAME); ps.setString(1, examVo.getS_name()); ps.setString(2, "%" + examVo.getS_name() + "%"); ps.setString(3, "%" + examVo.getS_name()); ps.setString(4, examVo.getS_name() + "%"); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumberByName(DBSql.SELECT_BY_S_NAME_COUNT, examVo.getS_name())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据课程名称模糊查询 * * @return */ public Object[][] selectByClassName() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_CLASS_NAME); ps.setString(1, examVo.getClass_name()); ps.setString(2, "%" + examVo.getClass_name() + "%"); ps.setString(3, "%" + examVo.getClass_name()); ps.setString(4, examVo.getClass_name() + "%"); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumberByName(DBSql.SELECT_BY_CLASS_COUNT, examVo.getClass_name())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 修改选中学生的成绩 * */ public void updatSelectClass() { PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.UPDATE_EXAM_BY_STUID); ps.setInt(1, examVo.getClassExamChivement()); ps.setInt(2, examVo.getS_id()); ps.setInt(3, examVo.getC_id()); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } /** * 得到所有课程号和课程名 * * @return */ public String[] getClassNoName() { String[] classNoName = null; PreparedStatement ps = null; ResultSet rs = null; int j = 0; try { int i = getnumberAll(DBSql.SELECT_CLASS_NAME_COUNT); classNoName = new String[i + i]; ps = conn.prepareStatement(DBSql.SELECT_CLASS_NAME); rs = ps.executeQuery(); while (rs.next()) { classNoName[j] = rs.getString(1); classNoName[j + i] = rs.getString(2); j++; } } catch (SQLException e) { e.printStackTrace(); } return classNoName; } /** * 根据科目修改成绩 查询学号 姓名 成绩 * */ public void SelectClassStuName() { int i = 0; PreparedStatement ps = null; ResultSet rs = null; int j = getnumberBySelectClassName( DBSql.SELECT_CLASS_STU_SNO_SNAME_EXAM_COUNT, examVo.getC_id()); int[] sNum = new int[j]; String[] sName = new String[j]; int[] classExam = new int[j]; try { ps = conn.prepareStatement(DBSql.SELECT_CLASS_STU_SNO_SNAME_EXAM); ps.setInt(1, examVo.getC_id()); rs = ps.executeQuery(); while (rs.next()) { sNum[i] = rs.getInt(1); sName[i] = rs.getString(2); classExam[i] = rs.getInt(3); i++; } } catch (SQLException e) { e.printStackTrace(); } examVo.setSid(sNum); examVo.setSname(sName); examVo.setClassExam(classExam); } /** * 根据科目修改成绩 查询学号 姓名 成绩 后修改成绩 * */ public void UpdateClassStuName() { // int i = 0; PreparedStatement ps = null; ResultSet rs = null; int j = getnumberBySelectClassName( DBSql.SELECT_CLASS_STU_SNO_SNAME_EXAM_COUNT, examVo.getC_id()); try { ps = conn.prepareStatement(DBSql.UPDATE_CHIVEMENT_BY_CLASS); for(int i =0;i<j;i++){ ps.setInt(1, examVo.getClassExam()[i]); ps.setInt(2, examVo.getSid()[i]); ps.setInt(3, examVo.getC_id()); ps.executeUpdate(); } } catch (SQLException e) { e.printStackTrace(); } } /** * 全部查询获得行数 * * @return */ public int getnumberAll(String str) { int number = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(str); rs = ps.executeQuery(); rs.next(); number = rs.getInt(1); // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return number; } /** * 根据学号 根据组号 根据课程号查询 获得行数 * * @return */ public int getnumber(String str, int i) { int number = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(str); ps.setInt(1, i); rs = ps.executeQuery(); rs.next(); number = rs.getInt(1); // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return number; } /** * 根据姓名 课程名 查询 获得行数 * */ public int getnumberByName(String str, String i) { int number = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(str); ps.setString(1, i); ps.setString(2, "%" + i + "%"); ps.setString(3, "%" + i); ps.setString(4, i + "%"); rs = ps.executeQuery(); rs.next(); number = rs.getInt(1); } catch (SQLException e) { e.printStackTrace(); } return number; } // /** // * 根据课程名查询 获得行数 // * // */ // // public int getnumberByClassName(String str, String i) { // int number = 0; // PreparedStatement ps = null; // ResultSet rs = null; // try { // ps = conn.prepareStatement(str); // ps.setString(1, i); // ps.setString(2, "%" + i + "%"); // ps.setString(3, "%" + i); // ps.setString(4, i + "%"); // rs = ps.executeQuery(); // rs.next(); // number = rs.getInt(1); // } catch (SQLException e) { // e.printStackTrace(); // } // return number; // } /** * * 根据课程名修改成绩获得行数 * * @param str * @param i * @return */ public int getnumberBySelectClassName(String str, int i) { int number = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(str); ps.setInt(1, i); rs = ps.executeQuery(); rs.next(); number = rs.getInt(1); } catch (SQLException e) { e.printStackTrace(); } return number; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值