Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.OptionalDouble;
- /**
- *
- * @author chobi
- */
- public class Employee {
- private String name;
- private double salary;
- private String position;
- private String department;
- private String email;
- private int age;
- public Employee(Builder builder) {
- this.name = builder.name;
- this.salary = builder.salary;
- this.position = builder.position;
- this.department = builder.department;
- this.email = builder.email;
- this.age = builder.age;
- }
- public static class Builder {
- private String name;
- private double salary;
- private String position;
- private String department;
- private String email = "n/a";
- private int age = -1;
- private Builder name (String name){
- this.name = name;
- return this;
- }
- private Builder salary (Double salary){
- this.salary = salary;
- return this;
- }
- private Builder position (String position){
- this.position = position;
- return this;
- }
- private Builder department (String department){
- this.department = department;
- return this;
- }
- private Builder email (String email){
- this.email = email;
- return this;
- }
- private Builder age (Integer age){
- this.age = age;
- return this;
- }
- public Employee build() {
- return new Employee(this);
- }
- }
- public static void main(String[] args) throws Exception {
- InputStreamReader isr = new InputStreamReader(System.in);
- BufferedReader reader = new BufferedReader(isr);
- int count = Integer.parseInt(reader.readLine());
- Map<String, ArrayList<Employee>> employeeList = new HashMap<>();
- Map<String, ArrayList<Double>> averageDepartmentSalary = new HashMap<>();
- for (int i = 0; i < count; i++) {
- String[] input = reader.readLine().split("\\s+");
- addEmployee(input, employeeList);
- if (!averageDepartmentSalary.containsKey(input[3])) {
- averageDepartmentSalary.put(input[3], new ArrayList<>());
- averageDepartmentSalary.get(input[3]).add(Double.parseDouble(input[1]));
- }else{
- averageDepartmentSalary.get(input[3]).add(Double.parseDouble(input[1]));
- }
- }
- String highestAverageSalary = averageDepartmentSalary.entrySet().stream()
- .max((entry1, entry2) -> averageValue(entry1.getValue()) > averageValue(entry2.getValue()) ? 1 : -1).get().getKey();
- for (Map.Entry <String, ArrayList<Employee>> arg : employeeList.entrySet()) {
- if (arg.getKey().equals(highestAverageSalary)) {
- System.out.printf("Highest Average Salary: %s%n", arg.getKey());
- arg.getValue().
- stream().
- sorted((p1, p2) ->Double.compare(p2.salary, p1.salary)).
- forEach(employee -> System.out.printf("%s %.2f %s %d%n",
- employee.name,
- employee.salary,
- employee.email,
- employee.age));
- }
- }
- }
- private static void addEmployee(String[] input, Map<String, ArrayList<Employee>> employeeList) {
- int numStats = input.length;
- if (!employeeList.containsKey(input[3])) {
- employeeList.put(input[3], new ArrayList<>());
- }
- switch(numStats){
- case 4: employeeList.get(input[3]).add(new Employee.Builder().
- name(input[0]).
- salary(Double.parseDouble(input[1])).
- position(input[2]).
- department(input[3]).
- build());
- break;
- case 5:if (input[4].contains("@")) {
- employeeList.get(input[3]).add(new Employee.Builder().
- name(input[0]).
- salary(Double.parseDouble(input[1])).
- position(input[2]).
- department(input[3]).
- email(input[4]).
- build());
- }else{
- employeeList.get(input[3]).add(new Employee.Builder().
- name(input[0]).
- salary(Double.parseDouble(input[1])).
- position(input[2]).
- department(input[3]).
- age(Integer.parseInt(input[4])).
- build());
- }
- break;
- case 6:
- employeeList.get(input[3]).add(new Employee.Builder().
- name(input[0]).
- salary(Double.parseDouble(input[1])).
- position(input[2]).
- department(input[3]).
- email(input[4]).
- age(Integer.parseInt(input[5])).
- build());
- break;
- }
- }
- private static Double averageValue(ArrayList<Double> value) {
- return value.stream().mapToDouble(i -> i).average().orElse(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment