KNikov

CompanyRoster

Jun 21st, 2016
357
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.text.CollationElementIterator;
  5. import java.util.*;
  6. import java.util.stream.Collectors;
  7.  
  8. public class CompanyRoster {
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.         HashMap<String, List<Double>> departments = new HashMap<>();
  14.         int n = Integer.parseInt(bf.readLine());
  15.         List<Employee> employeeList = new ArrayList<>();
  16.         for (int i = 0; i < n; i++) {
  17.             String[] input = bf.readLine().split("\\s+");
  18.             String currentName = input[0];
  19.             double currentSalary = Double.parseDouble(input[1]);
  20.             String currentPosition = input[2];
  21.             String currentDepartment = input[3];
  22.             String currentEmail = null;
  23.             Integer currentAge = null;
  24.             switch (input.length) {
  25.                 case 5:
  26.                     currentEmail = input[4].contains("@") ? input[4] : null;
  27.                     currentAge = !input[4].contains("@") ? Integer.parseInt(input[4]) : null;
  28.                     break;
  29.                 case 6:
  30.                     currentEmail = input[4];
  31.                     currentAge = Integer.parseInt(input[5]);
  32.                     break;
  33.             }
  34.             Employee employee = new Employee(currentName, currentSalary, currentPosition, currentDepartment, currentEmail, currentAge);
  35.             if (!departments.containsKey(currentDepartment)) {
  36.                 departments.put(currentDepartment, new ArrayList<>());
  37.             }
  38.             departments.get(currentDepartment).add(currentSalary);
  39.             employeeList.add(employee);
  40.         }
  41.         HashMap<String, Double> averages = new HashMap<>();
  42.         departments.entrySet().stream().forEach(e -> {
  43.             double a = e.getValue().stream().mapToDouble(i -> i).average().getAsDouble();
  44.             averages.put(e.getKey(), a);
  45.         });
  46.         String departmentWithHighestSalary =
  47.                 Collections.max(averages.entrySet(), (entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1).getKey();
  48.  
  49.         System.out.printf("Highest Average Salary: %s\n",departmentWithHighestSalary);
  50.  
  51.         employeeList.stream()
  52.                 .filter(e -> e.getDepartment().equals(departmentWithHighestSalary))
  53.                 .sorted((e1,e2)-> Double.compare(e2.getSalary(),e1.getSalary()))
  54.                 .forEach(System.out::println);
  55.     }
  56. }
  57.  
  58. class Employee {
  59.     private String name;
  60.     private double salary;
  61.     private String position;
  62.     private String department;
  63.     private String email;
  64.     private Integer age;
  65.  
  66.     Employee(String name, double salary, String position, String department, String email, Integer age) {
  67.         this.setName(name);
  68.         this.setSalary(salary);
  69.         this.setPosition(position);
  70.         this.setDepartment(department);
  71.         this.setAge(age);
  72.         this.setEmail(email);
  73.     }
  74.  
  75.     public String getName() {
  76.         return name;
  77.     }
  78.  
  79.     private void setName(String name) {
  80.         this.name = name;
  81.     }
  82.  
  83.     public double getSalary() {
  84.         return salary;
  85.     }
  86.  
  87.     private void setSalary(double salary) {
  88.         this.salary = salary;
  89.     }
  90.  
  91.     public String getPosition() {
  92.         return position;
  93.     }
  94.  
  95.     private void setPosition(String position) {
  96.         this.position = position;
  97.     }
  98.  
  99.     public String getDepartment() {
  100.         return department;
  101.     }
  102.  
  103.     private void setDepartment(String department) {
  104.         this.department = department;
  105.     }
  106.  
  107.     public String getEmail() {
  108.         return email;
  109.     }
  110.  
  111.     private void setEmail(String email) {
  112.         if (email == null) {
  113.             this.email = "n/a";
  114.             return;
  115.         }
  116.         this.email = email;
  117.     }
  118.  
  119.     public int getAge() {
  120.         return age;
  121.     }
  122.  
  123.     private void setAge(Integer age) {
  124.         if (age == null) {
  125.             this.age = -1;
  126.             return;
  127.         }
  128.         this.age = age;
  129.     }
  130.  
  131.     @Override
  132.     public String toString() {
  133.         return String.format("%s %.2f %s %d", this.getName(), this.getSalary(), this.getEmail(), this.getAge());
  134.     }
  135. }
RAW Paste Data