Advertisement
Danny_Berova

06.CompanyRooster-StartUp

Feb 14th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class StartUp
  6. {
  7.     static void Main()
  8.     {
  9.         int numOfEmployees = int.Parse(Console.ReadLine());
  10.  
  11.         List<Employee> employees = new List<Employee>();
  12.  
  13.         for (int i = 0; i < numOfEmployees; i++)
  14.         {
  15.             var tokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  16.  
  17.             var employee = new Employee(tokens[0],
  18.                           decimal.Parse(tokens[1]),
  19.                                         tokens[2],
  20.                                         tokens[3]);
  21.  
  22.             if (tokens.Length > 4)
  23.             {
  24.                 var ageOrEmail = tokens[4];
  25.                 if (ageOrEmail.Contains("@"))
  26.                 {
  27.                     employee.Email = ageOrEmail;
  28.                 }
  29.                 else
  30.                 {
  31.                     employee.Age = int.Parse(ageOrEmail);
  32.                 }
  33.             }
  34.  
  35.             if (tokens.Length > 5)
  36.             {
  37.                 employee.Age = int.Parse(tokens[5]);
  38.             }
  39.  
  40.             employees.Add(employee);            
  41.         }
  42.  
  43.         //inspired by master Kenov - with additions
  44.  
  45.         var result = employees
  46.                      .GroupBy(e => e.Department)
  47.                      .Select(e => new {Department = e.Key,
  48.                                       AvgSalary = e.Average(emp => emp.Salary),
  49.                                       Empls = e.OrderByDescending(emp => emp.Salary)})
  50.                      .ToList()
  51.                      .OrderByDescending(e => e.AvgSalary)
  52.                      .FirstOrDefault();
  53.  
  54.         Console.WriteLine($"Highest Average Salary: {result.Department}");
  55.  
  56.         foreach (var empl in result.Empls)
  57.         {
  58.             Console.WriteLine($"{empl.Name} {empl.Salary:f2} {empl.Email} {empl.Age}");
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement