Advertisement
Guest User

Company Roster

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