Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. public class Employee
  8. {
  9.     //mantadory
  10.     public string name;
  11.     public decimal salary;
  12.     public string position;
  13.     public string department;
  14.  
  15.     //optional
  16.     public string email;
  17.     public int age;
  18.  
  19.     public Employee(string name, decimal salary, string position, string department)
  20.     {
  21.         this.name = name;
  22.         this.salary = salary;
  23.         this.position = position;
  24.         this.department = department;
  25.         this.email = "n/a";
  26.         this.age = -1;
  27.     }
  28.  
  29.     public Employee(string email, string name, decimal salary, string position, string department)
  30.            : this(name, salary, position, department)
  31.     {
  32.         this.email = email;
  33.     }
  34.  
  35.     public Employee(int age, string email, string name, decimal salary, string position, string department)
  36.         : this(email, name, salary, position, department)
  37.     {
  38.         this.age = age;
  39.     }
  40.  
  41. }
  42.  
  43. public class CompanyRoster
  44. {
  45.  
  46.     public static void Main()
  47.     {
  48.         var numberOfEmployess = int.Parse(Console.ReadLine());
  49.         var lstEmployess = new List<Employee>();
  50.         for (int i = 0; i < numberOfEmployess; i++)
  51.         {
  52.             var inputEmployess = Console.ReadLine()
  53.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  54.             var employee = new Employee(inputEmployess[0], decimal.Parse(inputEmployess[1]), inputEmployess[2], inputEmployess[3]);
  55.  
  56.             if (inputEmployess.Length == 5)
  57.             {
  58.                 if (inputEmployess[4].Contains("@"))
  59.                 {
  60.                     employee.email = inputEmployess[4];
  61.                 }
  62.                 else
  63.                 {
  64.                     employee.age = int.Parse(inputEmployess[4]);
  65.                 }
  66.             }
  67.             else if (inputEmployess.Length == 6)
  68.             {
  69.                 employee.email = inputEmployess[4];
  70.                 employee.age = int.Parse(inputEmployess[5]);
  71.             }
  72.  
  73.             lstEmployess.Add(employee);
  74.         }
  75.         var result = lstEmployess
  76.             .GroupBy(e => e.department)
  77.             .Select(e => new
  78.             {
  79.                 Department = e.Key,
  80.                 Salary = e.Average(emp => emp.salary),
  81.                 Employess = e.OrderByDescending(emp => emp.salary)
  82.             })
  83.             .OrderByDescending(e=>e.Salary)
  84.             .FirstOrDefault();
  85.  
  86.         Console.WriteLine($"Highest Average Salary: {result.Department}");
  87.         foreach (var employe in result.Employess)
  88.         {
  89.             Console.WriteLine($"{employe.name} {employe.salary:F2} {employe.email} {employe.age}");
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement