JAVA职工排序题

1. 为某保险公司设计一个职工管理系统,其中职工类的属性有:职工编号,姓名,性别,团体险业绩,个体险业绩;方法有: 每个属性对应的set,get方法; 不带参数的构造方法; 带参数的构造方法,完成对职工属性的初始化; 该类实现接口Comparable,完成对职工总业绩的比较。

2. 设计一个类,实现Comparator接口,完成对团体险业绩的比较;

3. 在Main类中,创建一个职工的线性表,分别完成对职工线性表按照总业绩升序排序,按照团体险业绩升序排序。 注意:不要设计键盘输入职工信息,可根据样例中提供的数据直接创建职工对象;

输入格式:
输出格式:
各项之间用逗号“,”分隔

输入样例:
在这里给出一组输入。例如:

输出样例:
在这里给出相应的输出。例如:

编号,团险,个险,姓名,性别
1,500,400,职工1,female
3,600,300,职工3,male
2,400,600,职工2,female
4,800,200,职工4,female
5,500,700,职工5,male
编号,团险,个险,姓名,性别
2,400,600,职工2,female
1,500,400,职工1,female
5,500,700,职工5,male
3,600,300,职工3,male
4,800,200,职工4,female

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Comparator;

public class Main {
public static void main(String[] args) {
List staffs = new ArrayList();

    staffs.add(new Staff(1, 500, 400, "职工1", "female"));
    staffs.add(new Staff(3, 600, 300, "职工3", "male"));
    staffs.add(new Staff(2, 400, 600, "职工2", "female"));
    staffs.add(new Staff(4, 800, 200, "职工4", "female"));
    staffs.add(new Staff(5, 500, 700, "职工5", "male"));

    System.out.println("编号,团险,个险,姓名,性别");
    for (int i = 0; i < staffs.size(); i++) {
        System.out.println(staffs.get(i));
    }

    // 排序,先团险升序,再个险升序
    Collections.sort(staffs);
    Collections.sort(staffs, new TeamComparator());

    System.out.println("编号,团险,个险,姓名,性别");
    for (int i = 0; i < staffs.size(); i++) {
        System.out.println(staffs.get(i));
    }
}

}

class TeamComparator implements Comparator {
// 团队险业绩升序
@Override
public int compare(Staff s1, Staff s2){
return s1.getTeamInsure() - s2.getTeamInsure();
}
}

class Staff implements Comparable {
private int no; // 职工编号
private String name; // 职工姓名
private String sex; // 性别
private int teamInsure; // 团队险业绩
private int personInsure; // 个人险业绩

// 构造方法
public Staff(){
}
public Staff(int no, int teamInsure, int personInsure, String name, String sex){
    this.no = no;
    this.teamInsure = teamInsure;
    this.personInsure = personInsure;
    this.name = name;
    this.sex = sex;
}

// 个人险业绩 升序
@Override
public int compareTo(Staff staff){
    return this.getPersonInsure() - staff.getPersonInsure();
}

@Override
public String toString(){
    return no + "," + teamInsure + "," + personInsure + "," + name + "," + sex;
}

// get set
public int getNo(){
    return no;
}
public void setNo(int no){
    this.no = no;
}
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 int getTeamInsure() {
    return teamInsure;
}
public void setTeamInsure(int teamInsure) {
    this.teamInsure = teamInsure;
}
public int getPersonInsure() {
    return personInsure;
}
public void setPersonInsure(int personInsure) {
    this.personInsure = personInsure;
}

}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于 Java 基数排序,我可以给你一些简单的介绍。 Java 基数排序是一种非比较排序算法,它通过将待排序元素按照位数切割成不同的数字,然后按照每个位数分别比较。具体来说,它将所有待比较数值统一为同样的数位长度,数位较短的数前面补零,然后从最低位开始,依次进行一次排序,这样从最低位排序一直到最高位排序完成后,数列就变成一个有序序列。 Java 基数排序的时间复杂度为 O(d(n+r)),其中 d 是数字位数,n 是待排序元素个数,r 是基数。虽然时间复杂度比较高,但是它的空间复杂度比较低,是一种稳定的排序算法。 下面是 Java 基数排序的代码实现: ``` public static void radixSort(int[] arr) { if (arr == null || arr.length < 2) { return; } int max = Integer.MIN_VALUE; for (int i = 0; i < arr.length; i++) { max = Math.max(max, arr[i]); } int maxDigit = 0; while (max != 0) { max /= 10; maxDigit++; } int mod = 10, div = 1; List<List<Integer>> bucketList = new ArrayList<>(); for (int i = 0; i < 10; i++) { bucketList.add(new ArrayList<>()); } for (int i = 0; i < maxDigit; i++, mod *= 10, div *= 10) { for (int j = 0; j < arr.length; j++) { int num = (arr[j] % mod) / div; bucketList.get(num).add(arr[j]); } int index = 0; for (int j = 0; j < bucketList.size(); j++) { List<Integer> bucket = bucketList.get(j); for (int k = 0; k < bucket.size(); k++) { arr[index++] = bucket.get(k); } bucket.clear(); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值