Java学习记录
Wucheng

一段时间没有更新,为了装作自己正在努力学习随便放点东西

2022.3.9更新:
添加了继承的使用

简易控制台版学生管理系统

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

public class Student {
private String id;
private String name;
private String age;

public Student() {
}

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

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 getAge() {
return age;
}

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

StudentsManager.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
package test;

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

public class StudentsManager {
static Scanner scan = new Scanner(System.in);

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

public static boolean choose(ArrayList<Student> students, boolean OK) {
String choose = scan.nextLine();
switch (choose) {
case "1":
addStudent(students);
break;
case "2":
setStudent(students);
break;
case "3":
delStudent(students);
break;
case "4":
allStudent(students);
break;
case "5":
OK = false;
break;
default:
System.out.println("没有该选项!");
}
return OK;
}

public static void addStudent(ArrayList<Student> students) {
do {
System.out.println("请输入学号:");
String id = scan.nextLine();
id = isTure(students, id);
System.out.println("请输入姓名:");
String name = scan.nextLine();
System.out.println("请输入年龄:");
String age = scan.nextLine();

if (id.length() == 0 || name.length() == 0 || age.length() == 0) {
System.out.println("有数据未输入!");
return;
} else {
Student student = new Student(id, name, age);
students.add(student);
System.out.println("是否继续录入(y/n)");
}
} while (scan.nextLine().equals("y"));
}

private static String isTure(ArrayList<Student> students, String id) {
Student student;
for (int i = 0; i < students.size(); i++) {
student = students.get(i);
while (student.getId().equals(id)) {
System.out.println("学号重复!请重新输入:");
id = scan.nextLine();
}
}
return id;
}

public static void setStudent(ArrayList<Student> students) {
System.out.println("请输入要修改的学生学号:");
String id = scan.nextLine();

Student student;

for (int i = 0; i < students.size(); i++) {
student = students.get(i);
if (student.getId().equals(id)) {
System.out.println("请输入新学号:");
String new_id = scan.nextLine();
id = isTure(students, new_id);
System.out.println("请输入新姓名:");
String name = scan.nextLine();
System.out.println("请输入新年龄:");
String age = scan.nextLine();

student.setId(new_id);
student.setName(name);
student.setAge(age);

students.set(i, student);
return;
}
}
System.out.println("未找到该学生学号,请检查是否正确!");
}

public static void delStudent(ArrayList<Student> students) {
System.out.println("请输入要删除的学生学号:");
String id = scan.nextLine();

Student student;

for (int i = 0; i < students.size(); i++) {
student = students.get(i);
if (student.getId().equals(id)) {
students.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该学生学号,请检查是否正确!");
}

public static void allStudent(ArrayList<Student> students) {
Student student;
System.out.println("学号\t姓名\t\t年龄");
if (students.size() == 0) {
System.out.println("没有学生信息!");
return;
}
for (int i = 0; i < students.size(); i++) {
student = students.get(i);
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t");
}
}
}

Manager.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.ArrayList;

public class Manager extends StudentsManager{
static boolean OK = true;
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
while (OK){
userUI();
OK = choose(students,OK);
}
System.out.println("感谢使用!");
}
}