John_IV

Untitled

Sep 24th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. package Programming_Fundamentals.Objects_and_Classes.Objects_and_Classes_More_Exercise.E01CompanyRoster;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10. public static void main(String[] args) {
  11. Scanner sc = new Scanner(System.in);
  12. int N = Integer.parseInt(sc.nextLine());
  13. List<Employee> employeeList = new ArrayList<>();
  14. double highestAverageSalary = 0;
  15. String bestDepartment = "";
  16.  
  17. for (int i = 0; i < N; i++) {
  18.  
  19. String[] employeeInfo = sc.nextLine().split("\\s+");
  20.  
  21. String email;
  22. int age;
  23.  
  24. if (employeeInfo.length == 4) {
  25. age = -1;
  26. email = "n/a";
  27.  
  28. } else if (employeeInfo.length == 5) {
  29. try {
  30. email = "n/a";
  31. age = Integer.parseInt(employeeInfo[4]);
  32.  
  33. } catch (Exception e) {
  34. email = employeeInfo[4];
  35. age = -1;
  36. }
  37.  
  38. } else {
  39. email = employeeInfo[4];
  40. age = Integer.parseInt(employeeInfo[5]);
  41. }
  42.  
  43. Employee employee = new Employee(employeeInfo[0], Double.parseDouble(employeeInfo[1]), employeeInfo[2], employeeInfo[3], email, age);
  44. employeeList.add(employee);
  45. }
  46.  
  47. List<String> departmentsList =
  48. employeeList
  49. .stream()
  50. .map(Employee::getDepartment)
  51. .distinct()
  52. .collect(Collectors.toList());
  53.  
  54. for (String department : departmentsList) {
  55.  
  56. double sum = 0.0;
  57. int count = 0;
  58.  
  59. for (Employee person : employeeList) {
  60.  
  61. if (person.getDepartment().equals(department)) {
  62.  
  63. sum += person.getSalary();
  64. count++;
  65. }
  66. }
  67.  
  68. if (sum / count > highestAverageSalary) {
  69.  
  70. highestAverageSalary = sum / count;
  71. bestDepartment = department;
  72. }
  73. }
  74.  
  75. System.out.println("Highest Average Salary: " + bestDepartment);
  76. String finalDepartment = bestDepartment;
  77.  
  78. employeeList
  79. .stream()
  80. .sorted(Comparator.comparingDouble(Employee::getSalary).reversed())
  81. .filter(employee -> employee.getDepartment().equals(finalDepartment))
  82. .forEach(System.out::println);
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment