Advertisement
Guest User

Untitled

a guest
Apr 25th, 2020
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.26 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.*;
  6. import java.util.function.Function;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9. import java.util.stream.Collectors;
  10.  
  11. public class Pr01CompanyRoster {
  12.  
  13.     public static void main(String[] args) throws IOException {
  14.         Company company = new Company();
  15.  
  16.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
  17.         reader.lines()
  18.                 .limit(Long.parseLong(reader.readLine()))
  19.                 .map(EmployeeData::fromString)
  20.                 .forEach(company::addEmployee);
  21.  
  22.         Comparator<Department> departmentByHighestAverageSalaryCmp = Comparator
  23.                 .comparingDouble((Department department) ->
  24.                         department.employees()
  25.                                 .stream()
  26.                                 .mapToDouble(Employee::getSalary)
  27.                                 .average()
  28.                                 .orElse(0.0))
  29.                 .reversed();
  30.  
  31.         Comparator<Employee> employeeByHighestSalaryCmp = Comparator
  32.                 .comparingDouble(Employee::getSalary)
  33.                 .reversed();
  34.  
  35.         String departmentInfo = company
  36.                 .firstDepartmentBy(departmentByHighestAverageSalaryCmp)
  37.                 .map(departmentInfo("Highest Average Salary: ", employeeByHighestSalaryCmp))
  38.                 .orElseThrow(() -> new IllegalStateException("Empty company"));
  39.  
  40.         System.out.println(departmentInfo);
  41.     }
  42.  
  43.     private static Function<Department, String> departmentInfo(String header,
  44.                                                                Comparator<? super Employee> employeeComparator) {
  45.         return department -> header + department.getName() + System.lineSeparator() +
  46.                 department.employees().stream()
  47.                         .sorted(employeeComparator)
  48.                         .map(Pr01CompanyRoster::employeeToString)
  49.                         .collect(Collectors.joining(System.lineSeparator()));
  50.     }
  51.  
  52.     private static String employeeToString(Employee employee) {
  53.         return String.format("%s %.2f %s %d",
  54.                 employee.getName(), employee.getSalary(),
  55.                 employee.getEmail(), employee.getAge());
  56.     }
  57.  
  58.     private static final class EmployeeData {
  59.         private static final String EMPLOYEE_REGEX =
  60.                 "^(?<name>\\S+)\\s+(?<salary>\\d+\\.*\\d+)\\s+(?<position>\\S+)\\s+" +
  61.                         "(?<department>\\S+)\\s*(?<email>\\S+@\\S+\\.\\S+)?\\s*(?<age>\\d+)?$";
  62.  
  63.         private static final Pattern EMPLOYEE_PATTERN = Pattern.compile(EMPLOYEE_REGEX);
  64.  
  65.         private final String name;
  66.         private final String email;
  67.         private final int age;
  68.         private final double salary;
  69.         private final String position;
  70.         private final String department;
  71.  
  72.         private EmployeeData(String name, String email, int age, double salary, String position, String department) {
  73.             this.name = name;
  74.             this.email = email;
  75.             this.age = age;
  76.             this.salary = salary;
  77.             this.position = position;
  78.             this.department = department;
  79.         }
  80.  
  81.         public static EmployeeData fromString(String employeeString) {
  82.             Matcher matcher = EMPLOYEE_PATTERN.matcher(employeeString);
  83.  
  84.             if (!matcher.matches()) {
  85.                 throw new IllegalArgumentException("Invalid employee string: " + employeeString);
  86.             }
  87.  
  88.             String name = Objects.requireNonNull(matcher.group("name"),
  89.                     () -> "Missing name in: " + employeeString);
  90.             double salary = Double.parseDouble(Objects.requireNonNull(matcher.group("salary"),
  91.                     () -> "Missing salary in: " + employeeString));
  92.             String position = Objects.requireNonNull(matcher.group("position"),
  93.                     () -> "Missing position in: " + employeeString);
  94.             String department = Objects.requireNonNull(matcher.group("department"),
  95.                     () -> "Missing department in: " + employeeString);
  96.             String email = Objects.requireNonNullElse(matcher.group("email"), "n/a");
  97.             int age = Integer.parseInt(Objects.requireNonNullElse(matcher.group("age"), "-1"));
  98.  
  99.             return new EmployeeData(name, email, age, salary, position, department);
  100.         }
  101.  
  102.         public String getName() {
  103.             return name;
  104.         }
  105.  
  106.         public String getEmail() {
  107.             return email;
  108.         }
  109.  
  110.         public int getAge() {
  111.             return age;
  112.         }
  113.  
  114.         public double getSalary() {
  115.             return salary;
  116.         }
  117.  
  118.         public String getPosition() {
  119.             return position;
  120.         }
  121.  
  122.         public String getDepartment() {
  123.             return department;
  124.         }
  125.     }
  126.  
  127.     private static final class Company {
  128.         private final Map<String, Department> departments;
  129.  
  130.         public Company() {
  131.             departments = new HashMap<>();
  132.         }
  133.  
  134.         public void addEmployee(EmployeeData employeeData) {
  135.             String departmentName = employeeData.getDepartment();
  136.             Department department = departments.compute(departmentName,
  137.                     (name, dep) -> Objects.requireNonNullElseGet(dep, () -> new Department(departmentName)));
  138.             Employee employee = new Employee(employeeData, department);
  139.             department.addEmployee(employee);
  140.         }
  141.  
  142.         public Optional<Department> firstDepartmentBy(Comparator<? super Department> comparator) {
  143.             return departments.values().stream().min(comparator);
  144.         }
  145.     }
  146.  
  147.     private static final class Employee {
  148.         private final String name;
  149.         private final String email;
  150.         private final int age;
  151.         private final double salary;
  152.         private final String position;
  153.         private final Department department;
  154.  
  155.         public Employee(EmployeeData employeeData, Department department) {
  156.             this.name = employeeData.getName();
  157.             this.salary = employeeData.getSalary();
  158.             this.email = employeeData.getEmail();
  159.             this.age = employeeData.getAge();
  160.             this.position = employeeData.getPosition();
  161.             this.department = department;
  162.         }
  163.  
  164.         public double getSalary() {
  165.             return salary;
  166.         }
  167.  
  168.         public String getName() {
  169.             return name;
  170.         }
  171.  
  172.         public String getEmail() {
  173.             return email;
  174.         }
  175.  
  176.         public int getAge() {
  177.             return age;
  178.         }
  179.     }
  180.  
  181.     private static final class Department {
  182.         private final String name;
  183.         private final Collection<Employee> employees;
  184.  
  185.         public Department(String name) {
  186.             this.name = name;
  187.             employees = new ArrayList<>();
  188.         }
  189.  
  190.         public String getName() {
  191.             return name;
  192.         }
  193.  
  194.         public void addEmployee(Employee employee) {
  195.             employees.add(employee);
  196.         }
  197.  
  198.         public Collection<Employee> employees() {
  199.             return Collections.unmodifiableCollection(employees);
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement