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;
}
}