Java学生管理系统

只是为了练习接口设计的一个学生系统小案例,算不上真正的学生管理系统.

需求说明

概要设计

image-20240117173051203

具体实现

image-20240117174122286

CodeDemo

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
// showAll 接口
public interface ShowAll {
void printInfo();
}

// showAvg 接口
public interface ShowAvg {
void showAvg();
}

// A类
import java.util.ArrayList;

public class A implements ShowAll, ShowAvg{
private ArrayList<Student> list = new ArrayList<>();
public A(){
Student s1 = new Student("张三", '男', 99);
Student s2 = new Student("李四", '女', 88);
Student s3 = new Student("王五", '男', 95);
list.add(s1);
list.add(s2);
list.add(s3);
}
@Override
public void printInfo() {
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println("学生姓名:" +student.getName()+"\t学生性别:"+student.getSex()+"\t学生成绩:" + student.getScore());
}
}

@Override
public void showAvg() {
double sum = 0;
int count = 0;
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
sum += student.getScore();
count ++;
}
System.out.println("平均成绩为:" + sum/count);

}
}

//B类
import java.util.ArrayList;

public class B implements ShowAvg, ShowAll{
private ArrayList<Student> list = new ArrayList<>();
public B(){
Student s1 = new Student("张三", '男', 99);
Student s2 = new Student("李四", '女', 88);
Student s3 = new Student("王五", '男', 95);
list.add(s1);
list.add(s2);
list.add(s3);
}
@Override
public void printInfo() {
int maleNum = 0;
int femalNum = 0;
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (student.getSex() == '男'){
maleNum ++;
}else if (student.getSex() == '女'){
femalNum ++;
}
System.out.println("学生姓名:" +student.getName()+"\t学生性别:"+student.getSex()+"\t学生成绩:" + student.getScore());
}
System.out.println("班级中男生人数为:"+maleNum + "\t女生人数为:" + femalNum);
}

@Override
public void showAvg() {
Student student = list.get(0);
double minScore = student.getScore();
double maxScore = student.getScore();
double sum = student.getScore();
int count = 1;
for (int i = 1; i < list.size(); i++) {
Student student1 = list.get(i);
if (student1.getScore() < minScore){
minScore = student1.getScore();
}else if (student1.getScore() > maxScore){
maxScore = student1.getScore();
}
sum += student1.getScore();
count ++;
}
System.out.println("去掉最高分,去掉最低分,全班学生的平均成绩为:" + (sum - minScore - maxScore)/ (count-2));
}
}


// Student类是标准的JavaBean
public class Student {
private String name;
private char sex;
private double score;

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex=" + sex +
", score=" + score +
'}';
}

public String getName() {
return name;
}

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

public char getSex() {
return sex;
}

public void setSex(char sex) {
this.sex = sex;
}

public double getScore() {
return score;
}

public void setScore(double score) {
this.score = score;
}

public Student(String name, char sex, double score) {
this.name = name;
this.sex = sex;
this.score = score;
}

public Student() {
}
}


// Test类
public class Test {
public static void main(String[] args) {
B a = new B();
a.printInfo();
a.showAvg();
}
}