军训分组练习Java实现

//Trainee.java
package cn.campsg.java.experiment.entity;

//interface Comparable{
//    public int compareTo(Trainee another);
//}

public class Trainee implements Comparable<Trainee>{
    private String name;
    private String sex;
    private float height;

    public Trainee(String name,String sex,float height){
        this.name=name;
        this.sex=sex;
        this.height=height;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    public Trainee(){}


    @Override
    public int compareTo(Trainee another) {
        if(this.getHeight()>another.getHeight())
            return Float.compare(this.getHeight(),another.getHeight());
        else if(this.getHeight()<another.getHeight())
            return Float.compare(this.getHeight(),another.getHeight());
        else
            return Float.compare(this.getHeight(),another.getHeight());
    }
}

//TraineesGroups.java
package cn.campsg.java.experiment.entity;

import java.util.*;

public class TraineesGroups {
    private List<Trainee> trnList = null;
    private Map<String,List<Trainee>> groups = null;

    public void initTrainees(List<Trainee> trnList){
        this.trnList=trnList;
    }

    public Map<String,List<Trainee>> groupTrainees(){
        if(trnList==null)
            return null;
        groups = new HashMap<String,List<Trainee>>();
        groups.put("male",new ArrayList<Trainee>());
        groups.put("female",new ArrayList<Trainee>());

        for (Trainee trainee : trnList) {
            switch (trainee.getSex()) {
                case "male":
                    groups.get("male").add(trainee);
                    break;
                case "female":
                    groups.get("female").add(trainee);
                    break;
                default:
                    break;
            }
        }

        Collections.sort(groups.get("male"));
        Collections.sort(groups.get("female"));

        return groups;
    }
}

//MainClass.java
package cn.campsg.java.experiment;

import cn.campsg.java.experiment.entity.Trainee;
import cn.campsg.java.experiment.entity.TraineesGroups;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MainClass {
    public static void main(String[] args) {
        TraineesGroups tg = new TraineesGroups();

        List<Trainee> trnList = new ArrayList<Trainee>();


        for(int i=0;i<5;i++){
            trnList.add(new Trainee("xm3","male",i+170));
        }

        for(int i=0;i<5;i++){
            trnList.add(new Trainee("xm4","female",i+160));
        }

        tg.initTrainees(trnList);

        Map<String, List<Trainee>> group = tg.groupTrainees();
        for (Trainee male :
                group.get("male")) {
            System.out.println(male.getName() + " " + male.getSex() + " " + male.getHeight());
        }

        for (Trainee female :
                group.get("female")) {
            System.out.println(female.getName() + " " + female.getSex() + " " + female.getHeight());
        }


    }
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值