学生信息管理系统

实体类

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 User {
private String userName;
private String password;
private String personId;

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;
}

public User(String userName, String password, String personId, String phoneNumber) {
this.userName = userName;
this.password = password;
this.personId = personId;
this.phoneNumber = phoneNumber;
}

private String phoneNumber;
}
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
public class Student {
private String id;
private String name;
private int age;
private String 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;
}

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

public Student() {
}
}

登录注册忘记密码功能

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class app {
public static void main(String[] args) {
//先创建一个学生集合
ArrayList<User> list = new ArrayList<>();
loop:
while (true) {
System.out.println("—————————————————欢迎登使用管理系统————————————————");
System.out.print(" 1.登录 ");
System.out.print(" 2.注册 ");
System.out.print(" 3.忘记密码 ");
System.out.println(" 4.退出");
System.out.print("请输入你的选择:");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose) {
case "1": {
login(list);
break;
}
case "2": {
register(list);
break;
}
case "3": {
forgetPassword(list);
break;
}
case "4": {
System.out.println("退出系统");
break loop;
}
default:
System.out.println("输入无效,请重新输入");
}
}
}

private static void forgetPassword(ArrayList<User> list) {
System.out.println("忘记密码");
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();
//定义函数比较用户对象中的信息
int index = findUserInfo(list, username);
User user = list.get(index);
//比较用户对象中的信息
if (!(personID.equalsIgnoreCase(user.getPersonId()) && phoneNumber.equals(user.getPhoneNumber()))) {
System.out.println("身份证号码或手机号错误,不能修改密码");
return;
}
//重置密码
String password;
while (true) {
System.out.println("请输入新的密码");
password = sc.next();
System.out.println("请再次输入密码");
String againPassword = sc.next();
if (!(password.equals(againPassword))) {
System.out.println("两次密码不一致,请重新输入");
} else {
System.out.println("两次密码一致,请继续输入其他信息");
break;
}
}
user.setPassword(password);
System.out.println("密码修改成功");
}

private static int findUserInfo(ArrayList<User> list, String username) {
for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
if (username.equals(u.getUserName())) {
return i;
}
}
return -1;
}

private static void register(ArrayList<User> list) {
System.out.println("注册");
//先录入用户信息
Scanner sc = new Scanner(System.in);
String userName;
String password;
String personID;
String phoneNumber;
while (true) {
System.out.println("请输入用户名:");
userName = sc.next();
boolean flag = checkName(userName);
if (!flag) {
System.out.println("用户名" + userName + "格式不正确,请重新输入");
continue;
}
//校验用户名是否唯一
boolean b = contains(list, userName);
if (b) {
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("两次密码不一致,请重新输入");
} else {
System.out.println("两次密码一致,请继续输入其他信息");
break;
}
}


//校验身份证信息,长度18,不能以0开头,最后一位是数字也可以是大写X小写x,前17位必须是数字
while (true) {
System.out.println("请输入身份证信息");
personID = sc.next();
boolean pID = checkPersonId(personID);
if (pID) {
System.out.println("身份证号码满足要求,请继续输入");
break;

} else {
System.out.println("身份证信息不满足要求,请重新输入");
}
}
//手机号码
while (true) {
System.out.println("请输入手机号码");
phoneNumber = sc.next();
boolean phone = checkPhoneNumber(phoneNumber);
if (phone) {
System.out.println("手机号正确,请继续输入");
break;
} else {
System.out.println("手机号格式不正确,请重新输入");
}
}
User user = new User(userName, password, personID, phoneNumber);
list.add(user);
System.out.println("注册成功");
printList(list);
}

private static void printList(ArrayList<User> list) {
for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
System.out.println("用户名:" + u.getUserName() + " 密码:" + u.getPassword() + " 身份证:" +
u.getPersonId() + " 手机号:" + u.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 ph = phoneNumber.charAt(i);
if (!(ph >= '0' && ph <= '9')) {
return false;
}
}
return true;
}


private static boolean checkPersonId(String personID) {
//长度18
if (personID.length() != 18) {
return false;
}
//不能0开头
if (personID.startsWith("0")) {
return false;
}
//前17位为数字
for (int i = 0; i < personID.length() - 1; i++) {
char c = personID.charAt(i);
if (!(c >= '0' && c <= '9')) {
return false;
}
}
//最后一位校验
char endchar = personID.charAt(personID.length() - 1);
if (((endchar >= '0' && endchar <= '9') || (endchar == 'X') || (endchar == 'x'))) {
return true;
} else {
return false;
}
}

private static boolean contains(ArrayList<User> list, String userName) {
//判断用户名是否唯一
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
if (user.getUserName().equals(userName)) {
return true;
}
}
return false;
}

