主函数:
import
StudentPackageshengjiban.Student;
import
StudentPackageshengjiban.User;
import
java.nio.channels.ScatteringByteChannel;
import
java.util.ArrayList;
import
java.util.Random;
import
java.util.Scanner;
public class
testUser {
public static void
main(String[] args) {
ArrayList<Student> list1 =
new
ArrayList<>();
ArrayList<User> list =
new
ArrayList<>();
loop1:
while
(
true
) {
System.
out
.println(
“————-欢迎来到学生管理系统——–”
);
System.
out
.println(
“请输入你的选择”
);
System.
out
.println(
“【1】登陆 【2】注册 【3】忘记密码 【输入其他】退出”
);
Scanner sc =
new
Scanner(System.
in
);
//输入选择的数字
int
shuzi = sc.nextInt();
switch
(shuzi) {
case
1:
System.
out
.println(
“请输入用户名”
);
String user = sc.next();
//判断是否成功登陆
boolean
u =
sign_in
(list, user);
//登陆成功返回true
if
(u) {
System.
out
.println(
“登陆成功”
);
loop2:
while
(
true
){
System.
out
.println(
“———–欢迎进入学生管理系统———”
);
System.
out
.println(
“【1】添加学生信息 【2】删除学生信息”
);
System.
out
.println(
“【3】修改学生信息 【4】查看学生信息”
);
System.
out
.println(
“【5】退出系统”
);
System.
out
.println(
“请输入你的选择:”
);
int
n = sc.nextInt();
switch
(n){
case
1:
//添加学生信息
System.
out
.println(
“请输入学生id:”
);
String id = sc.next();
add_student
(list1,id);
System.
out
.println(
“添加成功”
);
break
;
case
2:
//删除学生信息
System.
out
.println(
“请输入需要删除的id号:”
);
String id2 = sc.next();
shanchu_student
(list1,id2);
break
;
case
3:
//修改学生信息
System.
out
.println(
“请输入需要修改的学生id”
);
String id3 = sc.next();
xiugai_student
(list1,id3);
break
;
case
4:
//查看学生信息
System.
out
.println(
“请输入学生id:”
);
String id1 = sc.next();
find_student
(list1,id1);
break
;
default
:
//退出系统
break
loop1;
}
}
}
else
{
break
;
}
case
2:
System.
out
.println(
“请输入用户名”
);
String user1 = sc.next();
boolean
a =
register
(list, user1);
if
(a) {
//验证码判断
boolean
y =
chansheng_yanzhengma
();
if
(y) {
System.
out
.println(
“恭喜你,注册成功”
);
break
;
}
else
{
System.
out
.println(
“验证码输入错误”
);
}
}
else
{
System.
out
.println(
“注册失败”
);
break
;
}
case
3:
System.
out
.println(
“请输入用户名:”
);
String m = sc.next();
boolean
b =
forget
(list, m);
//密码修改成功返回true,失败返回false
if
(b) {
System.
out
.println(
“密码修改成功”
);
break
;
}
else
{
System.
out
.println(
“密码修改失败”
);
break
;
}
default
:
break
loop1;
}
}
}
//用户登陆操作
public static boolean
sign_in(ArrayList<User> list, String user) {
boolean
flag =
check_user
(list, user);
Scanner sc =
new
Scanner(System.
in
);
//true为用户名存在,继续登陆
if
(flag) {
System.
out
.println(
“请输入密码:”
);
String mima = sc.next();
//返回密码判断
boolean
m =
check_password
(list, user, mima);
//判断密码是否正确,正确返回true
if
(m) {
System.
out
.println(
“请输入身份证号:”
);
String id_number = sc.next();
//判断身份证号
Boolean i =
check_id_number
(list, user, id_number);
//如果身份证号正确,返回true
if
(i) {
System.
out
.println(
“请输入手机号:”
);
String phone_number = sc.next();
//判断手机号是否正确
boolean
p =
check_phone_number
(list, user, phone_number);
//如果手机号正确返回true
if
(p) {
return true
;
}
//如果手机号错误,返回false
else
{
System.
out
.println(
“手机号输入错误”
);
return false
;
}
}
//如果身份证号错误,返回false
else
{
System.
out
.println(
“身份证号输入错误”
);
return false
;
}
}
//如果密码错误,返回false
else
{
System.
out
.println(
“密码输入错误”
);
return false
;
}
}
//false为用户名不存在,请先注册
else
{
System.
out
.println(
“用户名不存在,请先注册”
);
return false
;
}
}
//查看用户名是否唯一
public static boolean
check_user(ArrayList<User> list, String user) {
boolean
flag =
false
;
for
(
int
i = 0; i < list.size(); i++) {
if
(list.get(i).getUser().equals(user)) {
//用户名存在返回true
flag =
true
;
}
}
//循环结束,返回false,用户名不存在
return
flag;
}
//判断密码是否正确
public static boolean
check_password(ArrayList<User> list, String user, String password) {
for
(
int
i = 0; i < list.size(); i++) {
//用用户名去寻找索引
if
(user.equals(list.get(i).getUser())) {
if
(password.equals(list.get(i).getPassword())) {
//判断密码是否正确,如果正确返回true
return true
;
}
}
}
//如果密码错误,返回false
return false
;
}
//判断身份证号是否正确
public static boolean
check_id_number(ArrayList<User> list, String user, String id_number) {
for
(
int
i = 0; i < list.size(); i++) {
//用用户名去寻找索引
if
(user.equals(list.get(i).getUser())) {
if
(id_number.equals(list.get(i).getId_number())) {
//判断身份证号是否正确,如果正确返回true
return true
;
}
}
}
//如果身份证号错误,返回false
return false
;
}
//判断手机号是否正确
public static boolean
check_phone_number(ArrayList<User> list, String user, String phone_number) {
for
(
int
i = 0; i < list.size(); i++) {
//用用户名去寻找索引
if
(user.equals(list.get(i).getUser())) {
if
(phone_number.equals(list.get(i).getPhone_number())) {
//判断手机号是否正确,如果正确返回true
return true
;
}
}
}
//如果手机号错误,返回false
return false
;
}
//用户注册
public static boolean
register(ArrayList<User> list, String user) {
User s =
new
User();
Scanner sc =
new
Scanner(System.
in
);
boolean
flag =
check_user
(list, user);
//判断用户名是否以及唯一存在,存在返回true,不存在返回false
if
(flag) {
System.
out
.println(
“用户名已经存在,请重新注册”
);
return false
;
}
//用户名不存在,开始注册
else
{
//判断用户名是否规范
//1、用户名唯一
//2、长度为3-15之间
//3、只能是数字加字母的组合,不能是存数字
boolean
u =
check_long_user
(user);
//如果字符串长度在2-15之间,返回true
if
(u) {
//判断是否是数字加字母的组合
boolean
a =
pan_user_number
(user);
boolean
b =
pan_number_string
(user);
//如果a,b都为true,说明数字和字母都存在
if
(a && b) {
//将用户名存入集合
s.setUser(user);
//用户名输入成功
//接下来输入密码
System.
out
.println(
“请设置密码:”
);
String password1 = sc.next();
//两次输入一致
System.
out
.println(
“请再次输入密码:”
);
String password2 = sc.next();
//判断两次输入是否一致
if
(password1.equals(password2)) {
//密码输入成功,存入集合
s.setPassword(password1);
//判断身份证是否符合
//长度为18,不能以零开头
//前十七为必须都是数字
//最后一位可以是数字或者大小写x
System.
out
.println(
“请输入身份证号:”
);
String id_number = sc.next();
//判断身份证号长度
if
(id_number.length() == 18) {
//判断第一位是否为零
if
(id_number.charAt(0) – 48 == 0) {
System.
out
.println(
“身份证号第一位不能为零”
);
return false
;
}
else
{
//判断身份证号前十七位以及最后一位
boolean
id =
pan_id_number
(id_number);
//如果符合,返回true
if
(id) {
//身份证号符合,将其存入集合
s.setId_number(id_number);
//判断手机号是否符合
System.
out
.println(
“请输入手机号:”
);
String phone_number = sc.next();
//长度为11,不能以0开头
//必须都是数字
//判断手机号长度
if
(phone_number.length() == 11) {
//判断手机号第一位
char
c = phone_number.charAt(0);
if
((c – 48) == 0) {
System.
out
.println(
“输入错误,手机号第一位不能为零”
);
return false
;
}
else
{
//手机号输入正确,存入集合
s.setPhone_number(phone_number);
list.add(s);
return true
;
}
}
else
{
System.
out
.println(
“手机号长度输入错误”
);
return false
;
}
}
else
{
System.
out
.println(
“身份证格式错误”
);
return false
;
}
}
}
else
{
System.
out
.println(
“身份证号长度输入错误,请重新操作”
);
return false
;
}
}
else
{
System.
out
.println(
“两次输入密码不一致,请重新操作”
);
return false
;
}
}
else
{
System.
out
.println(
“用户名输入不规范,应该是数字加字母的组合”
);
return false
;
}
}
else
{
return false
;
}
}
}
//判断用户名长度
public static boolean
check_long_user(String user) {
int
long_user = user.length();
if
(long_user >= 3 && long_user <= 15) {
return true
;
}
else if
(long_user < 3) {
System.
out
.println(
“用户名长度小于3,不符合规范,请重新输入”
);
return false
;
}
else
{
System.
out
.println(
“用户名长度大于15,不符合规范,请重新输入”
);
return false
;
}
}
//判断用户名中是否含有数字
public static boolean
pan_user_number(String user) {
for
(
int
i = 0; i < user.length(); i++) {
char
c = user.charAt(i);
int
n = c – 48;
if
(n >= 0 && n <= 9) {
//存在数字返回true
return true
;
}
}
//循环结束,都没有找到数字,返回false
return false
;
}
//判断用户名中是否含有字母
public static boolean
pan_number_string(String user) {
for
(
int
i = 0; i < user.length(); i++) {
char
c = user.charAt(i);
if
((c >=
‘a’
&& c <=
‘z’
) || (c >=
‘A’
&& c <=
‘Z’
)) {
//判断是否存在数字,如果存在返回true
return true
;
}
}
//如果不存在,返回false
return false
;
}
//判断身份证号前十七位是否都为数字
//判断身份证号最后一位
public static boolean
pan_id_number(String id_number) {
boolean
flag =
false
;
for
(
int
i = 0; i < id_number.length() – 1; i++) {
flag =
false
;
char
c = id_number.charAt(i);
int
n = c – 48;
if
(n >= 0 && n <= 9) {
//如果前十七位为数字,flag等于true
flag =
true
;
}
}
if
(flag) {
flag =
false
;
char
c1 = id_number.charAt(id_number.length() – 1);
if
(((c1 – 48) >= 0 && (c1 – 48) <= 9) || c1 ==
‘X’
|| c1 ==
‘x’
) {
flag =
true
;
}
}
return
flag;
}
//忘记密码功能
public static boolean
forget(ArrayList<User> list, String user) {
Scanner sc =
new
Scanner(System.
in
);
boolean
flag =
check_user
(list, user);
//用户名存在返回true,不存在返回false
if
(flag) {
//用身份证号和手机号修改密码
System.
out
.println(
“请输入身份证号:”
);
String id_number = sc.next();
System.
out
.println(
“请输入手机号:”
);
String phone_number = sc.next();
boolean
a1 =
check_id_number
(list, user, id_number);
boolean
a2 =
check_phone_number
(list, user, phone_number);
if
(a1) {
if
(a2) {
//身份证号和手机号都输入正确,开始修改密码
for
(
int
i = 0; i < list.size(); i++) {
if
(list.get(i).getId_number().equals(id_number)) {
//用身份证号去寻找索引
System.
out
.println(
“请输入修改后的密码:”
);
String password1 = sc.next();
System.
out
.println(
“请再次确定你修改的密码:”
);
String password2 = sc.next();
if
(password1.equals(password2)) {
list.get(i).setPassword(password1);
return true
;
}
else
{
System.
out
.println(
“两次输入的密码不一样”
);
return false
;
}
}
}
}
}
else
{
System.
out
.println(
“身份证号或者手机号输入错误”
);
return false
;
}
}
System.
out
.println(
“该用户名未注册”
);
return false
;
}
//产生验证码
public static boolean
chansheng_yanzhengma() {
Scanner sc =
new
Scanner(System.
in
);
Random r =
new
Random();
String result =
“”
;
String s =
“qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM”
;
for
(
int
i = 0; i < 5; i++) {
int
n = r.nextInt(53);
char
c = s.charAt(n);
result = result + c;
}
int
number = r.nextInt(10);
result = result + number;
//打乱顺序
result =
daluan
(result);
System.
out
.println(
“验证码为:”
+ result);
System.
out
.println(
“请输入验证码:”
);
String yanzhengma = sc.next();
if
(yanzhengma.equals(result)) {
System.
out
.println(
“验证码输入成功”
);
return true
;
}
else
{
return false
;
}
}
//打乱顺序
public static
String daluan(String result) {
Random r =
new
Random();
char
[] arr =
new char
[result.length()];
for
(
int
i = 0; i < result.length(); i++) {
arr[i] = result.charAt(i);
}
//产生一个随机数,利用索引交换位置
int
n = r.nextInt(5);
char
temp = arr[n];
arr[n] = arr[result.length() – 1];
arr[result.length() – 1] = temp;
String result1 =
“”
;
for
(
int
i = 0; i < arr.
length
; i++) {
result1 = result1 + arr[i];
}
return
result1;
}
//添加学生信息
public static void
add_student(ArrayList<Student> list,String id){
Scanner sc =
new
Scanner(System.
in
);
boolean
flag =
check_id
(list,id);
Student s =
new
Student();
//id存在返回true
//id不存在返回false
if
(flag){
System.
out
.println(
“当前添加id已经存在,请重新操作”
);
return
;
}
else
{
//存入id
s.setId(id);
System.
out
.println(
“请输入学生姓名:”
);
String name = sc.next();
s.setName(name);
System.
out
.println(
“请输入学生学号:”
);
String School_number = sc.next();
s.setShoolnumber(School_number);
System.
out
.println(
“请输入学生家庭住址:”
);
String address = sc.next();
s.setAddress(address);
list.add(s);
return
;
}
}
//查询id是否存在
public static boolean
check_id(ArrayList<Student> list,String id){
for
(
int
i = 0; i < list.size(); i++) {
if
(id.equals(list.get(i).getId())){
//id存在返回true
return true
;
}
}
//id不存在返回false
return false
;
}
//查看学生信息
public static void
find_student(ArrayList<Student> list,String id){
boolean
flag =
true
;
for
(
int
i = 0; i < list.size(); i++) {
if
(id.equals(list.get(i).getId())){
System.
out
.println(
“学生id为”
+ list.get(i).getId());
System.
out
.println(
“学生姓名为”
+ list.get(i).getName());
System.
out
.println(
“学生学号为”
+ list.get(i).getShoolnumber());
System.
out
.println(
“学生家庭住址为”
+ list.get(i).getAddress());
return
;
}
else
{
flag =
false
;
}
}
if
(!flag){
System.
out
.println(
“该id不存在”
);
}
}
//删除学生信息
public static void
shanchu_student(ArrayList<Student> list,String id){
//查看id是否存在,存在返回true,不存在返回false
boolean
flag =
check_id
(list,id);
if
(flag){
for
(
int
i = 0; i < list.size(); i++) {
if
(id.equals(list.get(i).getId())) {
list.remove(i);
return
;
}
}
}
else
{
System.
out
.println(
“该id不存在”
);
return
;
}
}
//修改学生信息
public static void
xiugai_student(ArrayList<Student> list,String id){
Scanner sc =
new
Scanner(System.
in
);
boolean
flag =
check_id
(list,id);
if
(flag){
for
(
int
i = 0; i < list.size(); i++) {
if
(id.equals(list.get(i).getId())){
System.
out
.println(
“请输入修改后的学生id:”
);
String new_id = sc.next();
list.get(i).setId(new_id);
System.
out
.println(
“请输入修改后的学生姓名:”
);
String new_name = sc.next();
list.get(i).setName(new_name);
System.
out
.println(
“请输入修改后的学生学号:”
);
String new_school_number = sc.next();
list.get(i).setShoolnumber(new_school_number);
System.
out
.println(
“请输入修改后的学生家庭住址:”
);
String new_adress = sc.next();
list.get(i).setAddress(new_adress);
return
;
}
}
}
else
{
System.
out
.println(
“该id不存在”
);
return
;
}
}
}
用户类函数:
package
StudentPackageshengjiban;
public class
User {
private
String
user
;
private
String
password
;
private
String
id_number
;
private
String
phone_number
;
public
User() {
}
public
User(String user, String password, String id_number, String phone_number) {
this
.
user
= user;
this
.
password
= password;
this
.
id_number
= id_number;
this
.
phone_number
= phone_number;
}
public
String getUser() {
return user
;
}
public void
setUser(String user) {
this
.
user
= user;
}
public
String getPassword() {
return password
;
}
public void
setPassword(String password) {
this
.
password
= password;
}
public
String getId_number() {
return id_number
;
}
public void
setId_number(String id_number) {
this
.
id_number
= id_number;
}
public
String getPhone_number() {
return phone_number
;
}
public void
setPhone_number(String phone_number) {
this
.
phone_number
= phone_number;
}
}
学生类函数:
package
StudentPackageshengjiban;
public class
Student {
private
String
id
;
private
String
name
;
private
String
shoolnumber
;
private
String
address
;
public
Student() {
}
public
Student(String id, String name, String shoolnumber, String address) {
this
.
id
= id;
this
.
name
= name;
this
.
shoolnumber
= shoolnumber;
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
String getShoolnumber() {
return shoolnumber
;
}
public void
setShoolnumber(String shoolnumber) {
this
.
shoolnumber
= shoolnumber;
}
public
String getAddress() {
return address
;
}
public void
setAddress(String address) {
this
.
address
= address;
}
}
运行结果:
