源代码附在文章末尾
注意:
-
代码中定义了Student和User两个类,最初的登录操作对应User,之后才是对Student信息的增删改查。代码中对大部分用户键入都进行了校验,校验规则如下
用户名: 长度在3~15位 只能是数字和字母的组合不能都是数字 用户名已存在,无法注册手机号: 长度11位 不能以0开头 必须都是数字身份证: 长度为18 不能以0开头 前十七位是数字 最后一位是x或X或数字
-
这个简易系统没有使用到数据库,因此每一次重启之后,之前的数据都会丢失需要再次创建用户以及学生信息
功能演示
-
进入学生管理系统后选择操作(登录、注册、忘记密码、退出)
如果未注册会提示请先注册再登录
-
进行注册
注册时如果两次密码不一致或者身份证号码以及手机号码长度不正确均有错误提示,需要再次输入正确格式的数据。同时注册的信息也会打印在控制台
-
登录成功
接下来可以对学生的信息进行增删改查
-
添加学生信息
-
修改并查询学生信息
对比上面的信息可以发现学生的信息全部修改成功
-
系统中有的功能具有实现,因篇幅有限就不一一展示,剩下的部分可自行探索
源代码
包含以下几个文件(需要放在同一文件夹下)
Student.java
package com.itheima.studentsystem;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;}
}
StudentSystem.java
package com.itheima.studentsystem;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;public class StudentSystem {public static void startStudentSystem(){ArrayList<com.itheima.studentsystem.Student> list=new ArrayList<>();loop:while (true) { //loop: 是一个标签,它给 while (true) 循环一个名字。标签用于在复杂的循环结构中,例如用 break 语句跳出多层循环。break loop; 可以用来直接跳出标记为 loop 的循环。System.out.println("-------------------欢迎来到学生管理系统--------------------");System.out.print("1:添加学生\t");System.out.print("2:删除学生\t");System.out.print("3:修改学生\t");System.out.print("4:查询学生\t");System.out.println("5:退出");System.out.print("请输入您的选择:");Scanner sc=new Scanner(System.in);String choose=sc.next();switch (choose){case "1"->addStudent(list);case "2"->deleteStudent(list);case "3"->updateStudent(list);case "4"->queryStudent(list);case "5"-> {System.out.println("5:退出");break loop;}default -> System.out.println("没有这个选项");}}}//添加学生public static void addStudent(ArrayList<com.itheima.studentsystem.Student> list){//利用空参构造创建学生对象com.itheima.studentsystem.Student s=new com.itheima.studentsystem.Student();Scanner sc=new Scanner(System.in);while (true){System.out.println("请输入学生id");String id=sc.next();boolean flag=contain(list,id);if(flag){//id存在,需要重新录入System.out.println("id已经存在,请重新录入");}else{//id不存在,可以使用ids.setId(id);break;}}System.out.println("请输入学生姓名");String name=sc.next();s.setName(name);System.out.println("请输入学生年龄");int age=sc.nextInt();s.setAge(age);System.out.println("请输入学生家庭住址");String address=sc.next();s.setAddress(address);//把学生对象添加到集合中list.add(s);System.out.println("学生信息添加成功");}//删除学生public static void deleteStudent(ArrayList<com.itheima.studentsystem.Student> list){System.out.println("请输入需要删除学生的id");Scanner sc=new Scanner(System.in);String id=sc.next();int index=getIndex(list,id); //index大于0,存在id,直接删除,index等于-1,不存在id,结束方法回到初始菜单if(index>=0){list.remove(index);System.out.println("id为:" +id +"的学生成绩删除成功");}else{System.out.println("id不存在,删除失败");}}//修改学生public static void updateStudent(ArrayList<com.itheima.studentsystem.Student> list){System.out.println("请输入需要修改学生信息的对应id");Scanner sc=new Scanner(System.in);String id=sc.next();int index=getIndex(list,id); //获取学生的索引if (index<0){System.out.println("id不存在,请重新输入");return;}else{System.out.println("id存在");System.out.println("请重新输入学生姓名");list.get(index).setName(sc.next());System.out.println("请重新输入学生年龄");list.get(index).setAge(Integer.parseInt(sc.next()));System.out.println("请重新输入学生家庭住址");list.get(index).setAddress(sc.next());System.out.println("学生信息修改成功");}}//查询学生public static void queryStudent(ArrayList<com.itheima.studentsystem.Student> list){if(list.size()==0){System.out.println("当前无学生信息,请添加后再查询");//结束方法return;}//打印表头信息System.out.println("id\t姓名\t年龄\t家庭住址");for(int i=0;i<list.size();i++){com.itheima.studentsystem.Student stu=list.get(i);System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());}}public static boolean contain(ArrayList<com.itheima.studentsystem.Student> list,String id){return getIndex(list,id)>=0;}public static int getIndex(ArrayList<com.itheima.studentsystem.Student> list, String id){//遍历集合,得到每一个学生对象的id,对比学生id和传入的id如果一样就返回索引,不一样就返回-1for (int i=0;i<list.size();i++){com.itheima.studentsystem.Student stu=list.get(i);String sid=stu.getId();if(sid.equals(id)){return i;}}return -1;}}
User.java
package com.itheima.studentsystem;
public class User {private String username;private String password;private String personID;private String phoneNumber;public User(String username, String password, String personID, String phoneNumber) {this.username = username;this.password = password;this.personID = personID;this.phoneNumber = phoneNumber;}public User() {}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 getPersonID() {return personID;}public void setPersonID(String personID) {this.personID = personID;}public String getPhoneNumber() {return phoneNumber;}public void setPhoneNumber(String phoneNumber) {this.phoneNumber = phoneNumber;}
}
App.java
package com.itheima.studentsystem;import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;import static com.itheima.studentsystem.StudentSystem.startStudentSystem;public class App {public static void main(String[] args){ArrayList<com.itheima.studentsystem.User> list=new ArrayList<>();Scanner sc=new Scanner(System.in);while (true){System.out.println("欢迎来到学生管理系统:");System.out.println("请选择操作:1.登录 2.注册 3.忘记密码 4.退出");String choose=sc.next();switch (choose){case "1"->login(list);case "2"->register(list);case "3"->forgetPassword(list);case "4"->{System.out.println("退出");System.exit(0);}default -> System.out.println("没有此选项");}}}private static void forgetPassword(ArrayList<com.itheima.studentsystem.User> list) {Scanner sc=new Scanner(System.in);System.out.println("请输入用户名");String username=sc.next();//判断用户名是否存在boolean flag=contains(list,username);if (!flag){System.out.println("用户名"+username+"未注册,请先注册再登录");return;}System.out.println("请输入身份证号");String personID=sc.next();System.out.println("手机号码");String phoneNumber=sc.next();com.itheima.studentsystem.User user=new com.itheima.studentsystem.User(username,null,personID,phoneNumber);boolean findUser=false;com.itheima.studentsystem.User u=list.get(0);for (int i = 0; i < list.size(); i++) {u=list.get(i);if (u.getUsername().equals(username)&&u.getPersonID().equals(personID)&&u.getPhoneNumber().equals(phoneNumber)){findUser=true;}}if (!findUser){System.out.println("用户信息错误,无法修改密码");return;}while (true) {System.out.println("请输入新的密码");String newPassword=sc.next();System.out.println("请再次输入密码");String newPasswordAgain=sc.next();if (newPassword.equals(newPasswordAgain)){u.setPassword(newPassword);break;}else {System.out.println("两次输入的密码不一致");continue;}}}private static void register(ArrayList<com.itheima.studentsystem.User> list) {Scanner sc=new Scanner(System.in);String username;String password;String personID;String phoneNumber;//键盘录入用户名while (true) {System.out.println("请输入用户名:");username=sc.next();//先验证格式是否正确,再验证是否唯一//用户名长度在3~15位//用户名只能是数字和字母的组合不能都是数字boolean flag1=checkUsername(username);if (!flag1){System.out.println("用户名格式不满足条件,需要重新输入");continue;}//校验用户名唯一boolean flag2=contains(list,username);if (flag2){//用户名已存在,无法注册System.out.println("用户名"+username +"已存在,无法注册,请重新录入");}else {System.out.println("用户名"+username +"可以使用");break;}}//键盘录入密码while (true) {System.out.println("请输入密码:");password=sc.next();System.out.println("请再次输入密码:");String againPassword=sc.next();if (!password.equals(againPassword)){System.out.println("两次密码不一致,请重新输入");continue;}else {System.out.println("两次密码一致,继续录入");break;}}//键盘录入身份证号码while (true) {System.out.println("请输入身份证号码");personID=sc.next();boolean flag3=checkPersonID(personID);if (flag3){System.out.println("身份证号码满足要求");break;}else {System.out.println("身份证号码格式有误,请重新输入");continue;}}//键盘录入手机号while (true) {System.out.println("请输入手机号码");phoneNumber=sc.next();boolean flag4=checkPhoneNumber(phoneNumber);if (flag4){System.out.println("手机号码格式正确");break;}else {System.out.println("手机号码格式有误,请重新输入");continue;}}//把用户对象添加到集合com.itheima.studentsystem.User u=new com.itheima.studentsystem.User(username,password,personID,phoneNumber);list.add(u);System.out.println("注册成功");//遍历用户集合printList(list);}private static void printList(ArrayList<com.itheima.studentsystem.User> list) {for (int i = 0; i < list.size(); i++) {com.itheima.studentsystem.User user=list.get(i);System.out.println(user.getUsername()+","+user.getPassword()+","+user.getPersonID()+","+user.getPhoneNumber());}}private static boolean checkPhoneNumber(String phoneNumber) {//长度11位if (phoneNumber.length()!=11){return false;}//不能以0开头if (phoneNumber.startsWith("0")){return false;}//必须都是数字for (int i = 0; i < phoneNumber.length(); i++) {char c=phoneNumber.charAt(i);if (!(c>='0'&&c<='9')){return false;}}return true;}private static boolean checkPersonID(String personID) {//长度为18if (personID.length()!=18){return false;}//不能以0开头if (personID.startsWith("0")){return false;}//前十七位是数字for (int i = 0; i < personID.length()-1; i++) {char c=personID.charAt(i);if (!(c>='0'&&c<='9')){return false;}}//最后一位是x或X或数字char endChar=personID.charAt(17);if (endChar>='0'&&endChar<='9'||endChar=='x'||endChar=='X'){return true;}else {return false;}}private static boolean contains(ArrayList<com.itheima.studentsystem.User> list,String username){for (int i = 0; i < list.size(); i++) {com.itheima.studentsystem.User user=list.get(i);String rightUsername=user.getUsername();if (rightUsername.equals(username)){return true;}}return false;}private static boolean checkUsername(String username) {//用户名长度在3~15位int len=username.length();if(len<3||len>15){return false;}//用户名只能是数字和字母的组合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'))){return false;}}//不能都是数字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++;break;}}return count>0;}private static void login(ArrayList<com.itheima.studentsystem.User> list) {Scanner sc=new Scanner(System.in);for (int i = 0; i < 3; i++) {System.out.println("请输入用户名");String username=sc.next();//判断用户名是否存在boolean flag=contains(list,username);if (!flag){System.out.println("用户名"+username+"未注册,请先注册再登录");return;}System.out.println("请输入密码");String password=sc.next();while (true) {String rightCode=getCode();System.out.println("验证码为:"+rightCode);System.out.println("请输入验证码");String code=sc.next();if (code.equalsIgnoreCase(rightCode)){System.out.println("验证码输入正确");break;}else {System.out.println("验证码错误,请重新输入");continue;}}//验证用户名和密码是否正确,判断集合中是否包含用户名和密码//封装思想,将零散的数据封装成一个对象传递com.itheima.studentsystem.User userInfo=new com.itheima.studentsystem.User(username,password,null,null);boolean result=CheckUserInfo(list,userInfo);if (result){System.out.println("登录成功,欢迎使用学生管理系统");//创建对象调用方法,启动学生管理系统com.itheima.studentsystem.Student ss=new com.itheima.studentsystem.Student();startStudentSystem();break;}else {System.out.println("登录失败,用户名或密码错误");if (i==2){System.out.println("已尝试3次,当前账号被锁定");return;}else {System.out.println("用户名或密码错误,已尝试"+(i+1)+"次");}}}}private static boolean CheckUserInfo(ArrayList<com.itheima.studentsystem.User> list, com.itheima.studentsystem.User userInfo) {//遍历集合,判断用户是否存在,存在则登录成功for (int i = 0; i < list.size(); i++) {com.itheima.studentsystem.User user=list.get(i);if (user.getUsername().equals(userInfo.getUsername())&&user.getPassword().equals(userInfo.getPassword())){return true;}}return false;}//生成一个验证码private static String getCode(){//创建一个集合添加所有大写和小写字母ArrayList<Character> list =new ArrayList<>();for (int i = 0; i < 26; i++) {list.add((char)('a'+i));list.add((char)('A'+i));}StringBuilder sb=new StringBuilder();//随机抽取四个字符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);}//末尾添加一个随机数字int number=r.nextInt(10);sb.append(number);//保证数字可以出现在任意位置——将字符串变成字符数组,在字符数组中修改,然后再创建一个新的字符串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);}
}