Advertisement
butoff

Company Roster

Feb 13th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program
  6. {
  7.     static void Main()
  8.     {
  9.         List<Employee> register = new List<Employee>();
  10.         int n = int.Parse(Console.ReadLine());
  11.         for (int i = 0; i < n; i++)
  12.         {
  13.             string[] data = Console.ReadLine().Split();
  14.             string name = data[0];
  15.             decimal salary = decimal.Parse(data[1]);
  16.             string position = data[2];
  17.             string department = data[3];
  18.             if (data.Length == 4)
  19.             {
  20.                 var newEmployee = new Employee(name, salary, position, department);
  21.                 register.Add(newEmployee);
  22.             }
  23.             else if (data.Length == 5)
  24.             {
  25.                 if (data[4].Contains("@"))
  26.                 {
  27.                     string email = data[4];
  28.                     var newEmployee = new Employee(name, salary, position, department, email);
  29.                     register.Add(newEmployee);
  30.                 }
  31.                 else
  32.                 {
  33.                     int age = int.Parse(data[4]);
  34.                     var newEmployee = new Employee(name, salary, position, department, age);
  35.                     register.Add(newEmployee);
  36.                 }
  37.             }
  38.             else if (data.Length == 6)
  39.             {
  40.                 string email = data[4];
  41.                 int age = int.Parse(data[5]);
  42.                 var newEmployee = new Employee(name, salary, position, department, email, age);
  43.                 register.Add(newEmployee);
  44.             }
  45.         }
  46.         var highest = register.GroupBy(e => e.Department)
  47.                                        .Select(group => new
  48.                                        {
  49.                                            Dep = group.Key,
  50.                                            Avg = group.Average(e => e.Salary)
  51.                                        })
  52.                                        .OrderByDescending(g => g.Avg)
  53.                                        .FirstOrDefault();
  54.  
  55.         Console.WriteLine($"Highest Average Salary: {highest.Dep}");
  56.  
  57.         foreach (var emp in register.Where(e => e.Department == highest.Dep).OrderByDescending(e => e.Salary))
  58.         {
  59.             Console.WriteLine(emp);
  60.         }
  61.  
  62.     }
  63. }
  64.  
  65.  
  66. public class Employee
  67. {
  68.     public string Name { get; set; }
  69.     public decimal Salary { get; set; }
  70.     public string Position { get; set; }
  71.     public string Department { get; set; }
  72.     public string Email { get; set; }
  73.     public int Age { get; set; }
  74.  
  75.     public Employee(string name, decimal salary, string position, string department)
  76.         : this(name, salary, position, department, "n/a", -1)
  77.     {
  78.     }
  79.  
  80.     public Employee(string name, decimal salary, string position, string department, string email)
  81.        : this(name, salary, position, department, email, -1)
  82.     {
  83.     }
  84.  
  85.     public Employee(string name, decimal salary, string position, string department, int age)
  86.        : this(name, salary, position, department, "n/a", age)
  87.     {
  88.     }
  89.  
  90.     public Employee(string name, decimal salary, string position, string department, string email, int age)
  91.     {
  92.         this.Name = name;
  93.         this.Salary = salary;
  94.         this.Position = position;
  95.         this.Department = department;
  96.         this.Email = email;
  97.         this.Age = age;
  98.     }
  99.  
  100.     public override string ToString()
  101.     {
  102.         return $"{this.Name} {this.Salary} {this.Email} {this.Age}";
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement