package exercise;
import java.util.ArrayList;
public class Student {
private String name;
private String no;
private double score;
public Student(){
}
public Student( String no) {
super();
this.no = no;
}
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;
}
}
package exercise;
import java.util.ArrayList;
import java.util.List;
public class ClassRoom {
private String no;
private List<Student> stulist;
private double total;
public ClassRoom() {
super();
stulist = new ArrayList<Student>();
}
public ClassRoom(String no) {
this();
this.no = no;
}
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;
}
}
package exercise;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/***
*
* 面向对象+分拣存储
* 计算每个班的总分和平均分
* @author zw
*
*/
public class Demo01 {
public static void main(String[] args){
List<Student> stuList = exam();
Map<String,ClassRoom> map = count(stuList);
view(map);
}
public static void view(Map<String,ClassRoom> map){
Set<String> keySet = map.keySet();
Iterator<String> ter = keySet.iterator();
while(ter.hasNext()){
String no = ter.next();
ClassRoom room = map.get(no);
double total =room.getTotal();
double avg = total/room.getStulist().size();
System.out.println(no+"班--->总分为:"+total+"--->平均分为"+avg);
}
}
/***
* 统计分析
*/
public static 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(null == room){
room = new ClassRoom(no);
map.put(no, room);
}
room.getStulist().add(stu);
room.setTotal(room.getTotal()+score);
}
return map;
}
/***
* 模拟考试,将考试数据放到list中
* @return
*/
public static List<Student> exam(){
List<Student> list = new ArrayList<Student>();
list.add(new Student("张伟","a",85));
list.add(new Student("老王","a",80));
list.add(new Student("王麻子","a",90));
list.add(new Student("马云","b",85));
list.add(new Student("马化腾","b",95));
return list;
}
}