Advertisement
mark79

Java Fundamentals - Company Roster

Jul 26th, 2019
1,696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.18 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class CompanyRoster {
  5.  
  6.     static class Department {
  7.         String name;
  8.         List<Employee> employee;
  9.         double avgSalary;
  10.  
  11.         Department(String name, List<Employee> employee) {
  12.             this.name = name;
  13.             this.employee = employee;
  14.             this.avgSalary = employee.stream().mapToDouble(Employee::getSalary).average().getAsDouble();
  15.         }
  16.  
  17.         double getAvgSalary() {
  18.             return avgSalary;
  19.         }
  20.  
  21.         String getName() {
  22.             return name;
  23.         }
  24.  
  25.         List<Employee> getEmployee() {
  26.             return employee;
  27.         }
  28.     }
  29.  
  30.     static class Employee {
  31.         String name;
  32.         double salary;
  33.         String position;
  34.         String department;
  35.         String email;
  36.         int age;
  37.  
  38.         Employee() {
  39.             this.email = "n/a";
  40.             this.age = -1;
  41.         }
  42.  
  43.         void setName(String name) {
  44.             this.name = name;
  45.         }
  46.  
  47.         void setSalary(double salary) {
  48.             this.salary = salary;
  49.         }
  50.  
  51.         void setPosition(String position) {
  52.             this.position = position;
  53.         }
  54.  
  55.         void setDepartment(String department) {
  56.             this.department = department;
  57.         }
  58.  
  59.         void setEmail(String email) {
  60.             this.email = email;
  61.         }
  62.  
  63.         void setAge(int age) {
  64.             this.age = age;
  65.         }
  66.  
  67.         double getSalary() {
  68.             return salary;
  69.         }
  70.  
  71.         String getDepartment() {
  72.             return department;
  73.         }
  74.  
  75.         String getName() {
  76.             return name;
  77.         }
  78.  
  79.         public String getPosition() {
  80.             return position;
  81.         }
  82.  
  83.         String getEmail() {
  84.             return email;
  85.         }
  86.  
  87.         int getAge() {
  88.             return age;
  89.         }
  90.     }
  91.  
  92.     public static void main(String[] args) {
  93.         Scanner scanner = new Scanner(System.in);
  94.  
  95.         List<Employee> employees = new ArrayList<>();
  96.  
  97.         int n = Integer.parseInt(scanner.nextLine());
  98.         for (int i = 0; i < n; i++) {
  99.             String[] input = scanner.nextLine().split("\\s+");
  100.  
  101.             Employee employee = new Employee();
  102.  
  103.             employee.setName(input[0]);
  104.             employee.setSalary(Double.parseDouble(input[1]));
  105.             employee.setPosition(input[2]);
  106.             employee.setDepartment(input[3]);
  107.  
  108.             switch (input.length) {
  109.                 case 5:
  110.                     if (input[4].contains("@")) {
  111.                         employee.setEmail(input[4]);
  112.                     } else {
  113.                         employee.setAge(Integer.parseInt(input[4]));
  114.                     }
  115.                     break;
  116.                 case 6:
  117.                     if (input[4].contains("@")) {
  118.                         employee.setEmail(input[4]);
  119.                         employee.setAge(Integer.parseInt(input[5]));
  120.                     } else {
  121.                         employee.setAge(Integer.parseInt(input[4]));
  122.                         employee.setEmail(input[5]);
  123.                     }
  124.                     break;
  125.             }
  126.  
  127.             employees.add(employee);
  128.         }
  129.  
  130.         List<String> departmentsList = employees.stream().map(Employee::getDepartment).distinct().collect(Collectors.toList());
  131.  
  132.         List<Department> departments = new ArrayList<>();
  133.         for (String department : departmentsList) {
  134.             departments.add(new Department(department,
  135.                     employees.stream().filter(employee -> employee.getDepartment().equals(department)).collect(Collectors.toList())));
  136.         }
  137.  
  138.         departments.sort(Comparator.comparingDouble(Department::getAvgSalary).reversed());
  139.         Department department = departments.get(0);
  140.         department.getEmployee().sort(Comparator.comparingDouble(Employee::getSalary).reversed());
  141.  
  142.         System.out.printf("Highest Average Salary: %s%n", department.getName());
  143.         for (Employee employee : department.getEmployee()) {
  144.             System.out.printf("%s %.2f %s %d%n", employee.getName(), employee.getSalary(), employee.getEmail(), employee.getAge());
  145.         }
  146.  
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement