Guest User

Employee

a guest
Jun 21st, 2016
272
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package javaOOPBasics.defineClasses.problem04;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.LinkedHashMap;
  8. import java.util.List;
  9. import java.util.stream.Collectors;
  10.  
  11. public class Employee {
  12.  
  13.     String name;
  14.     Double salary;
  15.     String position;
  16.     String department;
  17.     String email;
  18.     Integer age;
  19.  
  20.     public Employee(String name, Double salary, String position, String department, String email, Integer age) {
  21.         this.name = name;
  22.         this.salary = salary;
  23.         this.position = position;
  24.         this.department = department;
  25.         this.email = email;
  26.         this.age = age;
  27.     }
  28.  
  29.     public Employee(String name, Double salary, String position, String department) {
  30.         this(name, salary, position, department, "n/a", -1);
  31.     }
  32.  
  33.     public Employee(String name, Double salary, String position, String department, String email) {
  34.         this(name, salary, position, department, email, -1);
  35.     }
  36.  
  37.     public Employee(String name, Double salary, String position, String department, Integer age) {
  38.         this(name, salary, position, department, "n/a", age);
  39.     }
  40.  
  41.     public static void main(String[] args) throws IOException {
  42.         BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
  43.  
  44.         int count = Integer.parseInt(sc.readLine());
  45.  
  46.         LinkedHashMap<String, List<Employee>> employees = new LinkedHashMap<>();
  47.  
  48.         for (int i = 0; i < count; i++) {
  49.             String[] inputData = sc.readLine().split("\\s+");
  50.             String name = inputData[0];
  51.             Double salary = Double.parseDouble(inputData[1]);
  52.             String position = inputData[2];
  53.             String department = inputData[3];
  54.  
  55.             Employee employee = new Employee(name, salary, position, department);
  56.  
  57.             if (inputData.length == 5) {
  58.                 try {
  59.                     Integer age = Integer.parseInt(inputData[4]);
  60.                     employee = new Employee(name, salary, position, department, age);
  61.                 } catch (Exception ex) {
  62.                     String email = inputData[4];
  63.                     employee = new Employee(name, salary, position, department, email);
  64.                 }
  65.             } else if (inputData.length == 6) {
  66.                 String email = inputData[4];
  67.                 Integer age = Integer.parseInt(inputData[5]);
  68.                 employee = new Employee(name, salary, position, department, email, age);
  69.             }
  70.  
  71.             if (!employees.containsKey(department)) {
  72.                 employees.put(department, new ArrayList<>());
  73.             }
  74.  
  75.             employees.get(department).add(employee);
  76.         }
  77.  
  78.         String department = "";
  79.  
  80.         System.out.printf("Highest Average Salary: %s%n", department = employees.entrySet()
  81.                 .stream().sorted((e1, e2) -> {
  82.                     double curAvgSalaryE1 = e1.getValue().stream()
  83.                             .collect(Collectors.averagingDouble(a -> a.salary));
  84.  
  85.                     double curAvgSalaryE2 = e2.getValue().stream()
  86.                             .collect(Collectors.averagingDouble(a -> a.salary));
  87.  
  88.                     return Double.compare(curAvgSalaryE2, curAvgSalaryE1);
  89.  
  90.                 }).findFirst().get().getKey());
  91.  
  92.         employees.get(department).stream()
  93.                 .sorted((e1, e2) -> Double.compare(e2.salary, e1.salary))
  94.                 .forEach(e -> System.out.printf("%s %.2f %s %d%n",
  95.                         e.name,
  96.                         e.salary,
  97.                         e.email,
  98.                         e.age));
  99.     }
  100. }
RAW Paste Data