面向对象分拣存储3、案例

public class SortingStudent {

    /**
     * 定义一个Student类,属性:name(姓名),no(班号),score(成绩)
     * 现在将若干Student对象放入List,请统计出每个班级的总分和平均分
     * 
     * 面向对象分拣存储 
     * -不推荐使用 Map<String,List<Student>> 操作不方便 
     * -推荐使用    Map<String,ClassRoom>
     */
    // 测试数据
    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;
    }

    /*
     * 统计分析 1、面向对象 2、分拣存储
     */
    public static Map<String, ClassRoom> count(List<Student> list) {
        Map<String, ClassRoom> map = new HashMap<String, ClassRoom>();
        // 1、遍历List
        for (Student stu : list) {
            String no = stu.getNo(); // 班级编号
            double score = stu.getScore(); // 学号
            // 2、分拣,查看是否存在该编号的班级
            // 如果不存在,创建班级
            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;
    }

    /*
     * 查看每个班的总分和平均分 -->遍历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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值