从不同的类java中排序对象

时间:2014-05-30 14:48:33

标签: java class sorting collections

我在任务之下。我坚持了问题a)。 创建类,描述员工的小时工资和固定付款。提出你的建议 关于班级之间的关系。实现计算月平均工资的方法。对于 小时工资的员工使用下一个公式:“平均月薪= 20.8 * 8 *小时费率”,适用于员工 固定付款 - “平均月薪=固定月付”。写好注释的代码 解决下一个问题 a)按平均月薪按降序对员工集合进行排序。如果是 同等薪水 - 名字。为收集的所有员工写下ID,姓名和月薪。 b)从收集中写出前五名员工的信息(问题a)。 c)从收集中写出最后三名员工的ID(问题b)。 d)编写用于从(到)文件中读取和写入这些对象的集合的代码。 e)编写用于处理传入文件格式不正确的代码。

我创建了以下类,但我不知道如何从不同的类中对不同的对象进行排序。请帮帮我!!!!

import java.util.*;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        List<EmployeeFixedPayment> coll = new ArrayList<Employee>();

        EmployeeHourlyWage a = new EmployeeHourlyWage("Edd", "Goo", 23, 4);
        EmployeeHourlyWage b = new EmployeeHourlyWage("Tedd", "Foo", 2, 5);
        EmployeeHourlyWage c = new EmployeeHourlyWage("Bob", "Bee", 4, 2);
        EmployeeHourlyWage d = new EmployeeHourlyWage("Kate", "See", 2, 5);
        EmployeeFixedPayment e = new EmployeeFixedPayment("Lisa", "Lee", 7, 500);
        EmployeeFixedPayment f = new EmployeeFixedPayment("Mike", "Ree", 10,
                450);
        EmployeeFixedPayment g = new EmployeeFixedPayment("Izia", "Kurz", 13,
                1000);
        EmployeeFixedPayment j = new EmployeeFixedPayment("Aisha", "Moore", 20,
                800);

        coll.add(a);
        coll.add(b);
        coll.add(c);
        coll.add(d);
        coll.add(e);
        coll.add(f);
        coll.add(g);
        coll.add(j);

        Collections.sort(coll);

        // System.out.println(coll.size());
        for (Employee i : coll) {
            System.out.print(i.secondName + " ");
        }

    }

}

public class Employee {
    String firstName;
    String secondName;
    int id;

    public Employee(String firstName, String secondName,int id){
        this.firstName = firstName;
        this.secondName = secondName;
        this.id = id;
    }


    public void printEmployee(){
        System.out.println(firstName+" "+secondName+" "+id);
    }


    public String getSecondName() {
        return secondName;
    }

    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

}

public class EmployeeFixedPayment extends Employee {
    double fixedPayment;

    public EmployeeFixedPayment(String firstName, String secondName, int id,
            double salary) {
        super(firstName, secondName, id);
        fixedPayment = salary;
    }

    public double getSalary() {
        return fixedPayment;
    }

    public void setSalary(double salary) {
        fixedPayment = salary;
    }
}

public class EmployeeHourlyWage extends Employee {
    Double hourlyWage;

    public EmployeeHourlyWage(String firstName, String secondName, int id, double hourlyRate) {
        super(firstName, secondName, id);
        hourlyWage = 20.8*8*hourlyRate;
    }
    public double getWage(){
        return hourlyWage; 
    }

    public void setWage(double rate) {
        hourlyWage = 20.8*8*rate;
    }

}

2 个答案:

答案 0 :(得分:1)

Employee实施Comparable界面。通过这种方式,当您拨打Collections.sort(coll)时,他们会以正确的方式获得分拣。

就个人而言,我发现CompareToBuilder课对此有帮助。

答案 1 :(得分:0)

这里的诀窍是将相关信息(即月薪)提取到基类(Employee),这样你就可以为它写一个Comparator

首先,让我们将相关方法提升到Employee。由于Employee不包含计算其薪水的逻辑,因此该类会生成班级abstract

public abstract class Employee {
    public abstract double getMonthlySalary();
    // Rest of the members and methods you declared
}

public class EmployeeFixedPayment extends Employee {
    double fixedPayment;

    @Override
    public double getMonthlySalary() {
        return fixedPayment;
    }

    // Rest of the members, ctots, etc. you declared
}

public class EmployeeHourlyWage extends Employee {
    double hourlyWage;

    @Override
    public double getMonthlySalary() {
        return hourlyWage * 20.8 * 8;
    }

    // Rest of the members, ctots, etc. you declared
}

现在所有Employee都在一个连贯的界面中公开他们的薪水,可以通过实施Comparator来对它们进行排序:

public class EmployeeComparator implements Comparator<Employee> {
    @Override
    public int compare (Employee e1, Employee e2) {
        // Note we're comparing e2 to e1 to get a descneding effect
        int salaryCompare = Double.compare(e2.getMonthlySalary(), e1.getMonthlySalary());
        if (salaryCompare != 0) {
            return salaryCompare;
        }

        // If the salaries are equal, compare names
        return e1.getName().compareTo(e2.getName());
    }
}

现在,要将它们放在一起,我们只需使用自定义ListComparator进行排序:

List<Employee> coll = new ArrayList<>();
Collections.sort(coll, new EmployeeComparator());