Advertisement
iskren_penev

CompanyRoster

Jun 21st, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 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. namespace _04.CompanyRoster
  8. {
  9.     public class CompanyRoster
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             HashSet<string> departments = new HashSet<string>();
  14.             List<Employee> employees = new List<Employee>();
  15.             int n = int.Parse(Console.ReadLine());
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 string[] tokens = Console.ReadLine()
  19.                     .Split(new char[] { ' ', '\t', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  20.                 string name = tokens[0];
  21.                 decimal salary = decimal.Parse(tokens[1]);
  22.                 string position = tokens[2];
  23.                 string department = tokens[3];
  24.                 Employee emp = new Employee(name, salary, position, department);
  25.                 if (tokens.Length == 5)
  26.                 {
  27.                     if (tokens[4].Contains("@"))
  28.                     {
  29.                         emp.email = tokens[4];
  30.                     }
  31.                     else
  32.                     {
  33.                         int age;
  34.                         bool isNumber = int.TryParse(tokens[4], out age);
  35.                         if (isNumber)
  36.                         {
  37.                             emp.age = age;
  38.                         }
  39.                     }
  40.                 }
  41.                 else if (tokens.Length == 6)
  42.                 {
  43.                     emp.email = tokens[4];
  44.                     emp.age = int.Parse(tokens[5]);
  45.                 }
  46.                 employees.Add(emp);
  47.                 departments.Add(department);
  48.             }
  49.             decimal avgMaxSalary = 0;
  50.             string maxSalaryDepartment = string.Empty;
  51.             foreach (var dep in departments)
  52.             {
  53.                 decimal avgSalary = 0;
  54.                 int emps = 0;
  55.                 foreach (var emp in employees)
  56.                 {
  57.                     if (emp.department == dep)
  58.                     {
  59.                         emps++;
  60.                         avgSalary += emp.salary;
  61.                     }
  62.                 }
  63.                 avgSalary /= emps;
  64.                 if (avgSalary > avgMaxSalary)
  65.                 {
  66.                     avgMaxSalary = avgSalary;
  67.                     maxSalaryDepartment = dep;
  68.                 }
  69.             }
  70.             Console.WriteLine("Highest Average Salary: {0}", maxSalaryDepartment);
  71.             employees = employees
  72.                 .Where(emp => emp.department == maxSalaryDepartment)
  73.                 .OrderByDescending(emp => emp.salary)
  74.                 .ToList();
  75.             foreach (var emp in employees)
  76.             {
  77.                 Console.WriteLine("{0} {1:F2} {2} {3}",
  78.                     emp.name, emp.salary, emp.email, emp.age);
  79.             }
  80.         }
  81.     }
  82.  
  83.     public class Employee
  84.     {
  85.         public string name;
  86.         public decimal salary;
  87.         public string position;
  88.         public string department;
  89.         public string email { get; set; }
  90.         public int age { get; set; }
  91.  
  92.         public Employee(string name, decimal salary,
  93.             string position, string department)
  94.         {
  95.             this.name = name;
  96.             this.salary = salary;
  97.             this.position = position;
  98.             this.department = department;
  99.             this.email = "n/a";
  100.             this.age = -1;
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement