public class Test {
public static void main(String[] args) {
Student stu1 = new Student("张三","10001",98);
Student stu2 = new Student("李四","10002",96);
Student stu3 = new Student("王五","10003",99);
List<Student> list = new ArrayList<Student>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
Student stu4 = new Student("赵六","10004",98);
Student stu5 = new Student("田七","10005",96);
Student stu6 = new Student("周八","10006",99);
List<Student> list2 = new ArrayList<Student>();
list2.add(stu4);
list2.add(stu5);
list2.add(stu6);
ClassRoom cr1 = new ClassRoom(list);
ClassRoom cr2 = new ClassRoom(list);
Map<String,ClassRoom> map = new HashMap<String,ClassRoom>();
map.put("班级1", cr1);
map.put("班级2", cr2);
Set<Entry<String,ClassRoom>> entrySet = map.entrySet();
for(Entry<String,ClassRoom> entry:entrySet){
double totalScore = 0;
double avgScore = 0;
int stuCount = 0;
ArrayList<Student> studentList = (ArrayList<Student>) entry.getValue().getStuList();
for (Student student : studentList) {
totalScore += student.getScore();
stuCount++;
}
System.out.println(entry.getKey()+"的总分数为: "+totalScore);
avgScore = totalScore / stuCount;
System.out.println(entry.getKey()+"的平均分数为: "+avgScore);
}
}
}
class ClassRoom{
private List<Student> stuList;
public List<Student> getStuList() {
return stuList;
}
public void setStuList(ArrayList<Student> stuList) {
this.stuList = stuList;
}
public ClassRoom(List<Student> stuList) {
super();
this.stuList = stuList;
}
public ClassRoom() {
super();
}
}
class Student{
private String name;
private String no;
private double score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public Student() {
super();
}
public Student(String name, String no, double score) {
super();
this.name = name;
this.no = no;
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", no=" + no + ", score=" + score + "]";
}
} |