学生管理系统项目
package exercise;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentProgramme {
public static void main(String[] args) {
ArrayList<Student> studentArrayList = new ArrayList<>();
Student newStudent = new Student("001", "test", 1, "foshan");
studentArrayList.add(newStudent);
Scanner sc = new Scanner(System.in);
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("请输入您的选择:");
int choose = sc.nextInt();
switch (choose) {
case 1 -> addStudent(studentArrayList);
case 2 -> delStudent(studentArrayList);
case 3 -> changeStudent(studentArrayList);
case 4 -> checkStudent(studentArrayList);
case 5 -> System.exit(0);
default -> System.out.println("没有这个选择!");
}
}
}
public static void addStudent(ArrayList<Student> studentArrayList) {
Scanner sc = new Scanner(System.in);
Student newStudent = new Student();
String id = "";
while (true) {
System.out.println("请输入学生id:");
id = sc.next();
if (contains(studentArrayList, id)) {
System.out.println("输入的学生id已存在,请重新输入!");
} else {
newStudent.setId(id);
break;
}
}
System.out.println("请输入学生姓名:");
String name = sc.next();
newStudent.setName(name);
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
newStudent.setAge(age);
System.out.println("请输入学生住址:");
String address = sc.next();
newStudent.setAddress(address);
studentArrayList.add(newStudent);
System.out.println("添加成功!");
}
public static void checkStudent(ArrayList<Student> studentArrayList) {
if (studentArrayList.size() == 0) {
System.out.println("当前无学生信息,请添加后再查询!");
} else {
System.out.println("id\t\t" + "姓名\t\t" + "年龄\t\t" + "家庭住址");
for (Student student : studentArrayList) {
System.out.println(student.getId() + "\t\t" + student.getName() + "\t" + student.getAge() + "\t\t" + student.getAddress());
}
}
}
public static void delStudent(ArrayList<Student> studentArrayList) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入删除的学生id:");
String delId = sc.next();
int index = getIndex(studentArrayList, delId);
if (index >= 0) {
studentArrayList.remove(index);
System.out.println("删除成功!");
} else {
System.out.println("删除的学生id不存在!");
}
}
public static void changeStudent(ArrayList<Student> studentArrayList) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入修改的学生id:");
String changeId = sc.next();
int index = getIndex(studentArrayList, changeId);
if (index >= 0) {
System.out.println("请输入修改学生的姓名:");
String changeName = sc.next();
studentArrayList.get(index).setName(changeName);
System.out.println("请输入修改学生的年龄:");
int changeAge = sc.nextInt();
studentArrayList.get(index).setAge(changeAge);
System.out.println("请输入修改学生的家庭住址:");
String changeAddress = sc.next();
studentArrayList.get(index).setAddress(changeAddress);
System.out.println("修改成功!");
} else {
System.out.println("修改的学生id不存在!");
}
}
public static boolean contains(ArrayList<Student> studentArrayList, String id) {
return getIndex(studentArrayList, id) >= 0;
}
public static int getIndex(ArrayList<Student> studentArrayList, String id) {
for (int i = 0; i < studentArrayList.size(); i++) {
if (studentArrayList.get(i).getId().equals(id)) {
return i;
}
}
return -1;
}
}
package exercise;
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;
}
}
用户名和密码校验
package exercise;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class UserProgramme {
public static void main(String[] args) {
ArrayList<User> list = new ArrayList<>();
User newUser = new User("001abc", "123456", "123456789012345678", "12345678901");
list.add(newUser);
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("-------------欢迎来到学生管理系统----------------");
System.out.println("1:登录");
System.out.println("2:注册");
System.out.println("3:忘记密码");
System.out.println("4:退出");
System.out.println("请输入您的选择:");
int choose = sc.nextInt();
switch (choose) {
case 1 -> longin(list);
case 2 -> register(list);
case 3 -> forgetPassword(list);
case 4 -> System.exit(0);
default -> System.out.println("没有这个选择!");
}
}
}
public static void longin(ArrayList<User> list) {
//键盘录入用户名
Scanner sc = new Scanner(System.in);
System.out.println("输入用户名:");
String userName = sc.next();
//用户名如果未注册,直接结束方法,并提示:用户名未注册,请先注册
int index = getIndex(list, userName);
if (index == -1) {
System.out.println("用户名未注册,请先注册!");
return;
}
//键盘录入密码
System.out.println("输入密码:");
String password = sc.next();
//键盘录入验证码
loop3:
while (true) {
String rightNum = getCheckNum();
System.out.println("验证码:" + rightNum);
System.out.println("输入验证码:");
String checkNum = sc.next();
if (rightNum.equalsIgnoreCase(checkNum)) {
System.out.println("验证码正确!");
for (int i = 0; i < 3; i++) {
if (password.equals(list.get(index).getPassword())) {
System.out.println("登录成功!");
StudentProgramme ss = new StudentProgramme();
ss.startStudentProgramme();
break loop3;
} else {
if (i == 2) {
System.out.println("密码输错3次,登录失败!");
break loop3;
}
System.out.println("重新输入密码,还有" + (2 - i) + "次机会");
password = sc.next();
}
}
//密码输错3次,回到菜单
break;
} else {
System.out.println("验证码不对!");
}
}
}
public static void forgetPassword(ArrayList<User> list) {
//键盘录入用户名,判断当前用户名是否存在
Scanner sc = new Scanner(System.in);
System.out.println("输入用户名:");
String userName = sc.next();
//如不存在,直接结束方法,并提示:未注册
int index = getIndex(list, userName);
if (index == -1) {
System.out.println("未注册!");
return;
}
//键盘录入身份证号码和手机号码
System.out.println("输入身份证号码:");
String id = sc.next();
System.out.println("输入手机号:");
String phone = sc.next();
//判断当前用户的身份证号码和手机号码是否一致
boolean flag1 = list.get(index).getId().equals(id);
boolean flag2 = list.get(index).getPhoneNum().equals(phone);
if (!(flag1 && flag2)) {
System.out.println("账号信息不匹配,修改失败!");
return;
}
System.out.println("输入密码:");
String newPassword = sc.next();
list.get(index).setPassword(newPassword);
System.out.println("密码修改成功!");
}
public static void register(ArrayList<User> list) {
User newUser = new User();
Scanner sc = new Scanner(System.in);
System.out.println("输入用户名:");
String newUserName = sc.next();
//用户名长度必须在3~15位之间
int len = newUserName.length();
if (len < 3 || len > 15) {
System.out.println("用户名长度不符合要求!");
return;
}
//只能是字母加数字的组合
char[] charArray = newUserName.toCharArray();
int countAlpha = 0;
for (char c : charArray) {
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') {
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
countAlpha++;
break;
}
} else {
System.out.println("用户名有字母和数字以外,不符合要求!");
return;
}
}
//不能是纯数字
if (countAlpha == 0) {
System.out.println("用户名是纯数字,不符合要求!");
return;
}
//用户名唯一
if (contains(list, newUserName)) {
System.out.println("用户名已存在!");
return;
}
newUser.setUserName(newUserName);
//密码键盘输入两次,两次一致才可以进行注册。
while (true) {
System.out.println("输入密码:");
String newPassword = sc.next();
System.out.println("再次输入密码:");
String checkPassword = sc.next();
boolean equals = newPassword.equals(checkPassword);
if (equals) {
newUser.setPassword(newPassword);
break;
} else {
System.out.println("两次输入密码不同,重新输入!");
}
}
//身份证号码需要验证。
loop1:
while (true) {
System.out.println("输入身份证号码:");
String id = sc.next();
//长度为18位
if (id.length() != 18) {
System.out.println("输入身份证号码的长度不对!");
continue;
}
//不能以0为开头
if (id.charAt(0) == 0) {
System.out.println("输入身份证号码第一位不对!");
continue;
}
//前17位,必须都是数字
char[] charArrayId = id.toCharArray();
for (int i = 0; i < charArrayId.length - 1; i++) {
if (charArrayId[i] < '0' || charArrayId[i] > '9') {
System.out.println("输入身份证号码前17位不全是数字!");
continue loop1;
}
}
//最为一位可以是数字,也可以是大写X或小写x
char lastChar = id.charAt(id.length() - 1);
boolean flag = ((lastChar >= '0' && lastChar <= '9') || lastChar == 'X' || lastChar == 'x');
if (!flag) {
System.out.println("输入身份证最后一位不是数字或X或x!");
continue;
}
newUser.setId(id);
break;
}
//手机号验证。
loop2:
while (true) {
System.out.println("输入手机号码:");
String phone = sc.next();
//长度为11位
if (phone.length() != 11) {
System.out.println("手机号长度不对!");
continue;
}
//不能以0为开头
if (phone.charAt(0) == 0) {
System.out.println("手机号第一位不对!");
continue;
}
//必须都是数字
char[] charArrayPhone = phone.toCharArray();
for (int i = 0; i < charArrayPhone.length; i++) {
if (charArrayPhone[i] < '0' || charArrayPhone[i] > '9') {
System.out.println("输入身份证号码不全是数字!");
continue loop2;
}
}
newUser.setPhoneNum(phone);
break;
}
System.out.println("注册成功!");
}
public static boolean contains(ArrayList<User> list, String userName) {
return getIndex(list, userName) >= 0;
}
public static int getIndex(ArrayList<User> list, String userName) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getUserName().equals(userName)) {
return i;
}
}
return -1;
}
public static String getCheckNum() {
char[] resultChar = new char[5];
char[] arr = new char[52];
for (int i = 0; i < 26; i++) {
arr[i] = (char) ('a' + i);
arr[i + 26] = (char) ('A' + i);
}
Random r = new Random();
for (int i = 0; i < 4; i++) {
int index = r.nextInt(52);
resultChar[i] = arr[index];
}
int number = r.nextInt(10);
resultChar[4] = (char) (number + 48);
int index = r.nextInt(5);
char temp = resultChar[4];
resultChar[4] = resultChar[index];
resultChar[index] = temp;
String resultStr = new String(resultChar);
return resultStr;
}
public static boolean isRightCheckNum(String rightNum, String checkNum) {
for (int i = 0; i < rightNum.length(); i++) {
if (rightNum.charAt(i) == checkNum.charAt(i) || rightNum.charAt(i) == (checkNum.charAt(i) + 32)
|| rightNum.charAt(i) == (checkNum.charAt(i) - 32)) {
continue;
} else return false;
}
return true;
}
}
package exercise;
public class User {
private String userName;
private String password;
private String id;
private String phoneNum;
public User() {
}
public User(String userName, String password, String id, String phoneNum) {
this.userName = userName;
this.password = password;
this.id = id;
this.phoneNum = phoneNum;
}
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 getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
}