如何使用java OPP在ArrayList中获得前3条记录<Employee>?



我有两个文件a.t txt和b.t txt。

a.t txt有3列:id,姓名和月份e1,大卫,12
e2,简,24
e4,彼得,15
e3,约翰,25

b.t txt有2列:id和baseSalary
e1,1200
e2,1800
e4,2400

我将读取a.t txt到一个数组列表

public ArrayList<Employee> loadEmployee(String file) {
ArrayList<Employee> Emp = new ArrayList<Employee>();
ArrayList<String> employees = loadFile(file);
for (String emp : employees) {
String[] data = emp.split(",");
Emp.add(new Employee(data[0], data[1], Integer.parseInt(data[2])));
}
return Emp;
}

public class Employee{
private String id;
private String name;
private int month;

public Employee(String id, String name, int month){
this.id=id;
this.name=name;
this.month=month;
}

public double getSalary(int baseSalary){
return this.month*baseSalary;
}

public String toString(){
return this.id+"_"+this.name+"_"+this.month;
}
}

现在我想知道薪水最高的前3名员工。但是不能创建新类。我不知道该怎么办。
我想我会读b.t txt到哈希图,但哈希图不能排序。所以我不能得到前3。
有人能帮我吗?
对不起,如果我的英语不好。

您的loadEmployee方法是一个良好的开端,但正如您所说,您必须阅读第二个文件以获得每个员工的基本工资并计算他们的总工资(我猜这是总收入)。

您可以定义第二个方法setEmployeeBaseSalary,它接受第二个文件名和雇员的List。然后,基本上以与第一个文件相同的方式读取第二个文件。对于读取的每个id,您将在List中执行搜索,根据其id收集员工,然后设置其基本工资。

在更新了雇员的List之后,您可以使用收集流按工资对列表进行排序,收集前三个结果,然后将它们收集到新的List中。

public static ArrayList<Employee> loadEmployee(String file) {
//your exact same implementation
}
public static ArrayList<Employee> setEmployeeBaseSalary(String file, List<Employee> list) {
ArrayList<Employee> Emp = new ArrayList<>();
try (FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr)) {
Employee emp;
String line = br.readLine();
while (line != null) {
String[] data = line.split(",");
//Looking for an employee by the id read
emp = list.stream().filter(employee -> employee.getId().equals(data[0])).findFirst().orElse(null);

//If an employee with that id has been found then it's base salary is set
if (emp != null) {
emp.setBaseSalary(Double.parseDouble(data[1]));
}
line = br.readLine();
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return Emp;
}

测试主要
public static void main(String[] args) {
//Reading the list of employees
ArrayList<Employee> listEmployee = loadEmployee("a.txt");
//Setting the base salary for each employee
setEmployeeBaseSalary("b.txt", listEmployee);
//Retrieving the top three salary employees
List<Employee> listTopThreeEmployee = listEmployee.stream()
.sorted(Comparator.comparingDouble(Employee::getSalary).reversed()) //sorting each employee by their salary in reversed order (from the highest to the lowest)
.limit(3) //Limiting the stream to the top three results
.collect(Collectors.toList()); //Collecting the top three employees in a new list
System.out.println(listTopThreeEmployee);
}

此外,您应该在Employee类中添加setter和getter方法来设置和检索Employee的字段。

class Employee {
private String id;
private String name;
private int month;
private double baseSalary;
public Employee(String id, String name, int month) {
this.id = id;
this.name = name;
this.month = month;
this.baseSalary = 0;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
public double getSalary() {
return this.month * this.baseSalary;
}
public String toString() {
return "(" + this.id + "-" + this.name + "-" + this.month + "-" + baseSalary + "-" + getSalary() + ")";
}
}

旁注:表示货币的变量应该声明为BigDecimal而不是double,因为重要的是要精确表示值,而不是近似值。实际上,双精度会尽可能接近地表示值,但并不是说它对应于实际赋值。

最新更新