deyanmalinov

04. Company Roster

Jun 5th, 2020
748
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DPM;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.         int lines = Integer.parseInt(scan.nextLine());
  9.         HashMap<String, Department> deparments = new HashMap<>();
  10.         while (lines-- > 0 ){
  11.             String[] line = scan.nextLine().split(" ");
  12.             String departmentName = line[3];
  13.             Employee emp = new Employee(line[0],Double.parseDouble(line[1]),line[2]);
  14.             if (line.length==5){
  15.                 if (Character.isDigit(line[4].charAt(0))){
  16.                     emp.setAge(Integer.parseInt(line[4]));
  17.                 }else {
  18.                     emp.setEmail(line[4]);
  19.                 }
  20.             }else if (line.length==6){
  21.                 emp.setEmail(line[4]);
  22.                 emp.setAge(Integer.parseInt(line[5]));
  23.             }
  24.             if (!deparments.containsKey(departmentName)){
  25.                 deparments.put(departmentName, new Department());
  26.             }
  27.             deparments.get(departmentName).addEmployee(emp);
  28.         }
  29.         Map.Entry<String, Department> bigSal= deparments.entrySet().stream().sorted((first, second) ->{
  30.             int result = 0;
  31.             if (second.getValue().getAvaregeSalery() > first.getValue().getAvaregeSalery()){
  32.                 result = 1;
  33.             }else if (second.getValue().getAvaregeSalery() < first.getValue().getAvaregeSalery()){
  34.                 result = -1;
  35.             }
  36.             return result;
  37.         }).findFirst().get();
  38.         System.out.println(String.format("Highest Average Salary: %s", bigSal.getKey()));
  39.         bigSal.getValue().getEmployees().stream().sorted((f,s) -> Double.compare(s.getSalary(), f.getSalary())).forEach(employee -> {
  40.             System.out.println(String.format("%s %.2f %s %d", employee.getName(), employee.getSalary(), employee.getEmail(), employee.getAge()));
  41.         });
  42.     }
  43. }
  44. -----------------------------------------------------------------------------------
  45. package DPM;
  46. public class Employee {
  47.     private String name;
  48.     private double salary;
  49.     private String position;
  50.     private String email;
  51.     private int age;
  52.     public Employee(String name, double salary, String position){
  53.         this.name=name;
  54.         this.salary=salary;
  55.         this.position=position;
  56.         this.email="n/a";
  57.         this.age=-1;
  58.     }
  59.     public void setEmail (String email){
  60.         this.email=email;
  61.     }
  62.     public void setAge(int age){
  63.         this.age=age;
  64.     }
  65.     public double getSalary(){
  66.         return this.salary;
  67.     }
  68.     public String getName(){
  69.         return this.name;
  70.     }
  71.     public String getEmail() {
  72.         return this.email;
  73.     }
  74.     public int getAge() {
  75.         return this.age;
  76.     }
  77. }
  78. ----------------------------------------------------------------------
  79. package DPM;
  80. import java.util.ArrayList;
  81. import java.util.List;
  82. public class Department {
  83.     private List<Employee> employees;
  84.     public Department(){
  85.         this.employees = new ArrayList<>();
  86.     }
  87.     public List<Employee> getEmployees(){
  88.         return this.employees;
  89.     }
  90.     public void addEmployee(Employee employee){
  91.         this.employees.add(employee);
  92.     }
  93.     public double getAvaregeSalery(){
  94.         return this.employees.stream().mapToDouble(e -> e.getSalary()).average().getAsDouble();
  95.     }
  96. }
RAW Paste Data