您的位置:首页 > 其它

分拣存储2-统计学生每个班级的总分和平均分

2016-04-16 14:23 399 查看
题目要求:定义一个Student类,属性有,name名字,no班级,score成绩,现在将若干不同班级的Student对象放入List中,统计每个班的总分和平均分

题目分析:首先需要一个student类是毋庸置疑的,采用javabean模式设计一个student类,接下来就是选择集合容器来装对象了,我们可以选择List〈student〉来装student对象,然后利用泛型嵌套把List〈student〉嵌套到Map中,

即Map〈String,List〈student〉〉,Map中的key存放班级,List〈student〉存放对应班级的学生,但是这样操作的时候会很不方便,把student对象放在List集合中需要不断的遍历list,所以我们采用另外一种方式,创建一个新的类,ClassRoom,这就是一个班级类,属性有班级和学生列表,以及总成绩

代码实现:

/**
* 学生类
* @author wwyDEPP
*
*/
public class Student {
private String name;
private String no;
private double score;
public Student() {

}
public Student(String name, String no, double score) {
super();
this.name = name;
this.no = no;
this.score = 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;
}

}


import java.util.ArrayList;
import java.util.List;
/**
* 班级类
* @author wwyDEPP
*
*/
public class ClassRoom {
private String no;
private List<Student> stuList;
private double total;
public ClassRoom() {
//避免出现空指针异常
stuList=new ArrayList<Student>();
}
public ClassRoom(String no) {
this.no=no;
//避免出现空指针异常
stuList=new ArrayList<Student>();
}
public ClassRoom(String no, List<Student> stuList, double total) {
super();
this.no = no;
this.stuList = stuList;
this.total = total;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public List<Student> getStuList() {
return stuList;
}
public void setStuList(List<Student> stuList) {
this.stuList = stuList;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}

}


核心代码:

把给出的Student对象添加到List集合中后,利用Map集合,key是班级,value是ClassRoom类对象,封装了班级总分和学生对象List
Map<String,ClassRoom> map =new HashMap<String,ClassRoom>();


接下来就需要便利存放student对象的list,进行分班,获取学生对象的班级和成绩属性

String no=stu.getNo();  double score=stu.getScore();


这里就遇到了分拣存储一对多的问题,一个班级对应很多个学生,像在之前博客分拣存储中提到的快递问题一样,我们也要判断有没有这个班级编号,有的话往里面添加数据,没有的话就新创建一个,然后计算总分

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Mytest {
public static void main(String[] args) {
Mytest test=new Mytest();
test.view(test.count(test.exam()));
}

//模拟考试输入学生成绩
public List<Student> exam(){
List<Student> list=new ArrayList<Student>();
list.add(new Student("depp","a",90));
list.add(new Student("depp","a",90));
list.add(new Student("depp","a",90));
list.add(new Student("jack","b",70));
return list;
}

//统计分析数据
public Map<String,ClassRoom> count(List<Student> list){
Map<String,ClassRoom> map =new HashMap<String,ClassRoom>();
for(Student stu:list){
String no=stu.getNo();
double score=stu.getScore();
ClassRoom room=map.get(no);
//不存在班级编号,新建一个
if(room==null){
room=new ClassRoom(no);
map.put(no, room);
}
//存在则放入
room.getStuList().add(stu);
//计算总分
room.setTotal(room.getTotal()+score);
}
return map;
}

//查看班级总分平均分
public void view(Map<String,ClassRoom> map){
Set<String> keys=map.keySet();
Iterator<String> keysIt=keys.iterator();
while(keysIt.hasNext()){
String no=keysIt.next();
ClassRoom room=map.get(no);
double total=room.getTotal();
double avg=Math.round(room.getTotal()/room.getStuList().size());
System.out.println("total="+total+",avg="+avg);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: