How do I transfer an employee to increase the average PO in departments?

Task: transfer an employee to increase the average PO in the departments?

As planned: at first, it was assumed that I would get all the salaries from the Department. Then I will work according to this algorithm: the transfer is possible from a department with a larger average po to a department with a smaller average po, we need employees of the department with a larger po who have a po ranging from the average of one department to the average of the second department.

Problems: 1. According to the po, which I got it - I still need to get the name of the employee who has this salary. 2. The whole mechanism works only when the first department has a large po, it is not suitable for the second, because when you save items to the list, the PO of the first department will also be recorded, and how to do this without them has not yet figured out.

public static BigDecimal empswap(Map<String, Department> departments, String key) {
    BigDecimal sal = new BigDecimal(0);
    List<BigDecimal> temp = new ArrayList<BigDecimal>();

    for (Department dep : departments.values()) {
        if (key.equals("Первый отдел имеет большую среднюю зп")) {
            int i = 0;
            // Сохраняю в List данные о зп первого отдела
            dep.getEmployeeList().forEach((c) -> temp.add(c.getSalary()));
            for (BigDecimal number : temp) {
                // Ищу макс. зп, которая находится между двух средних зп по отделам
                if (temp.get(i).compareTo(avgEmp.get(1)) > 0 && temp.get(i).compareTo(avgEmp.get(0)) > 0) {
                    sal = temp.get(i);
                }
                i++;
            }
break;
        }
        if (key.equals("Второй отдел имеет большую среднюю зп")) {
            break;
        }
    }
    return sal;
}

How data is stored in departments:

Первый - Department{name='Первый', employeeList=[Employee{salary=13000.13, name='Кошкин'}, Employee{salary=13000.18, name='Петрович Котович Кот'}, Employee{salary=20000.0, name='Сидоров'}, Employee{salary=40000.0, name='Пушкин'}]}
Второй - Department{name='Второй', employeeList=[Employee{salary=10000.0, name='Петров'}]}

Average po

public static void avg(Map<String, Departament> departments, List<BigDecimal> avgEmp) {
        for (Departament dep : departments.values()) {
            BigDecimal avg = dep.salaryAvg();
            avgEmp.add(avg);
            System.out.println("Средняя заработная плата отдела " + dep.getName() + ": " + avg);
        }
        systemMessage(1);
    }

public BigDecimal salaryAvg() {
        BigDecimal sum = BigDecimal.ZERO;
        for (Employee employee : employeeList)
            sum = sum.add(employee.getSalary());
        return sum.divide(BigDecimal.valueOf(employeeList.size()));
    }
Author: Blacit, 2020-08-04

1 answers

Let's go in order: As planned: at first it was assumed that I would get all the salaries from the Department. there is no need to create an additional one. structures for storing unnecessary information, because you already have it, work with it.

First you need to find the average PO of the departments:

double avgSalaryAtFirstDep = department1.getEmployeeList().stream().mapToDouble(Employee::getSalary).average().orElse(0.0);  // средняя ЗП первого отдела

Then we compare the average PO of the departments:

List<Employee> employeeList = new ArrayList<>(); // лист, в котором будут храниться сотрудники для перевода
if (avgSalaryAtFirstDep > avgSalaryAtSecondDep) { // если средняя ЗП в первом отделе больше
   employeeList.addAll(
      department1.getEmployeeList().stream() // значит проходим по первому листу и сравниваем
        .filter(emp -> emp.getSalary() > avgSalaryAtSecondDep && emp.getSalary() < avgSalaryAtFirstDep)
        .collect(Collectors.toList()));
 } else if (avgSalaryAtFirstDep < avgSalaryAtSecondDep) {
  // тое самое для второго
   } else { 
    // если равны
     }

And here at the output you get a sheet with employees who can move to another department employeeList

I decided with the help of streams, but it is also real on cycles.

 2
Author: azlov, 2020-08-07 07:10:21