Advertisement
social1986

Untitled

Feb 16th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class StartUp
  6. {
  7.     public static void Main()
  8.     {
  9.         var countOfEmployees = int.Parse(Console.ReadLine());
  10.         var departments = new List<Department>();
  11.  
  12.  
  13.         for (int i = 0; i < countOfEmployees; i++)
  14.         {
  15.             var tokens = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  16.             var name = tokens[0];
  17.             var salary = decimal.Parse(tokens[1]);
  18.             var position = tokens[2];
  19.             var currentDepartment = tokens[3];
  20.  
  21.             var employee = new Employee()
  22.             {
  23.                 Name = name,
  24.                 Salary = salary,
  25.                 Position = position,
  26.                 Department = currentDepartment
  27.             };
  28.  
  29.             if (tokens.Length == 6)
  30.             {
  31.                 var email = tokens[4];
  32.                 var age = int.Parse(tokens[5]);
  33.                 employee.Email = email;
  34.                 employee.Age = age;
  35.             }
  36.             else if (tokens.Length == 5)
  37.             {
  38.                 int age;
  39.                 if (Int32.TryParse(tokens[4], out age))
  40.                 {
  41.                     employee.Age = age;
  42.                 }
  43.                 else
  44.                 {
  45.                     var email = tokens[4];
  46.                     employee.Email = email;
  47.                 }
  48.             }
  49.  
  50.             if (!departments.Any(d => d.Name == currentDepartment))
  51.             {
  52.                 var dep = new Department()
  53.                 {
  54.                     Name = currentDepartment
  55.                 };
  56.                 departments.Add(dep);
  57.             }
  58.  
  59.             var department = departments.FirstOrDefault(n => n.Name == currentDepartment);
  60.             department.AddEmployee(employee);
  61.         }
  62.  
  63.         var mostPaidDepartment = departments.OrderByDescending(s => s.AverageSalary).First();
  64.         Console.WriteLine($"Highest Average Salary: {mostPaidDepartment.Name}");
  65.  
  66.         foreach (var emp in mostPaidDepartment.Employees)
  67.         {
  68.             Console.WriteLine($"{emp.Name} {emp.Salary:f2} {emp.Email} {emp.Age}");
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement