学生管理系统

学生管理系统:增删改查

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
static int choiceAdmin;
static Scanner sc = new Scanner(System.in);
static ArrayList<User> arrayListUser = new ArrayList<>();
static ArrayList<Student> arrayListStudent = new ArrayList<>();

public static void main(String[] args) {
while (true) {
interfaceMain();
}
}

public static void interfaceMain() {
System.out.println("-------------欢迎来到学生管理系统----------------");
System.out.println("请选择你的身份");
System.out.println("1.学生");
System.out.println("2.管理员");
System.out.println("请输入你的选择:");
choiceAdmin = sc.nextInt();
if (choiceAdmin == 1) {
interfaceUser();
} else if (choiceAdmin == 2) {
interfaceUser();
} else {
System.out.println("输入错误,请重新输入");
}
}

public static void interfaceStudent() {
System.out.println("----------------------------------------------");
System.out.println("请选择你的操作");
System.out.println("1.查看个人信息");
System.out.println("2.修改个人信息");
System.out.println("3.退出");
System.out.println("请输入你的选择:");
}

public static void interfaceAdmin() {
System.out.println("----------------------------------------------");
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("请输入你的选择:");
}

public static void interfaceUser() {
System.out.println("----------------------------------------------");
System.out.println("请选择你的操作");
System.out.println("1.登录");
System.out.println("2.注册");
System.out.println("3.忘记密码");
System.out.println("4.退出");
System.out.println("请输入你的选择:");
int choice = sc.nextInt();
switch (choice) {
case 2:
register();
break;
}
}

//注册
public static boolean register() {
User user = new User();
//选择身份
if (choiceAdmin == 1) {
user.setCharacter(1);
} else if (choiceAdmin == 2) {
user.setCharacter(2);
} else {
System.out.println("输入错误,请重新输入");
}
//验证用户名
String name;
while (true) {
System.out.println("请输入你的用户名:");
name = sc.next();
if (checkUserName(arrayListUser, name)) {
break;
} else {
System.out.println("用户名不符合规范,请重新输入");
}
}
user.setName(name);
//验证密码
String password1, password2;
while (true) {
System.out.println("请输入你的密码:");
password1 = sc.nextLine();
System.out.println("请再次输入你的密码:");
password2 = sc.nextLine();
if (password2.equals(password1)) {
user.setPassword(password1);
break;
}
System.out.println("两次密码不一致,请重新输入");
}
//验证身份证
String identity_id;
while (true) {
System.out.println("请输入你的身份证号:");
identity_id = sc.nextLine();
if (checkIdentityId(identity_id)) {
break;
} else {
System.out.println("身份证号不符合规范,请重新输入");
}
}
user.setIdentity_id(identity_id);
//验证手机号
String phone;
while (true) {
System.out.println("请输入你的手机号:");
phone = sc.nextLine();
if (checkPhone(phone)) {
break;
}
System.out.println("手机号不符合规范,请重新输入");
}
return true;
}

//验证注册的用户名
public static boolean checkUserName(ArrayList<User> ListUser, String name) {
if (name.length() < 3 || name.length() > 15) {
return false;//用户名长度不符合
}
for (int i = 0; i < ListUser.size(); i++) {
if (ListUser.get(i).getName().equals(name)) {
return false;//用户名已存在
}
}
for (int i = 0; i < name.length(); i++) {
if (name.charAt(i) < '0' || name.charAt(i) > '9') {
return false;//用户名包含非法字符
}
}
return true;//用户名符合规范
}

//验证注册的身份证号
public static boolean checkIdentityId(String identity_id) {
if (identity_id.length() != 18) {
return false;//身份证号长度不符合
}
if (identity_id.charAt(0) == 0) {
return false;//身份证号首位为0
}
for (int i = 0; i < 17; i++) {
if (identity_id.charAt(i) < '0' || identity_id.charAt(i) > '9') {
return false;//身份证号包含非法字符
}
}
if (identity_id.charAt(17) < '0' || identity_id.charAt(17) > '9' || identity_id.charAt(17) != 'X' || identity_id.charAt(17) != 'x') {
return false;//身份证号最后一位不是数字
}
return true;//身份证号符合规范
}

//验证注册的手机号
public static boolean checkPhone(String phone) {
if (phone.length() != 11) {
return false;//手机号长度不符合
}
if (phone.charAt(0) != '1') {
return false;//手机号首位不是1
}
for (int i = 0; i < 11; i++) {
if (phone.charAt(i) < '0' || phone.charAt(i) > '9') {
return false;//手机号包含非法字符
}
}
return true;//手机号符合规范
}
}

Student.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class Student {
private String name;
private char gender;
private int age;
private int id;

public Student() {
}

public Student(String name, char gender, int age,int id) {
this.name = name;
this.gender = gender;
this.age = age;
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public char getGender() {
return gender;
}

public void setGender(char gender) {
this.gender = gender;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}

User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class User {
private String name;
private String password;
private int character;//1.学生 2.管理员
private String identity_id;
private String phone;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public int getCharacter() {
return character;
}

public void setCharacter(int character) {
this.character = character;
}

public User() {
}

public User(String name, String password, int character, String identity_id, String phone) {
this.name = name;
this.password = password;
this.character = character;
this.identity_id = identity_id;
this.phone = phone;
}

public String getIdentity_id() {
return identity_id;
}

public void setIdentity_id(String identity_id) {
this.identity_id = identity_id;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}
}