代码
public class Student {private String id;private String name;private int age;private String address;public Student() {}public Student(String id, String name, int age, String address) {this.id = id;this.name = name;this.age = age;this.address = address;}public String getId() {return id;}public void setId(String 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;}}
import java.util.ArrayList;import java.util.Random;import java.util.Scanner;public class App {public static void main(String[] args) {ArrayList user = new ArrayList();System.out.println("录入数据中...");User u1 = new User("zhangsan", "123", "610302195607011642", "13669125183");User u2 = new User("lisi", "456", "620502196006248385", "13363222321");User u3 = new User("wangwu", "789", "512731188706034846", "13765642332");User u4 = new User("zhaoliu", "123456", "140725184710194731", "13464323579");user.add(u1);user.add(u2);user.add(u3);user.add(u4);System.out.println("录入完毕");String username;String password;String id;String phoneNumber;Scanner sc1 = new Scanner(System.in);while (true) {System.out.println("欢迎来到学生管理系统");System.out.println("请选择操作1登录 2注册 3忘记密码");int select = sc1.nextInt();switch (select) {case 1:while (true) {System.out.println("登录");System.out.println("请输入用户名:(输入-1,则返回登录界面)");username = sc1.next();if (username.equals("-1")) {break;}if (login(username, user)) {System.out.println("登录成功");//StudentSytstem studentSytstem = new StudentSytstem();StudentSytstem.start();// showAll(user);break;} else {System.out.println("登录失败");System.out.println("-----------------------------------------");}}break;case 2:while (true) {System.out.println("注册");System.out.println("请输入用户名:(输入-1,则返回登录界面)");username = sc1.next();if (!usernameRule(username)) {System.out.println("username格式错误");System.out.println("用户名唯一" + "\n" +"用户名长度必须在3~15位之间" + "\n" +"只能是字母加数字的组合,但是不能是纯数字");System.out.println("-----------------------------------------");continue;}if (username.equals("-1")) {break;}if (register(username, user)) {System.out.println("注册成功");printList(user);System.out.println("-----------------------------------------");break;} else {System.out.println("删除失败");System.out.println("-----------------------------------------");}}break;case 3:while(true) {System.out.println("忘记密码");System.out.println("请输入username:(输入-1,则返回登录界面)");username = sc1.next();if (username.equals("-1")) {break;}if(forgetPassword(username, user)) {System.out.println("修改成功");System.out.println("-----------------------------------------");break;} else {System.out.println("修改失败");System.out.println("-----------------------------------------");}}break;case 4:System.out.println("谢谢使用,再见!");System.exit(0);break;default:System.out.println("没有这个选项");break;}}}private static void printList(ArrayList user) {System.out.println(String.format(String.format("%-16s |", "姓名") +String.format("%-8s |", "密码") +String.format("%-18s |", "身份证号") +String.format("%-8s |", "手机号码")));for (int i = 0; i < user.size(); i++) {System.out.print(String.format("%-16s |", user.get(i).getUsername()));System.out.print(String.format("%-8s |", user.get(i).getPassword()));System.out.print(String.format("%-18s |", user.get(i).getId()));System.out.print(String.format("%-8s |", user.get(i).getPhoneNumber()));System.out.println();}}public static boolean login(String username, ArrayList user) {Scanner sc2 = new Scanner(System.in);/** // 生成四位随机数字的验证码,取消注释* Random random = new Random();* int randomNumber = random.nextInt(9000) + 1000;*/for (int i = 0; i < user.size(); i++) {if (username.equals(user.get(i).getUsername())){System.out.println("请输入密码:");String password = sc2.next();// 生成四位随机数字的验证码,替换下一行代码// String generate_code = String.valueOf(randomNumber);String generate_code = getCode();System.out.println("验证码为:" + generate_code);System.out.println("请输入验证码:");String code = sc2.next();if (user.get(i).getPassword().equals(password) && generate_code.equals(code)) {System.out.println("密码或验证码错误!");return true;}}}System.out.println("用户不存在");return false;}public static boolean register(String username, ArrayList user) {for (int i = 0; i < user.size(); i++) {if (username.equals(user.get(i).getUsername())){System.out.println("用户名已存在,不能录入");return false;}}Scanner sc3 = new Scanner(System.in);String password1, password2;while (true) {System.out.println("请输入password:");password1 = sc3.next();System.out.println("请重新输入一次password:");password2 = sc3.next();if (password1.equals(password2)) {break;}System.out.println("两次输入的password不一样,请重新输入");}while (true) {System.out.println("请输入id:");String id = sc3.next();if (!idRule(id)) {System.out.println("id格式错误,请重新输入id:");continue;}System.out.println("请输入phoneNumber:");String phoneNumber = sc3.next();if (!phoneNumberRule(phoneNumber)) {System.out.println("phoneNumber格式错误,请重新输入phoneNumber:");continue;}User u = new User(username, password1, id, phoneNumber);user.add(u);return true;}}public static boolean forgetPassword(String username,ArrayList user) {Scanner sc4 = new Scanner(System.in);for (int i = 0; i =0;){if (!Character.isDigit(target.charAt(i))){flag = false;}}if (flag)return true;return false;}/* * 不使用正则表达式,判断用户名 */public static boolean usernameRule(String username) {int length = username.length();if ((length >= 3 && length <= 15) && !(isNumber(username, length))){return true;}return false;}public static boolean idRule(String id) {int length = id.length();if (length == 18&& !(id.charAt(0) == '0')&& isNumber(id, 17)&& ((isNumber(String.valueOf(id.charAt(17)), 1)) || id.endsWith("X") || id.endsWith("x"))) {return true;}return false;}public static boolean phoneNumberRule(String phoneNumber) {int length = phoneNumber.length();if (length == 11&& !(phoneNumber.startsWith("0"))&& isNumber(phoneNumber, 11)) {return true;}return false;}private static String getCode() {//1.创建一个集合添加所有的大写和小写字母ArrayList 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.要随机抽取4个字符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);}//3.把一个随机数字添加到末尾int number = r.nextInt(10);sb.append(number);//4.如果我们要修改字符串中的内容//先把字符串变成字符数组,在数组中修改,然后再创建一个新的字符串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);}}
import java.util.ArrayList;import java.util.Random;import java.util.Scanner;public class App {staticArrayList user = new ArrayList();static {System.out.println("录入数据中...");User u1 = new User("zhangsan", "123", "610302195607011642", "13669125183");User u2 = new User("lisi", "456", "620502196006248385", "13363222321");User u3 = new User("wangwu", "789", "512731188706034846", "13765642332");User u4 = new User("zhaoliu", "123456", "140725184710194731", "13464323579");user.add(u1);user.add(u2);user.add(u3);user.add(u4);System.out.println("录入完毕");}public static void main(String[] args) throws InterruptedException {String username;String password;String id;String phoneNumber;Scanner sc1 = new Scanner(System.in);while (true) {System.out.println("欢迎来到学生管理系统");System.out.println("请选择操作1登录 2注册 3忘记密码");int select = sc1.nextInt();switch (select) {case 1:while (true) {System.out.println("登录");System.out.println("请输入用户名:(输入-1,则返回登录界面)");username = sc1.next();if (username.equals("-1")) {break;}if (login(username, user)) {System.out.println("登录成功");// StudentSytstem studentSytstem = new StudentSytstem();StudentSytstem.start();// showAll(user);break;} else {System.out.println("登录失败");System.out.println("-----------------------------------------");}}break;case 2:while (true) {System.out.println("注册");System.out.println("请输入用户名:(输入-1,则返回登录界面)");username = sc1.next();if (!usernameRule(username)) {System.out.println("username格式错误");System.out.println("用户名唯一" + "\n" +"用户名长度必须在3~15位之间" + "\n" +"只能是字母加数字的组合,但是不能是纯数字");System.out.println("-----------------------------------------");continue;}if (username.equals("-1")) {break;}if (register(username, user)) {System.out.println("注册成功");System.out.println("-----------------------------------------");break;} else {System.out.println("删除失败");System.out.println("-----------------------------------------");}}break;case 3:while(true) {System.out.println("忘记密码");System.out.println("请输入username:(输入-1,则返回登录界面)");username = sc1.next();if (username.equals("-1")) {break;}if(forgetPassword(username, user)) {System.out.println("修改成功");System.out.println("-----------------------------------------");break;} else {System.out.println("修改失败");System.out.println("-----------------------------------------");}}break;case 4:System.out.println("谢谢使用,再见!");System.exit(0);break;default:System.out.println("没有这个选项");break;}}}public static boolean login(String username, ArrayList user) throws InterruptedException {Scanner sc2 = new Scanner(System.in);/** // 生成四位随机数字的验证码,取消注释* Random random = new Random();* int randomNumber = random.nextInt(9000) + 1000;*/for (int i = 0; i < user.size(); i++) {if (username.equals(user.get(i).getUsername())){for (int j = 0; j < 3; j++) {System.out.println("请输入密码:");String password = sc2.next();// 生成四位随机数字的验证码,替换下一行代码// String generate_code = String.valueOf(randomNumber);String generate_code = getCode();System.out.println("验证码为:" + generate_code);System.out.println("请输入验证码:");String code = sc2.next();if (user.get(i).getPassword().equals(password) && generate_code.equals(code)) {return true;}if (j == 2) {System.out.println("当前系统被锁定3秒");Thread.sleep(3000);} else {System.out.println("用户名、密码或验证码错误,还剩下" + (2 - j) + "次机会");}}}}System.out.println("当前用户" + username + "不存在,请先注册");return false;}public static boolean register(String username, ArrayList user) {for (int i = 0; i < user.size(); i++) {if (username.equals(user.get(i).getUsername())){System.out.println("用户名已存在,不能反复注册");return false;}}Scanner sc3 = new Scanner(System.in);String password1, password2;while (true) {System.out.println("请输入password:");password1 = sc3.next();System.out.println("请重新输入一次password:");password2 = sc3.next();if (password1.equals(password2)) {break;}System.out.println("两次输入的password不一样,请重新输入");}while (true) {System.out.println("请输入id:");String id = sc3.next();if (!idRule(id)) {System.out.println("id格式错误,请重新输入id:");continue;}System.out.println("请输入phoneNumber:");String phoneNumber = sc3.next();if (!phoneNumberRule(phoneNumber)) {System.out.println("phoneNumber格式错误,请重新输入phoneNumber:");continue;}User u = new User(username, password1, id, phoneNumber);user.add(u);System.out.println(String.format("%-16s|", "姓名") +String.format("%-8s|", "密码") +String.format("%-18s|", "身份证号") +String.format("%-8s|", "手机号码"));System.out.print(String.format("%-16s|", username));System.out.print(String.format("%-8s|", password1));System.out.print(String.format("%-18s|", id));System.out.print(String.format("%-8s", phoneNumber));System.out.println();return true;}}public static boolean forgetPassword(String username,ArrayList user) {Scanner sc4 = new Scanner(System.in);for (int i = 0; i =0;){if (!Character.isDigit(target.charAt(i))){flag = false;}}if (flag)return true;return false;}/* * 不使用正则表达式,判断用户名 */public static boolean usernameRule(String username) {int length = username.length();if ((length >= 3 && length <= 15) && !(isNumber(username, length))){return true;}return false;}public static boolean idRule(String id) {int length = id.length();if (length == 18&& !(id.charAt(0) == '0')&& isNumber(id, 17)&& ((isNumber(String.valueOf(id.charAt(17)), 1)) || id.endsWith("X") || id.endsWith("x"))) {return true;}return false;}public static boolean phoneNumberRule(String phoneNumber) {int length = phoneNumber.length();if (length == 11&& !(phoneNumber.startsWith("0"))&& isNumber(phoneNumber, 11)) {return true;}return false;}private static String getCode() {//1.创建一个集合添加所有的大写和小写字母ArrayList 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.要随机抽取4个字符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);}//3.把一个随机数字添加到末尾int number = r.nextInt(10);sb.append(number);//4.如果我们要修改字符串中的内容//先把字符串变成字符数组,在数组中修改,然后再创建一个新的字符串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);}}
import java.util.ArrayList;import java.util.Scanner;public class StudentSytstem {private static final String ADD_STUDENT = "1";private static final String DELE_STUDENT = "2";private static final String UPDATE_STUDENT = "3";private static final String QUERY_STUDENT = "4";private static final String ALL_STUDENT = "5";private static final String LOGIN = "6";private static final String EXIT = "7";static ArrayList student = new ArrayList();static {System.out.println("录入数据中...");Student s1 = new Student("001", "张三", 23, "南京");Student s2 = new Student("002", "李四", 24, "北京");Student s3 = new Student("003", "王五", 25, "广州");Student s4 = new Student("004", "赵六", 26, "深圳");student.add(s1);student.add(s2);student.add(s3);student.add(s4);System.out.println("录入完毕");}public static void start() throws InterruptedException {ArrayList student = new ArrayList();System.out.println("录入数据中...");Student s1 = new Student("001", "张三", 23, "南京");Student s2 = new Student("002", "李四", 24, "北京");Student s3 = new Student("003", "王五", 25, "广州");Student s4 = new Student("004", "赵六", 26, "深圳");student.add(s1);student.add(s2);student.add(s3);student.add(s4);System.out.println("录入完毕");Scanner sc = new Scanner(System.in);String id;String name;int age;String address;while(true) {System.out.println("--------------欢迎来到学生管理系统-----------------" + "\n" +"1:添加学生" + "\n" +"2:删除学生" + "\n" +"3:修改学生" + "\n" +"4:查询学生" + "\n" +"5:所有学生" + "\n" +"6:返回登录界面" + "\n" +"7:退出系统" + "\n" +"请输入您的选择:");int select = sc.nextInt();switch (select) {case ADD_STUDENT:while(true) {System.out.println("添加");System.out.println("请输入id:(输入-1,则返回主菜单)");id = sc.next();if (id.equals("-1")) {break;}if (addStudent(id, student)) {System.out.println("添加成功");showAll(student);break;} else {System.out.println("添加失败");}}break;case DELE_STUDENT:while (true) {System.out.println("删除");System.out.println("请输入id:(输入-1,则返回主菜单)");id = sc.next();if (id.equals("-1")) {break;}if (deleteStudent(id, student)) {System.out.println("删除成功");showAll(student);break;} else {System.out.println("删除失败");}}break;case UPDATE_STUDENT:while(true) {System.out.println("修改");System.out.println("请输入id:(输入-1,则返回主菜单)");id = sc.next();if (id.equals("-1")) {break;}if(updateStudent(id, student)) {System.out.println("修改成功");showAll(student);break;} else {System.out.println("修改失败");}}break;case QUERY_STUDENT:while(true) {System.out.println("查询");System.out.println("请输入id:(输入-1,则返回主菜单)");id = sc.next();if (id.equals("-1")) {break;}if (queryStudent(id, student) != null) {Student s = queryStudent(id, student);System.out.println("id\t\t\t姓名\t\t年龄\t\t家庭住址");System.out.print(s.getId() + "\t");System.out.print(s.getName() + "\t\t");System.out.print(s.getAge() + "\t\t");System.out.print(s.getAddress());System.out.println();break;} else {System.out.println("查找失败");}}break;case ALL_STUDENT:System.out.println("所有学生");showAll(student);break;case LOGIN:System.out.println("返回登录界面");String[] args = {""};App.main(args);case EXIT:System.out.println("退出系统");System.exit(0);default:System.out.println("没有这个选项");break;}}}private static Student queryStudent(String id, ArrayList student) {if (student.size() == 0) {System.out.println("当前无学生信息,请添加后再查询");return null;}for (int i = 0; i < student.size(); i++) {if (id.equals(student.get(i).getId())) {Student s = student.get(i);return s;}}return null;}private static boolean updateStudent(String id, ArrayList student) {Scanner sc = new Scanner(System.in);for (int i = 0; i < student.size(); i++) {if (id.equals(student.get(i).getId())) {System.out.println(student.get(i).getName());System.out.println("请输入修改的姓名:");String new_name = sc.next();System.out.println(student.get(i).getAge());System.out.println("请输入修改的年龄:");int new_age = sc.nextInt();System.out.println(student.get(i).getAddress());System.out.println("请输入修改的地址:");String new_address = sc.next();Student s = new Student(id, new_name, new_age, new_address);student.set(i, s);return true;}}System.out.println("id不存在");return false;}private static boolean deleteStudent(String id, ArrayList student) {for (int i = 0; i < student.size(); i++) {if (id.equals(student.get(i).getId())) {student.remove(i);return true;}}return false;}private static boolean addStudent(String id, ArrayList student) {for (int i = 0; i < student.size(); i++) {if (id.equals(student.get(i).getId())){System.out.println("id已存在,不能录入");return false;}}Scanner sc = new Scanner(System.in);System.out.println("请输入name:");String name = sc.next();System.out.println("请输入age:");int age = sc.nextInt();System.out.println("请输入address:");String address = sc.next();Student s = new Student(id, name, age, address);student.add(s);return true;}private static void showAll(ArrayList student) {if (student.size() == 0) {System.out.println("当前无学生信息,请添加后再查询");return;}for (int i = 0; i < student.size(); i++) {if (i == 0) {System.out.println(String.format("%-8s |", "id") + String.format("%-16s |", "姓名") + String.format("%-8s |", "年龄") + String.format("%-8s |", "家庭住址"));}System.out.print(String.format("%-8s |", student.get(i).getId()));System.out.print(String.format("%-16s |", student.get(i).getName()));System.out.print(String.format("%-8d |", student.get(i).getAge()));System.out.print(String.format("%-8s |", student.get(i).getAddress()));System.out.println();}}}
需求
- 为学生管理系统书写一个登陆、注册、忘记密码的功能。
- 只有用户登录成功之后,才能进入到学生管理系统中进行增删改查操作。
- 采取控制台的方式去书写学生管理系统。
学生类
属性:id、姓名、年龄、家庭住址
用户类
属性:用户名、密码、身份证号码、手机号码
学生管理系统登录界面
注册功能
1,用户名需要满足以下要求
验证要求:
- 用户名唯一
- 用户名长度必须在3~15位之间
- 只能是字母加数字的组合,但是不能是纯数字
2,密码键盘输入两次,两次一致才可以进行注册
3,身份证号码需要验证
验证要求:
- 长度为18位
- 不能以0为开头
- 前17位,必须都是数字
- 最为一位可以是数字,也可以是大写X或小写x
4,手机号验证
- 验证要求:
- 长度为11位
- 不能以0为开头
- 必须都是数字
登录功能
1,键盘录入用户名
2,键盘录入密码
3,键盘录入验证码
验证要求:
- 用户名如果未注册,直接结束方法,并提示:用户名未注册,请先注册
- 判断验证码是否正确,如不正确,重新输入
- 再判断用户名和密码是否正确,有3次机会
登录成功
密码错误3次
用户不存在
忘记密码
1,键盘录入用户名,判断当前用户名是否存在,如不存在,直接结束方法,并提示:未注册
2,键盘录入身份证号码和手机号码
3,判断当前用户的身份证号码和手机号码是否一致,
- 如果一致,则提示输入密码,进行修改。
- 如果不一致,则提示:账号信息不匹配,修改失败。
验证码规则
- 长度为5
- 由4位大写或者小写字母和1位数字组成,同一个字母可重复
- 数字可以出现在任意位置
- 比如:aQa1K
添加功能
键盘录入每一个学生信息并添加,需要满足以下要求:
id唯一
删除功能
键盘录入要删除的学生id,需要满足以下要求:
id存在删除
id不存在,需要提示不存在,并回到初始菜单
修改功能
键盘录入要修改的学生id,需要满足以下要求
id存在,继续录入其他信息
id不存在,需要提示不存在,并回到初始菜单
查询功能
打印所有的学生信息,需要满足以下要求
如果没有学生信息,提示:当前无学生信息,请添加后再查询
如果有学生信息,需要按照格式输出。
待更新
1、正则表达式——>判断格式
2、数据的永久化存储——>登录次数过多,锁定账号
更新
2023/10/24 加入静态代码块
结论
系统有较好的健壮性,对于大部分异常输入都会处理。如果发现有未处理的地方,请在评论区告诉我,我会尽快修改,谢谢!