mellowdeep

company rooster

Jun 23rd, 2016
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. package definingClassesHomework.pr04CompanyRoster;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6. import java.util.LinkedHashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9.  
  10. class Employee implements Comparable<Employee> {
  11.  
  12.     static final String DEFAULT_EMAIL = "n/a";
  13.     static final int DEFAULT_AGE = -1;
  14.  
  15.     String name;
  16.     double salary;
  17.     String position;
  18.     String department;
  19.     String email;
  20.     int age;
  21.  
  22.     public Employee(String name, double salary, String position,
  23.             String department, String email, int age) {
  24.         this.name = name;
  25.         this.salary = salary;
  26.         this.position = position;
  27.         this.department = department;
  28.         this.email = email;
  29.         this.age = age;
  30.     }
  31.  
  32.     public Employee(String name, double salary, String position,
  33.             String department, String email) {
  34.         this(name, salary, position, department, email, DEFAULT_AGE);
  35.     }
  36.  
  37.     public Employee(String name, double salary, String position,
  38.             String department, int age) {
  39.         this(name, salary, position, department, DEFAULT_EMAIL, age);
  40.     }
  41.  
  42.     public Employee(String name, double salary, String position,
  43.             String department) {
  44.         this(name, salary, position, department, DEFAULT_EMAIL, DEFAULT_AGE);
  45.     }
  46.  
  47.     @Override
  48.     public int compareTo(Employee other) {
  49.         return Integer.compare(this.age, other.age);
  50.     }
  51.  
  52. }
  53.  
  54. public class Pr04CompanyRoster {
  55.  
  56.     public static void main(String[] args) {
  57.  
  58.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(
  59.                 System.in));) {
  60.             Map<String, List<Employee>> listOfEmployees = new LinkedHashMap<>();
  61.             int lines = Integer.parseInt(reader.readLine());
  62.             for (int i = 0; i < lines; i++) {
  63.                 String[] inputLine = reader.readLine().split(" ");
  64.                 String name = inputLine[0];
  65.                 double salary = Double.parseDouble(inputLine[1]);
  66.                 String position = inputLine[2];
  67.                 String department = inputLine[3];
  68.                 Employee employee = null;
  69.                 if (inputLine.length == 5) {
  70.                     String email = inputLine[4];
  71.                     int isEmail = email.indexOf('@');
  72.                     if (isEmail == -1) {
  73.                         int age = Integer.parseInt(email);
  74.                         employee = new Employee(name, salary, position,
  75.                                 department, age);
  76.                     } else {
  77.                         employee = new Employee(name, salary, position,
  78.                                 department, email);
  79.                     }
  80.                 } else if (inputLine.length == 6) {
  81.                     String email = inputLine[4];
  82.                     int age = Integer.parseInt(inputLine[5]);
  83.                     employee = new Employee(name, salary, position, department,
  84.                             email, age);
  85.                 } else {
  86.                     employee = new Employee(name, salary, position, department);
  87.                 }
  88.                 if (!listOfEmployees.containsKey(department)) {
  89.                     listOfEmployees.put(department, new ArrayList<>());
  90.                 }
  91.                 listOfEmployees.get(department).add(employee);
  92.             }
  93.             listOfEmployees
  94.                     .entrySet()
  95.                     .stream()
  96.                     .sorted((d1, d2) -> Double.compare(
  97.                             d2.getValue().stream().mapToDouble(x -> x.salary)
  98.                                     .average().getAsDouble(), d1.getValue()
  99.                                     .stream().mapToDouble(x -> x.salary)
  100.                                     .average().getAsDouble()))
  101.                     .limit(1)
  102.                     .forEach(
  103.                             dep -> {
  104.                                 System.out
  105.                                         .println("Highest Average Salary: "
  106.                                                 + dep.getKey());
  107.                                 List<Employee> printList = new ArrayList<>(dep
  108.                                         .getValue());
  109.                                 printList.sort((d1, d2) -> Double.compare(
  110.                                         d2.salary, d1.salary));
  111.                                 for (Employee empl : printList) {
  112.                                     System.out.printf("%s %.2f %s %d\n",
  113.                                             empl.name, empl.salary, empl.email,
  114.                                             empl.age);
  115.                                 }
  116.                             });
  117.  
  118.         } catch (Exception e) {
  119.             e.printStackTrace();
  120.         }
  121.  
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment