Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package definingClassesHomework.pr04CompanyRoster;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- class Employee implements Comparable<Employee> {
- static final String DEFAULT_EMAIL = "n/a";
- static final int DEFAULT_AGE = -1;
- String name;
- double salary;
- String position;
- String department;
- String email;
- int age;
- public Employee(String name, double salary, String position,
- String department, String email, int age) {
- this.name = name;
- this.salary = salary;
- this.position = position;
- this.department = department;
- this.email = email;
- this.age = age;
- }
- public Employee(String name, double salary, String position,
- String department, String email) {
- this(name, salary, position, department, email, DEFAULT_AGE);
- }
- public Employee(String name, double salary, String position,
- String department, int age) {
- this(name, salary, position, department, DEFAULT_EMAIL, age);
- }
- public Employee(String name, double salary, String position,
- String department) {
- this(name, salary, position, department, DEFAULT_EMAIL, DEFAULT_AGE);
- }
- @Override
- public int compareTo(Employee other) {
- return Integer.compare(this.age, other.age);
- }
- }
- public class Pr04CompanyRoster {
- public static void main(String[] args) {
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(
- System.in));) {
- Map<String, List<Employee>> listOfEmployees = new LinkedHashMap<>();
- int lines = Integer.parseInt(reader.readLine());
- for (int i = 0; i < lines; i++) {
- String[] inputLine = reader.readLine().split(" ");
- String name = inputLine[0];
- double salary = Double.parseDouble(inputLine[1]);
- String position = inputLine[2];
- String department = inputLine[3];
- Employee employee = null;
- if (inputLine.length == 5) {
- String email = inputLine[4];
- int isEmail = email.indexOf('@');
- if (isEmail == -1) {
- int age = Integer.parseInt(email);
- employee = new Employee(name, salary, position,
- department, age);
- } else {
- employee = new Employee(name, salary, position,
- department, email);
- }
- } else if (inputLine.length == 6) {
- String email = inputLine[4];
- int age = Integer.parseInt(inputLine[5]);
- employee = new Employee(name, salary, position, department,
- email, age);
- } else {
- employee = new Employee(name, salary, position, department);
- }
- if (!listOfEmployees.containsKey(department)) {
- listOfEmployees.put(department, new ArrayList<>());
- }
- listOfEmployees.get(department).add(employee);
- }
- listOfEmployees
- .entrySet()
- .stream()
- .sorted((d1, d2) -> Double.compare(
- d2.getValue().stream().mapToDouble(x -> x.salary)
- .average().getAsDouble(), d1.getValue()
- .stream().mapToDouble(x -> x.salary)
- .average().getAsDouble()))
- .limit(1)
- .forEach(
- dep -> {
- System.out
- .println("Highest Average Salary: "
- + dep.getKey());
- List<Employee> printList = new ArrayList<>(dep
- .getValue());
- printList.sort((d1, d2) -> Double.compare(
- d2.salary, d1.salary));
- for (Employee empl : printList) {
- System.out.printf("%s %.2f %s %d\n",
- empl.name, empl.salary, empl.email,
- empl.age);
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment