public class SortingStudent {
public static List<Student> exam() {
List<Student> list = new ArrayList<Student>();
list.add(new Student("T-ara", "二年一班", 94));
list.add(new Student("少女时代", "二年一班", 96));
list.add(new Student("BigBang", "二年八班", 98));
list.add(new Student("2NE1", "二年八班", 90));
list.add(new Student("4Minute", "二年一班", 92));
return list;
}
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 (room == null) {
room = new ClassRoom(no);
map.put(no, room);
}
room.getStuList().add(stu);
room.setTotal(room.getTotal() + score);
}
return map;
}
public static 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 = total / room.getStuList().size();
System.out.println("班级:" +no + ",总分" + total + ",平均分" + avg);
}
}
public static void main(String[] args) {
//考试
List<Student> stuList = exam();
//分析成绩
Map<String,ClassRoom> map = count(stuList);
//查看成绩
view(map);
}
}
/**
* 一个班级 多个学生(学生列表)
*/
public class ClassRoom {
private String no;
private List<Student> stuList;
private double total;
public ClassRoom(){
stuList = new ArrayList<Student>();
}
public ClassRoom(String no){
this();
this.no = no;
}
public ClassRoom(String no, List<Student> stuList, double total) {
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;
}
}
public class Student {
private String name;
private String no;
private double score;
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;
}
}