Java中面向对象分拣存储

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;


    }
}

1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值