员工和教职员工姓名和年薪按降序排列

时间:2016-11-18 01:15:45

标签: java arrays sorting

在我的课程中,教师的详细信息如姓名出生日期,状态。这只是为教师打印。 我有这样的输出:

John was born on Sun Jan 03 00:00:00 MST 1971: fulltime

然后我想打印每个工作人员的姓名和年薪,而教职员工和工资必须是降序。 我有这样的输出:

John was born on Sun Jan 03 00:00:00 MST 1971: fulltime
[Barry was born on Tue Jan 02 00:00:00 MST 1973: part-time, Andy was born on Wed Nov 12 00:00:00 MST 1980: full-time,  
 Name: Edith  Annual Salary : 7416.666666666666,  
 Name: Don  Annual Salary : 5866.666666666667,  
 Name: Cathy  Annual Salary : 2900.0]

但我刚拿到员工姓名和年薪。我想打印教员姓名和年薪。我不知道我错在哪里? 我怎么能这样做?

人类:

public abstract class Person implements Comparable<Person> {
    protected String firstName;
    protected java.util.Date birthdate;

    public Person(String firstName, java.util.Date birthdate) {
        this.firstName = firstName;
        this.birthdate = birthdate;
    }

    public java.util.Date getBirthdate() {
        return birthdate;
    }

    public String getName() {
        return firstName;
    }

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

    public void setBirthdate(java.util.Date birthdate) {
        this.birthdate = birthdate;
    }

    public String toString() {
        return " \n" + " Name: " + getName()+ " " + " Annual Salary : " + getSalary();
    }

    public abstract Double getSalary();

    public int compareTo(Person o) {
         Person per =(Person)o;

            if (this.getSalary() > per.getSalary()){
                return -1;
            }else if (this.getSalary() < per.getSalary()){
                return 1;
            }else
                return 0;

  }

}

教师班:

public class Faculty extends Person{

    private double monthlySalary;
    private String status;
    public Faculty(String firstName, Date birthdate, double salaryln, String statusln) {
        super(firstName, birthdate);

        this.monthlySalary = salaryln;
        this.status = statusln;
        // TODO Auto-generated constructor stub
    }

@Override

    public Double getSalary() {
        // TODO Auto-generated method stub
        double fsalary = monthlySalary*9.0;
        return fsalary;
    }

    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }

    public double getMonthlySalary() {
        return monthlySalary;
    }

    public void setMonthlySalary(double monthlySalary) {
        this.monthlySalary = monthlySalary;
    }

     public String toString(){
            return getName() + " was born on " + birthdate +  ": " + getStatus();
        }

}

员工班级:

public class Staff extends Person{

    private double annualSalary;
    private double experience;

    public Staff(String firstName, Date birthdate, double salaryln, double experienceln) {
        super(firstName, birthdate);
        this.annualSalary = salaryln;
        this.experience = experienceln;
        // TODO Auto-generated constructor stub
    }

    @Override
    public Double getSalary() {
        // TODO Auto-generated method stub
        double staffSalary = annualSalary + annualSalary*(30.0-experience)/60.0;
        return staffSalary;
    }

    public double getAnnualSalary() {
        return annualSalary;
    }

    public void setAnnualSalary(double annualSalary) {
        this.annualSalary = annualSalary;
    }

    public double getExperience() {
        return experience;
    }

    public void setExperience(int experience) {
        this.experience = experience;
    }

}

PersonTest Class:

public class PersonTest {

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

            String pattern = "MM/dd/yyyy";
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            Date date = format.parse("01/03/1971");
            Faculty faculty = new Faculty("John", date, 2000, "fulltime"); 
            System.out.println(faculty);



        Person[] ps = new Person[5];

        ps[0] = new Staff("Cathy", format.parse("8/19/1968"), 2000.0, 3.0);
        ps[1] = new Staff("Don", format.parse("4/17/1969"), 4000.0, 2.0);
        ps[2] = new Staff("Edith", format.parse("10/28/1979"), 5000.0, 1.0);
        ps[3] = new Faculty("Andy", format.parse("11/12/1980"), 4000.0, "full-time");
        ps[4] = new Faculty("Barry", format.parse("1/2/1973"), 6000.0, "part-time");




        Arrays.sort(ps);

        System.out.println(Arrays.toString(ps) );
        System.out.println();

    }

}

1 个答案:

答案 0 :(得分:0)

Faculty课程中,您可以覆盖toString(),如下所示:

// There should be a @Override here.
public String toString(){
    return getName() + " was born on " + birthdate +  ": " + getStatus();
}

这意味着当您打印调用Faculty::toString的数组时,它会打印您定义为String对象的Faculty表示形式,这是具有某些出生日期的名称和状态。

如果您想使用Person定义进行打印,则应停止覆盖toString方法,因此Faculty默认为超类toString方法(或者,将Faculty::toString的名称更改为不同的名称)或将Faculty转换为Person,以便您可以访问Person::toString并使用该方法而不是覆盖版本进行打印

此外,如果您的Person::compareTo几乎是简单的整数比较,您可以使用:return Integer.compare(this.getSalary(), o.getSalary())

如果您觉得我的回答有用,请接受!