Advertisement
Guest User

Untitled

a guest
Jun 21st, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.util.*;
  3. import java.util.stream.Collector;
  4. import java.util.stream.Collectors;
  5.  
  6. /**
  7. * Created by iwano on 6/21/2016.
  8. */
  9. public class Employee {
  10. private String name;
  11. private Double salary;
  12. private String position;
  13. private String department;
  14. private String email;
  15. private int age;
  16.  
  17. public Employee() {
  18.  
  19. }
  20.  
  21. public Employee(String name, double salary, String position, String department) {
  22. this.name = name;
  23. this.salary = salary;
  24. this.position = position;
  25. this.department = department;
  26. this.email = "n/a";
  27. this.age = -1;
  28. }
  29.  
  30. public Employee(String name, double salary, String position, String department, String email) {
  31. this(name, salary, position, department);
  32. this.email = email;
  33. this.age = -1;
  34. }
  35.  
  36. public Employee(String name, double salary, String position, String department, String email, int age) {
  37. this(name, salary, position, department, email);
  38. this.age = age;
  39. }
  40.  
  41. public double getSalary() {
  42. return salary;
  43. }
  44.  
  45. @Override
  46. public String toString() {
  47. return String.format("%s %.2f %s %d", this.name, this.salary, this.email, this.age);
  48. }
  49.  
  50. private static Double getAverageSalaray(ArrayList<Employee> l) {
  51. List<Double> lista = new ArrayList<>();
  52. lista = l.stream()
  53. .map(Employee::getSalary)
  54. .collect(Collectors.toList());
  55. double sum = 0;
  56. for (Double d : lista) {
  57. sum += d;
  58. }
  59. return sum/lista.size();
  60. }
  61.  
  62. public static void main(String[] args) {
  63. Scanner scan = new Scanner(System.in);
  64. int n = Integer.parseInt(scan.nextLine());
  65.  
  66. double max = Double.MIN_VALUE;
  67. String maxDep = "";
  68.  
  69.  
  70. HashMap<String, ArrayList<Employee>> depToPerson = new HashMap<>();
  71. for (int i = 0; i < n; i++) {
  72. String input[] = scan.nextLine().split("\\s+");
  73. Employee employee = new Employee();
  74. if (input.length == 4) {
  75. employee = new Employee(input[0], Double.parseDouble(input[1]), input[2], input[3]);
  76. } else if (input.length == 5) {
  77. employee = new Employee(input[0], Double.parseDouble(input[1]), input[2], input[3], input[4]);
  78. } else if (input.length == 6) {
  79. employee = new Employee(input[0], Double.parseDouble(input[1]), input[2], input[3], input[4], Integer.parseInt(input[5]));
  80. }
  81.  
  82. if (!depToPerson.containsKey(employee.department)) {
  83. depToPerson.put(employee.department, new ArrayList<>());
  84. }
  85. depToPerson.get(employee.department).add(employee);
  86. }
  87.  
  88. for (Map.Entry<String, ArrayList<Employee>> entry : depToPerson.entrySet()) {
  89. Double tempmax = Employee.getAverageSalaray(entry.getValue());
  90. if (tempmax > max) {
  91. max = tempmax;
  92. maxDep = entry.getKey();
  93. }
  94. }
  95.  
  96. System.out.printf("Highest Average Salary: %s\n", maxDep);
  97.  
  98. depToPerson.get(maxDep).stream()
  99. .sorted((e1, e2) -> Double.compare(e2.getSalary(), e1.getSalary()))
  100. .forEach(e -> {
  101. System.out.println(e.toString());
  102. });
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement