学生管理系统

前言:突然想起来要记录一下自己之前做的小”项目“。基于Java的学生管理系统及其升级版。
此项目是由小白自己亲手写完未有copy成分的第一个项目,所以意义非凡!
欢迎clone我的GitHub仓库!
https://github.com/Antarctica000/Student-Management-System.git

大体需求:

初级:学生管理系统要实现最基本的增删查改功能。
升级:为学生管理系统书写一个登陆、注册、忘记密码的功能。只有用户登录成功之后,才能进入到学生管理系统中进行增删查改操作。

初级学生管理系统:

一、搭建主菜单(添加Student类)

需求:

  • Student类属性:姓名、ID、年龄、家庭住址
  • 初始菜单显示四大功能选项(增删查改)

代码:

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
//Student类
public class Student {
private String name;
private int ID;
private int age;
private String home;

public Student() {}

public Student(String name, int ID, int age, String home) {
this.name = name;
this.ID = ID;
this.age = age;
this.home = home;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
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;
}//一次性展示学生信息
public void showAll() {
System.out.printf("%-10s %-10s %-10s %-10s%n", "id", "name", "age", "home");
System.out.println();
System.out.printf("%-10d %-10s %-10d %-10s%n", getID(), getName(), getAge(), getHome());
}//一次性输入学生信息
public void setAll() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter name:");
setName(sc.next());
System.out.println("Enter age:");
setAge(sc.nextInt());
System.out.println("Enter home:");
setHome(sc.next());
System.out.println("Enter ID:");
setID(sc.nextInt());
}
}
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
public class StudentSystem {
//改成final常量增加可读性
private static final String ADD_STU = "1";
private static final String DELETE_STU = "2";
private static final String REVISE_STU = "3";
private static final String ENQUIRE_STU = "4";
private static final String EXIT = "5";

static ArrayList<Student> list = new ArrayList<>();
static {
list.add(new Student("juju", 3, 18, "Antarctica"));
}

public static void startStudentSystem() {
//主菜单
while (true) {
//初始菜单
menu();
//选择功能
Scanner sc = new Scanner(System.in);
String option = sc.next();//代码容错率,若用户输入不是数字..
if (choose(option, list))
break;
}
}
//初始菜单
public static void menu() {
System.out.println("-------------Welcome to Student System-------------");
System.out.println("1:Add Student");
System.out.println("2:Delete Student");
System.out.println("3:revise Student");
System.out.println("4:enquire Student");
System.out.println("5:Exit ");
System.out.println("Enter your option");
}
//选择功能
public static boolean choose(String option, ArrayList<Student> list) {
switch (option) {
case ADD_STU:
System.out.println("Add student:");
add(list);
break;
case DELETE_STU:
System.out.println("Delete student:");
delete(list);
break;
case REVISE_STU:
System.out.println("Revise student:");
revise(list);
break;
case ENQUIRE_STU:
System.out.println("Enquire student:");
enquire(list);
break;
case EXIT:
System.out.println("Exit");
return true;
default:
System.out.println("Invalid operation");
}
return false;
}

二、查询功能

1
2
3
4
5
6
7
8
9
10
11
//查询功能
public static void enquire(ArrayList<Student> list) {
if (list.isEmpty())
System.out.println("No student");
else {
for (int i = 0; i < list.size(); i++) {
list.get(i).showAll();
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
//添加功能
public static void add(ArrayList<Student> list) {
//录入信息
Student s = new Student();
while (true) {
s.setAll();
int x = s.getID();
if (judgeID(list, x)) {
list.add(s);
break;
} else
System.out.println("ID already exist! Again:");
}
System.out.println("Option 1 OK");
}
///判断ID是否存在
public static boolean judgeID(ArrayList<Student> list, int x) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getID() == x)
return false;
}
return true;
}

四、删除功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//删除功能
public static void delete(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the ID:");
int x = sc.nextInt();
if (!judgeID(list, x)) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getID() == x)
list.remove(i);
}
System.out.println("Option 2 OK");
} else
System.out.println("ID not exist!");
}

五、修改功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//修改功能
public static void revise(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the ID:");
int x = sc.nextInt();
if (!judgeID(list, x)) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getID() == x)
list.get(i).setAll();
}
System.out.println("Option 3 OK");
} else
System.out.println("ID not exist!");
}

升级学生管理系统:

一、登录界面(添加User类)

需求:

  • User类属性:用户名、密码、身份证号码、手机号码

代码:

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 APP {
//静态代码块修饰,让它在方法开始后只被调用一次
static ArrayList<User> list = new ArrayList<>();
static {
list.add(new User("juju000", "1", "111111111111111111", "11111111111"));
}

public static void main(String[] args) {
//选择功能
while (true) {
menu();
Scanner sc = new Scanner(System.in);
String option = sc.next();
if (choose(list, option))
break;
}
}
//登录界面
public static void menu() {
System.out.println("-------------Welcome to Student System!-------------");
System.out.println("1:Sign in");
System.out.println("2:Register");
System.out.println("3:Forget password");
System.out.println("4:Exit");
System.out.println("Enter your choose:");
}
//选择功能
public static boolean choose(ArrayList<User> list, String option) {
switch (option) {
case "1":
System.out.println("Sign in:");
signIn(list);
break;
case "2":
System.out.println("Register:");
register(list);
break;
case "3":
System.out.println("Forget password:");
forgetPassword(list);
break;
case "4":
System.out.println("Exit");
return true;
default:
System.out.println("Invalid option !");
}
return false;
}

二、注册功能

需求:

  • 用户名:
    用户名唯一
    用户名长度必须在3~15位之间
    只能是字母加数字的组合,但是不能是纯数字

  • 密码输入两次一致才可以进行注册。

  • 身份证号码:
    长度为18位
    不能以0为开头
    前17位,必须都是数字
    最为一位可以是数字,也可以是大写X或小写x

  • 手机号验证。
    长度为11位
    不能以0为开头
    必须都是数字

代码:

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
//注册功能
public static void register(ArrayList<User> list) {
Scanner sc = new Scanner(System.in);
User u = new User();
list.add(u);
//用户名判断
while (true) {
System.out.println("Enter name:");
String name = sc.next();
u.setName(name);///???研究清楚此类错误!
if (judgeName(list, name)) {
u.setName(name);
break;
}
System.out.println("Invalid! Again:");
}
//密码输入两次
while (true) {
String p1, p2;
System.out.println("Enter password");
p1 = sc.next();
System.out.println("Enter password again:");
p2 = sc.next();
u.setPassword(p1);
if (p1.equals(p2)) {
u.setPassword(p1);
break;
}
System.out.println("Not same! Again:");
}
//身份证号码验证
while (true) {
System.out.println("Enter ID:");
String ID = sc.next();
u.setID(ID);
if (judgeID(ID)) {
u.setID(ID);
break;
}
System.out.println("Invalid! Again:");
}
//手机号验证
while (true) {
System.out.println("Enter phone:");
String p = sc.next();
u.setPhone(p);
if (judgePhone(p)) {
u.setPhone(p);
break;
}
System.out.println("Invalid! Again:");
}
System.out.println("Option 1 OK");
}
///用户名判断
public static boolean judgeName(ArrayList<User> list, String name) {
//长度3~15位
int length = name.length();
if (length < 3 || length > 15)
return false;
//必须要字母加数字(?)48-57,65-90,97-122
char[] n = name.toCharArray();
boolean isLetter = false;
boolean isNum = false;
for (int i = 0; i < length; i++) {
if (n[i] < 48 || (n[i] > 57 && n[i] < 65) || (n[i] > 90 && n[i] < 97) || n[i] > 122)
return false;
if (n[i] > 57)
isLetter = true;
if (n[i] <= 57)
isNum = true;
}
if (!(isLetter && isNum))
return false;
//用户名唯一
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i).getName().equals(name))
return false;
}
return true;
}
///ID判断
public static boolean judgeID(String ID) {
//长度18
if (ID.length() != 18)
return false;
//不能0开头
if (ID.charAt(0) == '0')
return false;
//前17位必须数字
char[] ch = ID.toCharArray();
for (int i = 0; i < ID.length() - 1; i++) {
if (ch[i] < 48 || ch[i] > 57)
return false;
}
//最后一位必须是数字或者大X小x
if (!((ch[17] >= 48 && ch[17] <= 57) || ch[17] == 'X' || ch[17] == 'x'))
return false;
return true;
}
///手机号判断
public static boolean judgePhone(String p) {
//长度11
if (p.length() != 11)
return false;
//不能0开头
if (p.charAt(0) == '0')
return false;
//必须数字
char[] ch = p.toCharArray();
for (int i = 0; i < p.length(); i++) {
if (ch[i] < 48 || ch[i] > 57)
return false;
}
return true;
}

