职工排序题
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.*;
public class Main {
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
List<Staff> ss=new ArrayList<Staff>();
ss.add(new Staff(1,500,400,"职工1","female"));
ss.add(new Staff(2,400,600,"职工2","female"));
ss.add(new Staff(3,600,300,"职工3","male"));
ss.add(new Staff(4,800,200,"职工4","female"));
ss.add(new Staff(5,500,700,"职工5","male"));
Collections.sort(ss);
System.out.println("编号,团险,个险,姓名,性别");
for (Staff s:ss){
System.out.println(s.toString());
}
ss.sort(new newComparetor());
System.out.println("编号,团险,个险,姓名,性别");
for (Staff s:ss){
System.out.println(s.toString());
}
}
}
class Staff implements Comparable<Staff>{
private int number;
private int tscore;
private int pscore;
private String name ;
private String sex;
public Staff() {
}
public Staff(int number, int tscore, int pscore, String name, String sex) {
this.number = number;
this.tscore = tscore;
this.pscore = pscore;
this.name = name;
this.sex = sex;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
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 getTscore() {
return tscore;
}
public void setTscore(int tscore) {
this.tscore = tscore;
}
public int getPscore() {
return pscore;
}
public void setPscore(int pscore) {
this.pscore = pscore;
}
@Override
public int compareTo(Staff s) {
int ret=this.pscore+this.tscore-s.getPscore()-s.getTscore();
if (ret>0)ret=1;
else if (ret<0)ret=-1;
return ret;
}
@Override
public String toString() {
return number +
"," + tscore +
"," + pscore +
"," + name +
"," + sex;
}
}
class newComparetor implements Comparator<Staff> {
public newComparetor() {
}
@Override
public int compare(Staff o1, Staff o2) {
int ret=o1.getTscore()-o2.getTscore();
if (ret>0)ret=1;
else if (ret<0)ret=-1;
return ret;
}
}