赞
踩
若未注册,提示先注册,返回主界面
输入密码
生成验证码并验证验证码
最后判断密码是否正确,密码错误3次返回主界面
用户名唯一,长度3~15
用户名只能由大小写字母或数字组成,不能是纯数字
身份证必须为18位,首位不为0
前17位必须是数字,末尾为数字或‘x’或‘X’
手机号必须为11位,首位不为0
需二次输入验证
输入身份证及手机号,任何一个错误提示信息不匹配,返回主界面
需二次验证
话不多说,直接放代码
管理员类:
- //管理员类:
- package project;
-
- public class StudentAdmin {
- private String username;
- private String password;
- private String idNum;
- private String phone;
-
- public StudentAdmin(){
- }
- public StudentAdmin(String username,String password,String idNum,String phone){
- this.username=username;
- this.password=password;
- this.idNum=idNum;
- this.phone=phone;
- }
-
- 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 getIdNum() {
- return idNum;
- }
-
- public void setIdNum(String idNum) {
- this.idNum = idNum;
- }
-
- public String getPhone() {
- return phone;
- }
-
- public void setPhone(String phone) {
- this.phone = phone;
- }
-
- }
学生类:
- //学生类:
- package project;
-
- public class Student {
- private int id;
- private String name;
- private int age;
- private String address;
-
- public Student() {
- }
- public Student(int id, String name, int age, String address) {
- this.id = id;
- this.name = name;
- this.age = age;
- this.address = address;
- }
-
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
-
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- }
- package project;
-
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.Scanner;
-
- public class StudentSystem {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- ArrayList<StudentAdmin> list = new ArrayList<>();
-
- while (true) {
- System.out.println("---------------------欢迎来到学生管理系统---------------------");
- System.out.println("---------------------------1.登录--------------------------");
- System.out.println("---------------------------2.注册--------------------------");
- System.out.println("--------------------------3.忘记密码------------------------");
- System.out.println("---------------------------4.退出--------------------------");
- System.out.println("请输入选择:");
- String choose = sc.next();
- switch (choose) {
- case "1":
- boolean login = login(list);
- if (login) {
- system();
- }
- break;
- case "2":
- register(list);
- break;
- case "3":
- forgetPassword(list);
- for (int i = 0; i < list.size(); i++) {
- StudentAdmin sa = list.get(i);
- System.out.println(sa.getUsername() + " " + sa.getPassword() + " " + sa.getIdNum() + " " + sa.getPhone());
- }
- break;
- case "4":
- System.out.println("谢谢使用,再见");
- System.exit(0);
- default:
- System.out.println("没有该选项");
- break;
- }
- }
-
-
- }
-
-
- public static boolean login(ArrayList<StudentAdmin> list) {
- Scanner sc = new Scanner(System.in);
- System.out.println("---------------------------登录中--------------------------");
- int index;
- System.out.println("请输入用户名:");
- String username = sc.next();
- if (getAdminIndex(list, username) == -1) {
- System.out.println("用户名不存在,请先注册");
- return false;
- } else {
- index = getAdminIndex(list, username);
- }
-
- System.out.println("请输入密码:");
- for (int i = 0; i < 3; i++) {
- String password = sc.next();
-
- while (true) {
- String yzm = yzm();
- System.out.println("请输入验证码:");
- String ipYzm = sc.next();
- if (!yzm.equalsIgnoreCase(ipYzm)) {
- System.out.println("验证失败,请重新验证");
- } else {
- break;
- }
- }
-
- if (!list.get(index).getPassword().equals(password)) {
- if (i == 2) {
- System.out.println("登录失败");
- return false;
- } else {
- System.out.println("密码错误,剩余" + (2 - i) + "次机会");
- }
- } else {
- System.out.println("登录成功");
- return true;
- }
- }
- return false;
- }
-
- public static ArrayList<StudentAdmin> register(ArrayList<StudentAdmin> list) {
- Scanner sc = new Scanner(System.in);
- StudentAdmin sa = new StudentAdmin();
- char num[]={'0','1','2','3','4','5','6','7','8','9'};
- char abc[] = new char[52];
- for (int i = 0; i < abc.length; i++) {
- if (i < 26) {
- abc[i] = (char) (97 + i);
- } else {
- abc[i] = (char) (65 + i - 26);
- }
- }
- System.out.println("---------------------------注册中--------------------------");
- System.out.println("请输入用户名:");
- while (true) {
- int numCnt=0;
- boolean con=true;
- String username = sc.next();
- for (int i = 0; i < username.length(); i++) {
- boolean abcCon=contain(username.charAt(i),abc);
- boolean numCon=contain(username.charAt(i),num);
- if (numCon){
- numCnt++;
- }
- if ((!abcCon) && (!numCon)){
- con=false;
- }
- }
- if (getAdminIndex(list, username) >= 0) {
- System.out.println("该用户名已存在,请重新输入:");
- } else if (username.length() < 3 || username.length() > 15) {
- System.out.println("用户名长度应为3~15位,请重新输入:");
- } else if (!con) {
- System.out.println("用户名只能由大小写字母及数字组成,请重新输入:");
- }else if (numCnt==username.length()){
- System.out.println("用户名不能是纯数字,请重新输入:");
- }else {
- sa.setUsername(username);
- System.out.println("设置成功");
- break;
- }
- }
-
- System.out.println("请设置密码:");
- while (true) {
- String p1 = sc.next();
- System.out.println("请再次确认:");
- String p2 = sc.next();
- if (!p1.equals(p2)) {
- System.out.println("两次输入不一致,请重新设置:");
- } else {
- sa.setPassword(p1);
- System.out.println("设置成功");
- break;
- }
- }
-
- System.out.println("验证身份证:");
- while (true) {
- boolean numCon=true;
- boolean endCon=false;
- String idNum = sc.next();
- String idSub=idNum.substring(0,17);
- char idEnd=idNum.charAt(17);
- for (int i = 0; i < idSub.length(); i++) {
- if (!contain(idSub.charAt(i),num)){
- numCon=false;
- }
- }
- if ((idEnd>='0' && idEnd<='9') || idEnd=='X' || idEnd=='x'){
- endCon=true;
- }
- if (idNum.length() != 18) {
- System.out.println("身份证应为18位,请重新输入:");
- } else if (idNum.startsWith("0")) {
- System.out.println("身份证首位不为0.请重新输入:");
- }else if (!numCon || !endCon){
- System.out.println("身份证前17位必须为纯数字,最后一位为数字或大写X或小写x,请重新输入:");
- }else {
- sa.setIdNum(idNum);
- System.out.println("验证成功");
- break;
- }
- }
-
- System.out.println("手机号验证:");
- while (true) {
- boolean numCon=true;
- String phone = sc.next();
- for (int i = 0; i < phone.length(); i++) {
- char c=phone.charAt(i);
- if(!(c>='0' && c<='9')){
- numCon=false;
- break;
- }
- }
- if (phone.length() != 11) {
- System.out.println("手机号应为11位,请重新输入:");
- } else if (phone.charAt(0) == '0') {
- System.out.println("手机号首位不为0.请重新输入:");
- }else if (!numCon){
- System.out.println("手机号应为纯数字,请重新输入:");
- }else {
- sa.setPhone(phone);
- System.out.println("验证成功");
- break;
- }
- }
-
- list.add(sa);
- System.out.println("注册成功");
- return list;
-
- }
-
- public static ArrayList<StudentAdmin> forgetPassword(ArrayList<StudentAdmin> list) {
- Scanner sc = new Scanner(System.in);
- System.out.println("---------------------------忘记密码--------------------------");
- int index;
- System.out.println("请输入用户名:");
- String username = sc.next();
- if (getAdminIndex(list, username) == -1) {
- System.out.println("用户名不存在,请先注册");
- return null;
- } else {
- index = getAdminIndex(list, username);
- }
-
- System.out.println("输入身份证:");
- String inIdNum = sc.next();
- System.out.println("输入手机号:");
- String inPhone = sc.next();
- StudentAdmin sa = list.get(index);
- boolean idNum = sa.getIdNum().equals(inIdNum);
- boolean phone = sa.getPhone().equals(inPhone);
- if (idNum && phone) {
- System.out.println("请输入密码");
- while (true) {
- String p1 = sc.next();
- System.out.println("请再次确认:");
- String p2 = sc.next();
- if (!p1.equals(p2)) {
- System.out.println("两次输入不一致,请重新设置:");
- } else {
- sa.setPassword(p1);
- list.set(index, sa);
- System.out.println("设置成功");
- return list;
- }
- }
- } else {
- System.out.println("信息不匹配,修改失败");
- return null;
- }
- }
-
- public static int getAdminIndex(ArrayList<StudentAdmin> list, String username) {
- for (int i = 0; i < list.size(); i++) {
- if (list.get(i).getUsername().equals(username)) {
- return i;
- }
- }
- return -1;
- }
-
- public static boolean contain(char c, char[] s) {
- for (int i = 0; i < s.length; i++) {
- if (s[i] == c) {
- return true;
- }
- }
- return false;
- }
-
- public static String yzm() {
- Random r = new Random();
- char abc[] = new char[4];
- for (int i = 0; i < abc.length; i++) {
- int index = r.nextInt(52);
- abc[i] = abc(index);
- }
- String zm = new String(abc);
- int sub = r.nextInt(abc.length);
- int ram = r.nextInt(10);
- String first = zm.substring(0, sub);
- String end = zm.substring(sub);
- StringBuilder sb = new StringBuilder();
- String yzm = sb.append(first).append(ram).append(end).toString();
- System.out.println(yzm);
- return yzm;
- }
-
- public static char abc(int index) {
- char abc[] = new char[52];
- for (int i = 0; i < abc.length; i++) {
- if (i < 26) {
- abc[i] = (char) (97 + i);
- } else {
- abc[i] = (char) (65 + i - 26);
- }
- }
- return abc[index];
- }
-
-
- public static void system() {
- Scanner sc = new Scanner(System.in);
- ArrayList<Student> list = new ArrayList<>();
-
- 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("-------------------------6.退出----------------------------");
- System.out.println("请输入选择:");
-
- String input = sc.next();
- switch (input) {
- case "1":
- addStudent(list);
- break;
- case "2":
- deleteStudent(list);
- break;
- case "3":
- updateStudent(list);
- break;
- case "4":
- searchStudent(list);
- break;
- case "5":
- showStudent(list);
- break;
- case "6":
- break loop;
- //System.exit(0); //停止虚拟机运行
- default:
- System.out.println("没有该选项");
- break;
- }
- }
-
- }
-
- public static ArrayList<Student> addStudent(ArrayList<Student> list) {
- Scanner sc = new Scanner(System.in);
- Student stu = new Student();
- System.out.println("请输入增加学生id:");
- while (true) {
- int id = sc.nextInt();
- int index = getIndex(list, id);
- if (index < 0) {
- stu.setId(id);
- break;
- } else {
- System.out.println("该id已存在,请重新输入:");
- }
- }
- System.out.println("请输入增加学生姓名:");
- stu.setName(sc.next());
- System.out.println("请输入增加学生年龄:");
- stu.setAge(sc.nextInt());
- System.out.println("请输入增加学生地址:");
- stu.setAddress(sc.next());
- list.add(stu);
- System.out.println("添加成功");
- return list;
- }
-
- public static ArrayList<Student> deleteStudent(ArrayList<Student> list) {
- if (list.size() != 0) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入删除学生id:");
- while (true) {
- int id = sc.nextInt();
- int index = getIndex(list, id);
- if (index >= 0) {
- list.remove(index);
- System.out.println("删除成功");
- break;
- } else {
- System.out.println("该id不存在,请重新输入:");
- }
- }
- } else {
- System.out.println("当前无学生信息,请添加后删除");
- }
- return list;
- }
-
-
- public static int getIndex(ArrayList<Student> list, int id) {
- for (int i = 0; i < list.size(); i++) {
- if (id == list.get(i).getId()) {
- return i;
- }
- }
- return -1;
- }
-
- public static ArrayList<Student> updateStudent(ArrayList<Student> list) {
- if (list.size() != 0) {
- Scanner sc = new Scanner(System.in);
- Student stu = new Student();
- System.out.println("请输入修改学生id:");
- while (true) {
- int id = sc.nextInt();
- int index = getIndex(list, id);
- if (index >= 0) {
- stu.setId(id);
- System.out.println("请输入修改后的姓名:");
- stu.setName(sc.next());
- System.out.println("请输入修改后的年龄:");
- stu.setAge(sc.nextInt());
- System.out.println("请输入修改后的地址:");
- stu.setAddress(sc.next());
- list.set(index, stu);
- System.out.println("修改成功");
- break;
- } else {
- System.out.println("该id不存在,请重新输入:");
- }
- }
- } else {
- System.out.println("当前无学生信息,请添加后修改");
- }
- return list;
- }
-
- public static void searchStudent(ArrayList<Student> list) {
- if (list.size() != 0) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入查询学生id:");
- while (true) {
- int id = sc.nextInt();
- int index = getIndex(list, id);
- if (index >= 0) {
- Student stu = list.get(index);
- System.out.println(" " + stu.getId() + '\t' + '\t' + stu.getName() + '\t' + '\t' + stu.getAge() + '\t' + '\t' + stu.getAddress());
- break;
- } else {
- System.out.println("id不存在,请重新输入:");
- }
- }
- } else {
- System.out.println("当前无学生信息,请添加后查询");
- }
- }
-
- public static void showStudent(ArrayList<Student> list) {
- if (list.size() != 0) {
- System.out.println(" " + "id" + '\t' + '\t' + "姓名" + '\t' + '\t' + "年龄" + '\t' + '\t' + "地址");
- for (int i = 0; i < list.size(); i++) {
- Student stu = list.get(i);
- System.out.println(" " + stu.getId() + '\t' + '\t' + stu.getName() + '\t' + '\t' + stu.getAge() + '\t' + '\t' + stu.getAddress());
- }
- } else {
- System.out.println("当前无学生信息,请添加后遍历");
- }
- }
-
- }
OVER~~
--------------------------------------------------分----------割----------线--------------------------------------------------
发现一个小bug:
身份证验证中,由于为后续判断前17位及末尾 提前进行了字符串分割,导致当输入小于18时,无法截取到17位而报错,这里的解决方法是将判断位数调整至外层if判断,else执行字符串分割及其他判断。
此处将身份证验证代码进行修改:
- //验证身份证终板:
- System.out.println("验证身份证:");
- while (true) {
- boolean numCon=true;
- boolean endCon=false;
- String idNum = sc.next();
- if (idNum.length() != 18) {
- System.out.println("身份证应为18位,请重新输入:");
- }else {
- String idSub=idNum.substring(0,17);
- char idEnd=idNum.charAt(17);
- for (int i = 0; i < idSub.length(); i++) {
- if (!contain(idSub.charAt(i),num)){
- numCon=false;
- }
- }
- if ((idEnd>='0' && idEnd<='9') || idEnd=='X' || idEnd=='x'){
- endCon=true;
- }
- if (idNum.startsWith("0")) {
- System.out.println("身份证首位不为0.请重新输入:");
- }else if (!numCon || !endCon){
- System.out.println("身份证前17位必须为纯数字,最后一位为数字或大写X或小写x,请重新输入:");
- }else {
- sa.setIdNum(idNum);
- System.out.println("验证成功");
- break;
- }
- }
- }
OVER~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。