private static boolean checkName(String username) {
//用户名必须在3-15位长度之间
int length = username.length();
if (length < 3 || length > 15) {
return false;
}
//只能是数字加字母
for (int i = 0; i < username.length(); i++) {
char ch = username.charAt(i);
//真草了忘记加等号就说出bug
if (!((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) {
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<User> list) {
System.out.println("登录");
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) {
System.out.println("请输入验证码");
String code = getCode();
System.out.println("验证码为: " + code);
String usercode = sc.next();
if (code.equalsIgnoreCase(usercode)) {
System.out.println("验证码正确");
break;
} else {
System.out.println("验证码错误,请重新输入");
}
}
//判断用户名和密码是否正确
User userInfo = new User(username, password, null, null);
boolean info = checkUserInfo(list, userInfo);
if (info) {
System.out.println("登录成功,开始使用");
StudentManagement.StartMenu();
break;
} else {
// System.out.println("登录失败,用户名或密码错误");
if (i == 2) {
System.out.println("账户为:" + username + "已被锁定,请联系管理员π!");
} else {
System.out.println("还剩" + (2 - i) + "次机会");
}
}
}
}

private static boolean checkUserInfo(ArrayList<User> list, User userInfo) {
for (int i = 0; i < list.size(); i++) {
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) (i + 'a'));
//添加大写字母
list.add((char) (i + 'A'));
}
//随机生成4个字符
Random r = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
//获取索引
int index = r.nextInt(list.size());
//将随机索引的值存入
char c = list.get(index);
sb.append(c);
}
int number = r.nextInt(10);
sb.append(number);
//将字符串转化为字符数组
char[] arr = sb.toString().toCharArray();
//打乱
for (int i = 0; i < arr.length; i++) {
int index = r.nextInt(arr.length);
char temp = arr[index];
arr[index] = arr[arr.length - 1];
arr[arr.length - 1] = temp;
}
return new String(arr);
}
}

主界面

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
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagement {
public static void StartMenu() {
//先创建一个学生集合
ArrayList<Student> list = new ArrayList<>();
loop:
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("请输入你的选择:");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose) {
case "1": {
addStudent(list);
break;
}
case "2": {
deleteStudent(list);
break;
}
case "3": {
updateStudent(list);
break;
}
case "4": {
queryStudent(list);
break;
}
case "5": {
System.out.println("退出系统");
break loop;
}
default:
System.out.println("输入无效,请重新输入");
}
}
}

//添加
public static void addStudent(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
Student st = new Student();
System.out.println("添加学生");
while (true) {
System.out.println("请输入id");
String id = sc.next();
boolean flag = contains(list, id);
if (flag) {
System.out.println("当前ID已存在");
} else {
st.setId(id);
break;
}
}
System.out.println("请输入姓名");
st.setName(sc.next());
System.out.println("请输入年龄");
st.setAge(sc.nextInt());
System.out.println("请输入地址");
st.setAddress(sc.next());
list.add(st);
System.out.println("添加成功");
}

//删除
public static void deleteStudent(ArrayList<Student> list) {
System.out.println("删除学生");
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要删除学生的ID");
String index = sc.next();
int i = getindex(list, index);
if (i >= 0) {
list.remove(i);
System.out.println("id为" + index + "的学生删除成功");
} else {
System.out.println("删除失败,学生信息不存在");
}
}

//修改
public static void updateStudent(ArrayList<Student> list) {
System.out.println("修改学生");
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要修改学生的id");
String id = sc.next();
int index = getindex(list, id);
if (index == -1) {
System.out.println("学生信息不存在,请添加!");
return;
}
Student stu = list.get(index);
System.out.println("请输入需要修改的姓名:");
String name = sc.next();
stu.setName(name);
System.out.println("请输入需要修改的年龄:");
int age = sc.nextInt();
stu.setAge(age);
System.out.println("请输入需要修改的地址:");
String address = sc.next();
stu.setAddress(address);
}

//查询
public static void queryStudent(ArrayList<Student> list) {
System.out.println("查询学生");
if (list.size() == 0) {
System.out.println("当前没有学生,请添加好后查询");
return;
}
//打印表头信息
System.out.println("id\t姓名\t年龄\t地址");
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
}
}

//判断id在集合中是否存在
public static boolean contains(ArrayList<Student> list, String id) {
// for (int i = 0; i < list.size(); i++) {
// Student st=list.get(i);
// if(st.getId().equals(id)){
// return true;
// }
// }
// return false;
int index = getindex(list, id);
return index >= 0;
}

//获取索引
public static int getindex(ArrayList<Student> list, String id) {
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (student.getId().equals(id)) {
return i;
}
}
return -1;
}
}