三、登录功能

需求:

  • 键盘录入用户名、密码、验证码

  • 用户名如果未注册,直接结束并提示:用户名未注册,请先注册

  • 判断验证码是否正确,如不正确,重新输入

  • 最后判断用户名和密码是否正确,有3次机会

  • 验证码:
    长度为5
    由4位大写或者小写字母和1位数字组成,同一个字母可重复
    数字可以出现在任意位置

代码:

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
//登录功能
public static void signIn(ArrayList<User> list) {
Scanner sc = new Scanner(System.in);
//输入并判断用户名是否已存在
String name = checkUser(list);
if (name.isEmpty())
return;
//生成并判断验证码是否正确
while (true) {
String s1 = getCode();
System.out.println("The code:" + s1);
System.out.println("Enter the code:");
String s2 = sc.next();
if (s1.equals(s2))
break;
System.out.println("Wrong! Again:");
}
//判断用户名和密码是否正确
int count = 0;
loop:
while (true) {
count++;
System.out.println("Enter the password:");
String password = sc.next();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(name)) {
if (list.get(i).getPassword().equals(password))
break loop;//一定要看清楚!循环跳错了导致一大堆错误
}
}
System.out.println("Wrong! Only " + (3 - count) + " times!");
if (count == 3)
return;
}
System.out.println("Option 2 OK");
StudentSystem ss = new StudentSystem();
ss.startStudentSystem();
}
///输入并判断用户名是否存在
public static String checkUser(ArrayList<User> list) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name:");
String name = sc.next();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(name))
return name;
}
System.out.println("no exit!Register first!");
return "";
}
///生成验证码
public static String getCode() {
ArrayList<Character> list = new ArrayList<>();
StringBuilder sb = new StringBuilder();
Random r = new Random();
for (int i = 0; i < 26; i++) {
list.add((char) ('a' + i));
list.add((char) ('A' + i));
}
for (int i = 0; i < 4; i++) {
int x = r.nextInt(52);
sb.append(list.get(x));
}
int x = r.nextInt(10);
int index = r.nextInt(5);
sb.insert(index, x);
return sb.toString();
}

四、忘记密码

需求:

  • 键盘录入用户名,若不存在,直接结束并提示:未注册

  • 键盘录入身份证号码和手机号码

  • 判断当前用户的身份证号码和手机号码是否一致,如果一致,则提示输入密码,进行修改。如果不一致,则提示:账号信息不匹配,修改失败。

代码:

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
//忘记密码
public static void forgetPassword(ArrayList<User> list) {
Scanner sc = new Scanner(System.in);
//判断用户名是否存在
String name = checkUser(list);
if (name.isEmpty())
return;
//判断手机号与身份证是否一致
System.out.println("Enter phone:");
String phone = sc.next();
System.out.println("Enter ID:");
String ID = sc.next();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(name)) {
if (list.get(i).getPhone().equals(phone) && list.get(i).getID().equals(ID)) {
System.out.println("Enter password:");
list.get(i).setPassword(sc.next());
break;
} else {
System.out.println("Failed!");
return;
}
}
}
System.out.println("Option 3 OK");
}