# 需求
接口的应用案例:班级学生信息管理模块的开发
- 请设计一个班级学生的信息管理模块:学生的数据有:姓名、性别、成绩
- 功能一:要求打印出全班学生的信息
- 功能二:要求打印出全班学生的平均成绩
注意!以上功能的业务实现是有多套方案的,如下:
- 方案一:能打印出班级全部学生的信息;能打印班级全部学生的平均分
- 方案二:能打印出班级全部学生的信息(包含男女人数);能打印班级全部学生的平均分(要求是去掉最高分、最低分)
要求:系统可以支持灵活的切换这些实现方案
# 代码实现
# 学生类
描述学生相关信息
public class Student { | |
private String name; | |
private char sex; | |
private double score; | |
public Student() { | |
} | |
public Student(String name, char sex, double score) { | |
this.name = name; | |
this.sex = sex; | |
this.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; | |
} | |
} |
# 接口
学生信息管理系统的两个功能
import java.util.ArrayList; | |
public interface StudentOperator { | |
void printAllInfo(ArrayList<Student> students); | |
void printAverageScore(ArrayList<Student> students); | |
} |
# 实现类
方案一的实现类
import java.util.ArrayList; | |
public class StudentOperatorImpl1 implements StudentOperator { | |
@Override | |
public void printAllInfo(ArrayList<Student> students) { | |
System.out.println("---------------全班学生信息---------------"); | |
for (int i = 0; i < students.size(); i++) { | |
Student s = students.get(i); | |
System.out.println("姓名:" + s.getName() + ",性别:" + s.getSex() + ",成绩:" + s.getScore()); | |
} | |
System.out.println("----------------------------------------"); | |
} | |
@Override | |
public void printAverageScore(ArrayList<Student> students) { | |
double allScore = 0.0; | |
for (int i = 0; i < students.size(); i++) { | |
Student s = students.get(i); | |
allScore += s.getScore(); | |
} | |
System.out.println("全班的平均分:" + allScore / students.size()); | |
} | |
} |
方案二的实现类
import java.util.ArrayList; | |
public class StudentOperatorImpl2 implements StudentOperator { | |
@Override | |
public void printAllInfo(ArrayList<Student> students) { | |
int girl_num = 0; | |
int boy_num = 0; | |
System.out.println("---------------全班学生信息---------------"); | |
for (int i = 0; i < students.size(); i++) { | |
Student s = students.get(i); | |
System.out.println("姓名:" + s.getName() + ",性别:" + s.getSex() + ",成绩:" + s.getScore()); | |
if (s.getSex() == '女') { | |
girl_num++; | |
} else { | |
boy_num++; | |
} | |
} | |
System.out.println("男生人数:" + boy_num + ",女生人数:" + girl_num); | |
System.out.println("班级总人数:" + students.size()); | |
System.out.println("----------------------------------------"); | |
} | |
@Override | |
public void printAverageScore(ArrayList<Student> students) { | |
double allScore = 0.0; | |
double maxScore = students.get(0).getScore(); | |
double minScore = students.get(0).getScore(); | |
for (int i = 0; i < students.size(); i++) { | |
Student s = students.get(i); | |
allScore += s.getScore(); | |
if (s.getScore() > maxScore) maxScore = s.getScore(); | |
if (s.getScore() < minScore) minScore = s.getScore(); | |
} | |
System.out.println("学生的最高分:" + maxScore + ",最低分:" + minScore); | |
System.out.println("全班的平均分:" + (allScore - maxScore - minScore) / (students.size() - 2)); | |
} | |
} |
# 班级管理类
import java.util.ArrayList; | |
public class ClassManager { | |
private ArrayList<Student> students = new ArrayList<>(); | |
private StudentOperator op = new StudentOperatorImpl1(); | |
// private StudentOperator op = new StudentOperatorImpl2(); | |
public ClassManager() { | |
students.add(new Student("呆呆", '女', 99)); | |
students.add(new Student("朱呆呆", '女', 96)); | |
students.add(new Student("朱朱", '男', 100)); | |
students.add(new Student("呆鸭", '女', 90)); | |
students.add(new Student("哈哈", '男', 80)); | |
} | |
public void printInfo() { | |
op.printAllInfo(students); | |
} | |
public void printScore() { | |
op.printAverageScore(students); | |
} | |
} |
# 测试类
public class Test { | |
public static void main(String[] args) { | |
ClassManager classManager = new ClassManager(); | |
classManager.printInfo(); | |
classManager.printScore(); | |
} | |
} |