Java-学生管理系统

需求文档:

image-20231030201415628

image-20231030201641846

代码展示

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
package Management_System;

public class Stu{
private int id;
private String name;
private int age;
private String home;

public Stu() {
}

public Stu(int id, String name, int age, String home) {
this.id = id;
this.name = name;
this.age = age;
this.home = home;
}

public int getId() {
return id;
}

public void setId(int 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 getHome() {
return home;
}

public void setHome(String home) {
this.home = home;
}
}

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
package Management_System;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class stu_System {
//显示菜单
public static void MeNu(){
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("请输入你的选择:");
}

//添加学生
public static void AddStu(ArrayList<Stu>Stu1){
System.out.println("开始添加学生,请输入学生ID");
Scanner s=new Scanner(System.in);
int ID=s.nextInt();
if (checkId(Stu1,ID)&&ID>=0){//这里调用小工具1进行布尔操作
Stu Stupeople=new Stu();
Stupeople.setId(ID);
System.out.println("请输入学生姓名");
String name=s.next();
Stupeople.setName(name);
System.out.println("请输入学生年龄");
int age=s.nextInt();
Stupeople.setAge(age);
System.out.println("请输入学生家庭住址");
String home=s.next();
Stupeople.setHome(home);
Stu1.add(Stupeople);
System.out.println("信息录入完毕!!!!");
}else {
System.out.println("输入错误:你输入的ID已经记录过");
}

}

//小工具1:ID查重
public static boolean checkId(ArrayList<Stu>Stu1,int ID){
for (int i = 0; i < Stu1.size(); i++) {
if (Stu1.get(i).getId()==ID)return false;
}
return true;
}

//删除学生
public static void Delete_stu(ArrayList<Stu>Stuj){
System.out.println("请输入你要删除学生的ID");
Scanner s=new Scanner(System.in);
int ID=s.nextInt();
//调用小工具1判断集合中是否存在这个学生
if(checkId(Stuj,ID)){
System.out.println("删除失败,你要删除的学生不存在!!");
}else {
//通过for循环寻找目标ID所在的索引,并进行删除操作
for (int i = 0; i < Stuj.size(); i++) {
if (Stuj.get(i).getId()==ID)Stuj.remove(i);
}
System.out.println("学生删除成功!!!");
}
}
//修改学生信息
public static void ChangeStu(ArrayList<Stu>Stuj){
System.out.println("请输入你要修改学生的ID");
Scanner s=new Scanner(System.in);
int ID=s.nextInt();
//调用小工具1,判断目标学生存不存在
if(checkId(Stuj,ID)){
System.out.println("ID输入错误,此ID学生不存在");
}else {
//借用for循环寻找对应ID的序列
for (int i = 0; i < Stuj.size(); i++) {
if (Stuj.get(i).getId()==ID){
System.out.println("请输入修改后的学生姓名");
String name=s.next();
Stuj.get(i).setName(name);
System.out.println("请输入修改后的学生年龄");
int age=s.nextInt();
Stuj.get(i).setAge(age);
System.out.println("请输入修改后的学生家庭住址");
String home=s.next();
Stuj.get(i).setHome(home);
System.out.println("信息更新成功!!");
}
}
}
}

//查询学生信息
public static void SpellStu(ArrayList<Stu>Stuj){
//打印head
System.out.println("id\t姓名\t年龄\t家庭住址");
//for循环遍历输出
for (int i = 0; i < Stuj.size(); i++) {
System.out.print(Stuj.get(i).getId()+"\t");
System.out.print(Stuj.get(i).getName()+"\t");
System.out.print(Stuj.get(i).getAge()+"\t");
System.out.println(Stuj.get(i).getHome()+"\t");
}
}

//程序主入口
public static void main(String[] args) {
ArrayList<Stu> Stuj=new ArrayList<>();
int x=1;
//不断的循环
while (x==1){
//初始菜单
MeNu();
Scanner s=new Scanner(System.in);
byte choose=s.nextByte();
if(choose<1||choose>5) System.out.println("笨蛋!看着点菜单啊喂~~");
//选择器
switch (choose){
case 1:
AddStu(Stuj);
break;
case 2:
Delete_stu(Stuj);
break;
case 3:
ChangeStu(Stuj);
break;
case 4:
SpellStu(Stuj);
break;
case 5:
x=0;
break;
}
}
}
}

代码到这里就完了,后面会更新升级版的题目,慢慢加难度

Java-学生管理系统升级版

需求文档:

image-20231031213937893

image-20231031214145046

image-20231031214327519

image-20231031214506571

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
package login;

public class user {
private String username;
private String userpassword;
private String userID;
private String userphonenumber;

public user() {
}

public user(String username, String userpassword, String userID, String userphonenumber) {
this.username = username;
this.userpassword = userpassword;
this.userID = userID;
this.userphonenumber = userphonenumber;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getUserpassword() {
return userpassword;
}

public void setUserpassword(String userpassword) {
this.userpassword = userpassword;
}

public String getUserID() {
return userID;
}

public void setUserID(String userID) {
this.userID = userID;
}

public String getUserphonenumber() {
return userphonenumber;
}

public void setUserphonenumber(String userphonenumber) {
this.userphonenumber = userphonenumber;
}
}

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
package login;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class window {
//注册功能
public static void register(ArrayList<user>s){
user user1=new user();
System.out.println("创建你的用户名");
Scanner sc=new Scanner(System.in);
String username=sc.next();
//判断用户名是否已经注册
if (UnSameName(s,username)){
//判断usename的长度是否符合规范
if(username.length()>=3&&username.length()<=15){
//定义一个计数器count,计算数字个数
byte count=0;
//通过for循环进行字符遍历,如果是数字则加1;
for (int i = 0; i < username.length(); i++) {
if(username.charAt(i)+0>=48&&username.charAt(i)+0<=57)count++;
}
if(count==username.length()||count==0){
System.out.println("输入的姓名必须是数字和字母的混合");
}else{
user1.setUsername(username);
System.out.println("请设置你的密码");
String password=sc.next();
System.out.println("请再次输入你的密码");
String password1= sc.next();
if (password.equals(password1)){
user1.setUserpassword(password1);
System.out.println("请输入你的身份证号码");
String idcard=sc.next();
//通过if语句和小工具2判断身份证号是否符合规范
if(idcard.charAt(0)!=0&&idcard.length()==18&&check(idcard)&&check2(idcard.charAt(idcard.length()-1))){
user1.setUserID(idcard);
System.out.println("请输入你的手机号码");
String phone=sc.next();
//通过if条件句判断手机号是否符合规范
if (num(phone)&&phone.charAt(0)!=0&&phone.length()==11){
user1.setUserphonenumber(phone);
//将对象存入集合
s.add(user1);
}else {
System.out.println("手机号输入错误");
}

}else {
System.out.println("身份证号输入错误!");
}

}else {
System.out.println("两次密码输入不一致,请重新输入!!");
}

}
}else {
System.out.println("创建的用户名长度必须在3~15之间");
}
}else {
System.out.println("姓名重复");
}
}
//小工具1,判断用户名是否已经存在,存在返回false,不存在则返回true
public static boolean UnSameName(ArrayList<user>s,String name){
for (int i = 0; i < s.size(); i++) {
if (s.get(i).getUsername()==name)return false;
}
return true;
}
//小工具2,判断身份证号前17位是否都为数字
public static boolean check(String idcard){
for (int i = 0; i < idcard.length()-1; i++) {
if(idcard.charAt(i)+0<48||idcard.charAt(i)+0>57)return false;
}
return true;
}
//小工具3,对身份证号最后一位的判定
public static boolean check2(Character a){
if ((a+0>=48&&a+0<=57)||(a.equals('x'))||(a.equals('X')))return true;
return false;
}
//小工具4,判断是否都为数字
public static boolean num(String phone){
for (int i = 0; i < phone.length(); i++) {
if(phone.charAt(i)+0<48||phone.charAt(i)+0>57)return false;
}
return true;
}

//登录功能
public static void Login(ArrayList<user>s){
System.out.println("请输入用户名");
Scanner sc=new Scanner(System.in);
String username= sc.next();
//使用小工具1进行用户是否存在判断,并定义一个int型接受返回的索引
int where=userNum(s,username);
if (where==(-1)){
System.out.println("用户未注册,请先注册");
}else {
System.out.println("请输入密码");
String password=sc.next();
//进行验证码关卡,判断是否是人机,定义一个字符型接受返回的验证码
String YZM=YZM();
System.out.println("验证码:"+YZM);
System.out.println("请输入验证码");
String YZMuser=sc.next();
//进行验证码是否正确判断
if(YZMuser.equalsIgnoreCase(YZM)){
//判断密码是否跟用户名匹配
if (s.get(where).getUserpassword().equals(password)){
System.out.println("登录成功!即将进入学生管理系统");
YC();
System.out.println("欢迎管理员");
}else {
System.out.println("密码或用户名错误请检查后继续!");
}
}else {
System.out.println("验证码输入错误");
}
}
}
//小工具1,设置一个函数看用户是否存在,存在就返回相关索引
public static int userNum(ArrayList<user>s,String username){
//for循环遍历查寻集合,看是否存在此用户
for (int i = 0; i < s.size(); i++) {
if (s.get(i).getUsername().equals(username)) {
return i;
}
}
return -1;
}
//小工具2,生成五位数的验证码
public static String YZM(){
//创建一个位数的数组,存放验证码
char []arr=new char[5];
//先将四个随机字母进行组合 小写97~122 大写65~90 数字48~57
//添加随机序列
Random s=new Random();
for (int i = 0; i < arr.length-1; i++) {
int num3=s.nextInt(2);
//0就加小写 1就加大写
switch (num3){
case 0:
arr[i]=(char)(s.nextInt(26)+97);
break;
case 1:
arr[i]=(char)(s.nextInt(26)+65);
break;
}
}
//随机找一个随机数
arr[arr.length-1]=(char)(s.nextInt(10)+48);
//随机找一个索引并把随机数跟索引位置数交换,数字默认索引位置为5
int num4=s.nextInt(5);
//定义中间变量
char a=arr[num4];
arr[num4]=arr[arr.length-1];
arr[arr.length-1]=a;
//定义一个字符串接受变量
String resul=new String(arr);
return resul;
}
//小工具3,生成5位数验证码,上面的这个函数写的思路有问题,麻辣隔壁,换思路重写
public static String YZM2(){
//初始化一个字符串用于拼接
String str="";
//编写一个switch语句进行生成大写,小写和数字之间的选择
Random Sj=new Random();
//生成一个计数器,限制数字必须占一位
int count=0;
do {
while (str.length()!=5){
int choose=Sj.nextInt(3);
switch (choose){
case 0://大写
str=str+(char)(Sj.nextInt(26)+65);
break;
case 1:
str=str+(char)(Sj.nextInt(26)+97);
break;
case 2:
if (count==1)break;
str=str+Sj.nextInt(10);
count++;
break;
}
}
}while (count==0);
System.out.println(str);
return str;
}
//小工具4,延迟代码
public static void YC(){
try {
// Delay for 5 seconds
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

//忘记密码
public static void Forgotpassword(ArrayList<user>s){
System.out.println("请输入你的用户名");
Scanner sc=new Scanner(System.in);
//键盘录入用户名
String Uname= sc.next();
//进行用户是否存在判断
if(userNum(s,Uname)==(-1)){
System.out.println("用户不存在,请先注册!");
}else {
//从集合调入身份证号和手机号进行安全验证,对自己键盘录入的身份证号和手机号进行比较
System.out.println("输入你留的身份证号进行验证");
String IDcard=sc.next();
System.out.println("输入你留的手机号进行验证");
String Phone= sc.next();
//调用登录界面写的一个小工具,定义一个新变量接受返回用户名在集合中的索引
int numWhere=userNum(s,Uname);
//通过if条件句进行判断身份证号和手机号是否都输入正确
if(s.get(numWhere).getUserID().equals(IDcard)&&s.get(numWhere).getUserphonenumber().equals(Phone)){
System.out.println("验证成功!请输入新创建的密码");
String password=sc.next();
System.out.println("请再次输入你的密码");
String password1= sc.next();
if (password.equals(password1)){
//将新密码替换原本在集合中的旧记录,可以先通过返回的索引在集合中将旧密码调出来
s.get(numWhere).setUserpassword(password);
System.out.println("密码更改成功");
}else {
System.out.println("两次密码输入不一致");
}
}else {
System.out.println("输入的身份证或手机号错误,请检查后继续");
}
}

}

//测试类
public static void test(ArrayList<user>s){
//添加默认用户
user First1=new user("默认用户0","0","111111111111111111","11111111111");
//将对象录入集合
s.add(First1);
}

public static void main(String[] args) {
ArrayList<user> s=new ArrayList<>();
//进行默认用户录入
//test(s);
//System.out.println(s.size());
while (true){
System.out.println("欢迎来到学生管理系统");
System.out.println("请选择操作:1.登录 2.注册 3.忘记密码");
Scanner sc=new Scanner(System.in);
byte choose=sc.nextByte();
switch (choose){
case 1:
Login(s);
break;
case 2:
register(s);
break;
case 3:
Forgotpassword(s);
break;
default:
System.out.println("你的输入有误请重新输入!!!");
}
}
}
}

最终作品

需求

当你看到这里应该是上面的两个系统都写完了,那现在就是把他们结合起来了,

得先进行管理员用户登录才能进行学生管理

代码展示

创建用户类

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
package login;

public class user {
private String username;
private String userpassword;
private String userID;
private String userphonenumber;

public user() {
}

public user(String username, String userpassword, String userID, String userphonenumber) {
this.username = username;
this.userpassword = userpassword;
this.userID = userID;
this.userphonenumber = userphonenumber;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getUserpassword() {
return userpassword;
}

public void setUserpassword(String userpassword) {
this.userpassword = userpassword;
}

public String getUserID() {
return userID;
}

public void setUserID(String userID) {
this.userID = userID;
}

public String getUserphonenumber() {
return userphonenumber;
}

public void setUserphonenumber(String userphonenumber) {
this.userphonenumber = userphonenumber;
}
}

创建学生类

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
package login;

public class Stu {
private int id;
private String name;
private int age;
private String home;

public Stu() {
}

public Stu(int id, String name, int age, String home) {
this.id = id;
this.name = name;
this.age = age;
this.home = home;
}

public int getId() {
return id;
}

public void setId(int 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 getHome() {
return home;
}

public void setHome(String home) {
this.home = home;
}
}

编写主方法

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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package login;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class window {
//注册功能
public static void register(ArrayList<user>s){
user user1=new user();
System.out.println("创建你的用户名");
Scanner sc=new Scanner(System.in);
String username=sc.next();
//判断用户名是否已经注册
if (UnSameName(s,username)){
//判断usename的长度是否符合规范
if(username.length()>=3&&username.length()<=15){
//定义一个计数器count,计算数字个数
byte count=0;
//通过for循环进行字符遍历,如果是数字则加1;
for (int i = 0; i < username.length(); i++) {
if(username.charAt(i)+0>=48&&username.charAt(i)+0<=57)count++;
}
if(count==username.length()||count==0){
System.out.println("输入的姓名必须是数字和字母的混合");
}else{
user1.setUsername(username);
System.out.println("请设置你的密码");
String password=sc.next();
System.out.println("请再次输入你的密码");
String password1= sc.next();
if (password.equals(password1)){
user1.setUserpassword(password1);
System.out.println("请输入你的身份证号码");
String idcard=sc.next();
//通过if语句和小工具2判断身份证号是否符合规范
if(idcard.charAt(0)!=0&&idcard.length()==18&&check(idcard)&&check2(idcard.charAt(idcard.length()-1))){
user1.setUserID(idcard);
System.out.println("请输入你的手机号码");
String phone=sc.next();
//通过if条件句判断手机号是否符合规范
if (num(phone)&&phone.charAt(0)!=0&&phone.length()==11){
user1.setUserphonenumber(phone);
//将对象存入集合
s.add(user1);
}else {
System.out.println("手机号输入错误");
}

}else {
System.out.println("身份证号输入错误!");
}

}else {
System.out.println("两次密码输入不一致,请重新输入!!");
}

}
}else {
System.out.println("创建的用户名长度必须在3~15之间");
}
}else {
System.out.println("姓名重复");
}
}
//小工具1,判断用户名是否已经存在,存在返回false,不存在则返回true
public static boolean UnSameName(ArrayList<user>s,String name){
for (int i = 0; i < s.size(); i++) {
if (s.get(i).getUsername()==name)return false;
}
return true;
}
//小工具2,判断身份证号前17位是否都为数字
public static boolean check(String idcard){
for (int i = 0; i < idcard.length()-1; i++) {
if(idcard.charAt(i)+0<48||idcard.charAt(i)+0>57)return false;
}
return true;
}
//小工具3,对身份证号最后一位的判定
public static boolean check2(Character a){
if ((a+0>=48&&a+0<=57)||(a.equals('x'))||(a.equals('X')))return true;
return false;
}
//小工具4,判断是否都为数字
public static boolean num(String phone){
for (int i = 0; i < phone.length(); i++) {
if(phone.charAt(i)+0<48||phone.charAt(i)+0>57)return false;
}
return true;
}

//登录功能
public static void Login(ArrayList<user>s){
System.out.println("请输入用户名");
Scanner sc=new Scanner(System.in);
String username= sc.next();
//使用小工具1进行用户是否存在判断,并定义一个int型接受返回的索引
int where=userNum(s,username);
if (where==(-1)){
System.out.println("用户未注册,请先注册");
}else {
System.out.println("请输入密码");
String password=sc.next();
//进行验证码关卡,判断是否是人机,定义一个字符型接受返回的验证码
String YZM=YZM();
System.out.println("验证码:"+YZM);
System.out.println("请输入验证码");
String YZMuser=sc.next();
//进行验证码是否正确判断
if(YZMuser.equalsIgnoreCase(YZM)){
//判断密码是否跟用户名匹配
if (s.get(where).getUserpassword().equals(password)){
System.out.println("登录成功!即将进入学生管理系统");
YC();
System.out.println("欢迎管理员"+s.get(where).getUsername());
//进行学生管理功能植入
ArrayList<Stu> Stuj=new ArrayList<>();
int x=1;
//不断的循环
while (x==1){
//初始菜单
MeNu();
Scanner si=new Scanner(System.in);
byte choose=si.nextByte();
if(choose<1||choose>5) System.out.println("笨蛋!看着点菜单啊喂~~");
//选择器
switch (choose){
case 1:
AddStu(Stuj);
break;
case 2:
Delete_stu(Stuj);
break;
case 3:
ChangeStu(Stuj);
break;
case 4:
SpellStu(Stuj);
break;
case 5:
x=0;
break;
}
}
}else {
System.out.println("密码或用户名错误请检查后继续!");
}
}else {
System.out.println("验证码输入错误");
}
}
}
//小工具1,设置一个函数看用户是否存在,存在就返回相关索引
public static int userNum(ArrayList<user>s,String username){
//for循环遍历查寻集合,看是否存在此用户
for (int i = 0; i < s.size(); i++) {
if (s.get(i).getUsername().equals(username)) {
return i;
}
}
return -1;
}
//小工具2,生成五位数的验证码
public static String YZM(){
//创建一个位数的数组,存放验证码
char []arr=new char[5];
//先将四个随机字母进行组合 小写97~122 大写65~90 数字48~57
//添加随机序列
Random s=new Random();
for (int i = 0; i < arr.length-1; i++) {
int num3=s.nextInt(2);
//0就加小写 1就加大写
switch (num3){
case 0:
arr[i]=(char)(s.nextInt(26)+97);
break;
case 1:
arr[i]=(char)(s.nextInt(26)+65);
break;
}
}
//随机找一个随机数
arr[arr.length-1]=(char)(s.nextInt(10)+48);
//随机找一个索引并把随机数跟索引位置数交换,数字默认索引位置为5
int num4=s.nextInt(5);
//定义中间变量
char a=arr[num4];
arr[num4]=arr[arr.length-1];
arr[arr.length-1]=a;
//定义一个字符串接受变量
String resul=new String(arr);
return resul;
}
//小工具3,生成5位数验证码,上面的这个函数写的思路有问题,麻辣隔壁,换思路重写
public static String YZM2(){
//初始化一个字符串用于拼接
String str="";
//编写一个switch语句进行生成大写,小写和数字之间的选择
Random Sj=new Random();
//生成一个计数器,限制数字必须占一位
int count=0;
do {
while (str.length()!=5){
int choose=Sj.nextInt(3);
switch (choose){
case 0://大写
str=str+(char)(Sj.nextInt(26)+65);
break;
case 1:
str=str+(char)(Sj.nextInt(26)+97);
break;
case 2:
if (count==1)break;
str=str+Sj.nextInt(10);
count++;
break;
}
}
}while (count==0);
System.out.println(str);
return str;
}
//小工具4,延迟代码
public static void YC(){
try {
// Delay for 5 seconds
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

//忘记密码
public static void Forgotpassword(ArrayList<user>s){
System.out.println("请输入你的用户名");
Scanner sc=new Scanner(System.in);
//键盘录入用户名
String Uname= sc.next();
//进行用户是否存在判断
if(userNum(s,Uname)==(-1)){
System.out.println("用户不存在,请先注册!");
}else {
//从集合调入身份证号和手机号进行安全验证,对自己键盘录入的身份证号和手机号进行比较
System.out.println("输入你留的身份证号进行验证");
String IDcard=sc.next();
System.out.println("输入你留的手机号进行验证");
String Phone= sc.next();
//调用登录界面写的一个小工具,定义一个新变量接受返回用户名在集合中的索引
int numWhere=userNum(s,Uname);
//通过if条件句进行判断身份证号和手机号是否都输入正确
if(s.get(numWhere).getUserID().equals(IDcard)&&s.get(numWhere).getUserphonenumber().equals(Phone)){
System.out.println("验证成功!请输入新创建的密码");
String password=sc.next();
System.out.println("请再次输入你的密码");
String password1= sc.next();
if (password.equals(password1)){
//将新密码替换原本在集合中的旧记录,可以先通过返回的索引在集合中将旧密码调出来
s.get(numWhere).setUserpassword(password);
System.out.println("密码更改成功");
}else {
System.out.println("两次密码输入不一致");
}
}else {
System.out.println("输入的身份证或手机号错误,请检查后继续");
}
}

}

//测试类
public static void test(ArrayList<user>s){
//添加默认用户
user First1=new user("默认用户0","0","111111111111111111","11111111111");
//将对象录入集合
s.add(First1);
}

//插入学生管理的各项函数
/*````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````*/
//显示菜单
public static void MeNu(){
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("请输入你的选择:");
}

//添加学生
public static void AddStu(ArrayList<Stu>Stu1){
System.out.println("开始添加学生,请输入学生ID");
Scanner s=new Scanner(System.in);
int ID=s.nextInt();
if (checkId(Stu1,ID)&&ID>=0){//这里调用小工具1进行布尔操作
Stu Stupeople=new Stu();
Stupeople.setId(ID);
System.out.println("请输入学生姓名");
String name=s.next();
Stupeople.setName(name);
System.out.println("请输入学生年龄");
int age=s.nextInt();
Stupeople.setAge(age);
System.out.println("请输入学生家庭住址");
String home=s.next();
Stupeople.setHome(home);
Stu1.add(Stupeople);
System.out.println("信息录入完毕!!!!");
}else {
System.out.println("输入错误:你输入的ID已经记录过");
}

}

//小工具1:ID查重
public static boolean checkId(ArrayList<Stu>Stu1,int ID){
for (int i = 0; i < Stu1.size(); i++) {
if (Stu1.get(i).getId()==ID)return false;
}
return true;
}

//删除学生
public static void Delete_stu(ArrayList<Stu>Stuj){
System.out.println("请输入你要删除学生的ID");
Scanner s=new Scanner(System.in);
int ID=s.nextInt();
//调用小工具1判断集合中是否存在这个学生
if(checkId(Stuj,ID)){
System.out.println("删除失败,你要删除的学生不存在!!");
}else {
//通过for循环寻找目标ID所在的索引,并进行删除操作
for (int i = 0; i < Stuj.size(); i++) {
if (Stuj.get(i).getId()==ID)Stuj.remove(i);
}
System.out.println("学生删除成功!!!");
}
}
//修改学生信息
public static void ChangeStu(ArrayList<Stu>Stuj){
System.out.println("请输入你要修改学生的ID");
Scanner s=new Scanner(System.in);
int ID=s.nextInt();
//调用小工具1,判断目标学生存不存在
if(checkId(Stuj,ID)){
System.out.println("ID输入错误,此ID学生不存在");
}else {
//借用for循环寻找对应ID的序列
for (int i = 0; i < Stuj.size(); i++) {
if (Stuj.get(i).getId()==ID){
System.out.println("请输入修改后的学生姓名");
String name=s.next();
Stuj.get(i).setName(name);
System.out.println("请输入修改后的学生年龄");
int age=s.nextInt();
Stuj.get(i).setAge(age);
System.out.println("请输入修改后的学生家庭住址");
String home=s.next();
Stuj.get(i).setHome(home);
System.out.println("信息更新成功!!");
}
}
}
}

//查询学生信息
public static void SpellStu(ArrayList<Stu>Stuj){
//打印head
System.out.println("id\t姓名\t\t年龄\t家庭住址");
//for循环遍历输出
for (int i = 0; i < Stuj.size(); i++) {
System.out.print(Stuj.get(i).getId()+"\t");
System.out.print(Stuj.get(i).getName()+"\t");
if(Stuj.get(i).getName().length()<3) System.out.print("\t");
System.out.print(Stuj.get(i).getAge()+"\t");
System.out.println(Stuj.get(i).getHome()+"\t");
}
}

//程序主入口
public static void main(String[] args) {
ArrayList<user> s=new ArrayList<>();
//进行默认用户录入
test(s);
//System.out.println(s.size());
while (true){
System.out.println("欢迎来到学生管理系统");
System.out.println("请选择操作:1.登录 2.注册 3.忘记密码");
Scanner sc=new Scanner(System.in);
byte choose=sc.nextByte();
switch (choose){
case 1:
Login(s);
break;
case 2:
register(s);
break;
case 3:
Forgotpassword(s);
break;
default:
System.out.println("你的输入有误请重新输入!!!");
}
}
}
}

更新于

请我喝[茶]~( ̄▽ ̄)~*

罗梓丰 微信支付

微信支付

罗梓丰 支付宝

支付宝