个人写作项目:班级学生的信息管理模块
请设计一个班级学生的信息管理模块:学生的数据由:姓名、性别、成绩
功能1:要求打印处全班学生的信息
功能2:要求打印出全班学生的平均成绩
注意:有两套方案
第一套:能打印出全部学生信息,能打印全班学生的平均分
第二套:能打印出全部学生信息(包含男女人数),能打印全班学生的平均分(要求去掉最高分、最低分)
要求:系统能狗灵活支持切换这些实现方案
测试类:
public static void main(String[] args) {
ClassManager clazz = new ClassManager();
clazz.printInfo();
clazz.printScore();
}
参与类:
/**
* @program: Day02
* @ClassName ClassManager
* @description(类信息): 班级管理类
* @author: honor
* @create: 2024-05-10 21:02
* @Version 1.0
**/
public class ClassManager {
private ArrayList<Student> students = new ArrayList<>();
//接口创建实现类对象实现多态
private StudentOperator studentOperator = new StudentOperatorImpl2();
public ClassManager() {
students.add(new Student("上善若水", '男', 19.6));
students.add(new Student("上善若", '男', 12));
students.add(new Student("上善水", '女', 1.6));
students.add(new Student("上若水", '男', 129.6));
students.add(new Student("上善水", '女', 14.6));
students.add(new Student("上善若", '女', 16.6));
}
//以下方法容易将方法写死,不利于未来修改
/**
* @return void
* @ClassName ClassManager
* @anthor 上善若水
* @description (方法信息):打印全班全部学生方法信息
* @Date 2024/5/10 21:27
* @Param []
*/
public void printInfo() {
studentOperator.printAllInfo(students);
}
/**
* @return void
* @ClassName ClassManager
* @anthor 上善若水
* @description (方法信息):打印全班全部学生的平均分
* @Date 2024/5/10 21:28
* @Param []
*/
public void printScore() {
studentOperator.printAverageScore(students);
}
}
/**
* @program: Day02
* @ClassName Student
* @description(类信息): 学生类
* @author: honor
* @create: 2024-05-10 20:59
* @Version 1.0
**/
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;
}
}
/**
* @program: Day02
* @ClassName StudentaOpertorImpI1
* @description(类信息): 实现类1号(后面带Impl是实现类的一种写法, 后面的数字代表套数)
* @author: honor
* @create: 2024-05-10 21:35
* @Version 1.0
**/
public class StudentOperatorImpl1 implements StudentOperator {
@Override
public void printAllInfo(ArrayList<Student> students) {
System.out.println("\t\t\t以下是全班全部学生的信息");
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println("姓名:" + s.getName() + "\t性别:" + s.getSex() + "\t成绩:" + 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());
}
}
/**
* @program: Day02
* @ClassName StudentOperatorImpl2
* @description(类信息): 实现类2
* @author: honor
* @create: 2024-05-10 21:48
* @Version 1.0
**/
public class StudentOperatorImpl2 implements StudentOperator {
@Override
public void printAllInfo(ArrayList<Student> students) {
System.out.println("\t\t\t以下是全班全部学生的信息");
int count = 0;
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println("姓名:" + s.getName() + "\t性别" + s.getSex() + "\t成绩:" + s.getScore());
if (s.getSex() == '男') count++;
}
System.out.println("男生人数为:" + count + "\t女生人数为:" + (students.size() - count));
System.out.println("班级总人数为:" + students.size());
System.out.println("-------------------------------------------");
}
@Override
public void printAverageScore(ArrayList<Student> students) {
double allScore = 0.0;
double max = students.get(0).getScore();
double min = students.get(1).getScore();
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
if (s.getScore() > max) max = s.getScore();
if (s.getScore() < min) min = s.getScore();
allScore += s.getScore();
}
System.out.println("最高分为:" + max + "最低分为:" + min);
System.out.print("平均分为:" + (allScore - max - min) / (students.size() - 2));
}
}
接口:
/**
* @program: Day02
* @InterfaceName StudentOperator
* @description(接口信息): 学生接口
* @author: honor
* @create: 2024-05-10 21:29
* @Version 1.0
**/
public interface StudentOperator {
void printAllInfo(ArrayList<Student> students);
void printAverageScore(ArrayList<Student> students);
}
思路:
制作接口作为模板,生成子类进行重写方法实现功能
主在类中使用接口创建实现类对象,方便转换
最后再测试类中调用主类得到结果
问题:
问题主要在于思维,刚接触接口并不太熟悉写法与运用,需要多加加强练习进行学习,力求达到简洁方便明了,增强对方法注释与类中成员的习惯
总结:
中规中矩的试水作,生疏之处较多,需要加强对于算法优化的理解,适当用空间来换取时间与代码简洁