用Java写出客户清单,共计500份,每份公司客户包含客户ID、客户名称、客户职级以及客户工资这些信息, 客户经理想要按照一定的优先顺序调查每个公司客户的信用情况,优先顺有两个要求,1、职级越高越优先,从高到底分别是 A\B\C\D\E。2、职级相同时,工资越高越优先。 有什么办法可以快速得到这个排序之后的清单?请写一个主方法测试类,把排序后的信息展示到工作台上。
时间: 2023-05-29 13:07:46 浏览: 76
客户类:
```java
public class Customer {
private String id;
private String name;
private String level;
private double salary;
public Customer(String id, String name, String level, double salary) {
this.id = id;
this.name = name;
this.level = level;
this.salary = salary;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getLevel() {
return level;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "Customer{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", level='" + level + '\'' +
", salary=" + salary +
'}';
}
}
```
排序类:
```java
import java.util.*;
public class CustomerSorter {
public static void sortByLevelAndSalary(List<Customer> customers) {
Collections.sort(customers, new Comparator<Customer>() {
@Override
public int compare(Customer o1, Customer o2) {
if (o1.getLevel().equals(o2.getLevel())) {
return Double.compare(o2.getSalary(), o1.getSalary());
} else {
return levelToPriority(o2.getLevel()) - levelToPriority(o1.getLevel());
}
}
});
}
private static int levelToPriority(String level) {
switch (level) {
case "A":
return 5;
case "B":
return 4;
case "C":
return 3;
case "D":
return 2;
case "E":
return 1;
default:
return 0;
}
}
}
```
测试类:
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Customer> customers = new ArrayList<>();
for (int i = 0; i < 500; i++) {
customers.add(new Customer("id" + i, "name" + i, "ABCDE".charAt((int) (Math.random() * 5)) + "", Math.random() * 10000));
}
CustomerSorter.sortByLevelAndSalary(customers);
for (Customer customer : customers) {
System.out.println(customer);
}
}
}
```
阅读全文