238025681

Company Roster

Jun 21st, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.94 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.Collections;
  7. import java.util.Comparator;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import java.util.OptionalDouble;
  11.  
  12.  
  13. /**
  14.  *
  15.  * @author chobi
  16.  */
  17. public class Employee {
  18.  
  19.    
  20.  
  21.    
  22.     private String name;
  23.     private double salary;
  24.     private String position;
  25.     private String department;
  26.     private String email;
  27.     private int age;
  28.  
  29.     public Employee(Builder builder) {
  30.         this.name = builder.name;
  31.         this.salary = builder.salary;
  32.         this.position = builder.position;
  33.         this.department = builder.department;
  34.         this.email = builder.email;
  35.         this.age = builder.age;
  36.     }
  37.  
  38.    
  39.    
  40.      public static class Builder {
  41.        private String name;
  42.        private double salary;
  43.        private String position;
  44.        private String department;
  45.        private String email = "n/a";
  46.        private int age = -1;
  47.        
  48.        private Builder name (String name){
  49.            this.name = name;
  50.            return this;
  51.        }
  52.        
  53.        private Builder salary (Double salary){
  54.            this.salary = salary;
  55.            return this;
  56.        }
  57.        
  58.        private Builder position (String position){
  59.            this.position = position;
  60.            return this;
  61.        }
  62.        
  63.        private Builder department (String department){
  64.            this.department = department;
  65.            return this;
  66.        }
  67.        
  68.        private Builder email (String email){
  69.            this.email = email;
  70.            return this;
  71.        }
  72.        
  73.        private Builder age (Integer age){
  74.            this.age = age;
  75.            return this;
  76.        }
  77.  
  78.        public Employee build() {
  79.            return new Employee(this);
  80.        }
  81.      }
  82.    
  83.     public static void main(String[] args) throws Exception {
  84.         InputStreamReader isr = new InputStreamReader(System.in);
  85.         BufferedReader reader = new BufferedReader(isr);
  86.        
  87.         int count = Integer.parseInt(reader.readLine());
  88.        
  89.         Map<String, ArrayList<Employee>> employeeList = new HashMap<>();
  90.         Map<String, ArrayList<Double>> averageDepartmentSalary = new HashMap<>();
  91.        
  92.         for (int i = 0; i < count; i++) {
  93.             String[] input = reader.readLine().split("\\s+");
  94.             addEmployee(input, employeeList);
  95.             if (!averageDepartmentSalary.containsKey(input[3])) {
  96.             averageDepartmentSalary.put(input[3], new ArrayList<>());
  97.             averageDepartmentSalary.get(input[3]).add(Double.parseDouble(input[1]));
  98.            
  99.         }else{
  100.             averageDepartmentSalary.get(input[3]).add(Double.parseDouble(input[1]));
  101.             }
  102.            
  103.            
  104.         }
  105.        
  106.        
  107.         String highestAverageSalary = averageDepartmentSalary.entrySet().stream()
  108.                 .max((entry1, entry2) -> averageValue(entry1.getValue()) > averageValue(entry2.getValue()) ? 1 : -1).get().getKey();
  109.        
  110.    
  111.        
  112.         for (Map.Entry <String, ArrayList<Employee>> arg : employeeList.entrySet()) {
  113.             if (arg.getKey().equals(highestAverageSalary)) {
  114.                 System.out.printf("Highest Average Salary: %s%n", arg.getKey());
  115.                  arg.getValue().
  116.                          stream().
  117.                          sorted((p1, p2) ->Double.compare(p2.salary, p1.salary)).
  118.                          forEach(employee -> System.out.printf("%s %.2f %s %d%n",
  119.                                                                  employee.name,
  120.                                                                  employee.salary,
  121.                                                                  employee.email,
  122.                                                                  employee.age));
  123.                        
  124.            
  125.             }
  126.            
  127.            
  128.         }
  129.        
  130.        
  131.        
  132.     }
  133.    
  134.     private static void addEmployee(String[] input, Map<String, ArrayList<Employee>> employeeList) {
  135.         int numStats = input.length;
  136.         if (!employeeList.containsKey(input[3])) {
  137.             employeeList.put(input[3], new ArrayList<>());
  138.         }
  139.         switch(numStats){
  140.        
  141.             case 4: employeeList.get(input[3]).add(new Employee.Builder().
  142.                     name(input[0]).
  143.                     salary(Double.parseDouble(input[1])).
  144.                     position(input[2]).
  145.                     department(input[3]).
  146.                     build());
  147.                    
  148.             break;
  149.  
  150.             case 5:if (input[4].contains("@")) {
  151.                  employeeList.get(input[3]).add(new Employee.Builder().
  152.                     name(input[0]).
  153.                     salary(Double.parseDouble(input[1])).
  154.                     position(input[2]).
  155.                     department(input[3]).
  156.                     email(input[4]).
  157.                     build());
  158.                 }else{
  159.                 employeeList.get(input[3]).add(new Employee.Builder().
  160.                     name(input[0]).
  161.                     salary(Double.parseDouble(input[1])).
  162.                     position(input[2]).
  163.                     department(input[3]).
  164.                     age(Integer.parseInt(input[4])).
  165.                     build());
  166.             }
  167.            
  168.             break;
  169.  
  170.             case 6:
  171.                  employeeList.get(input[3]).add(new Employee.Builder().
  172.                     name(input[0]).
  173.                     salary(Double.parseDouble(input[1])).
  174.                     position(input[2]).
  175.                     department(input[3]).
  176.                     email(input[4]).
  177.                     age(Integer.parseInt(input[5])).
  178.                     build());
  179.             break;
  180.         }
  181.     }
  182.    
  183.     private static Double averageValue(ArrayList<Double> value) {
  184.        
  185.         return value.stream().mapToDouble(i -> i).average().orElse(0);
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment