Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Company_Rosted
- {
- class Program
- {
- static void Main()
- {
- // READ INPUT
- int numberOfEmployees = int.Parse(Console.ReadLine());
- List<Employee> listOfEmployees = new List<Employee>();
- for (int i = 0; i < numberOfEmployees; i++)
- {
- string[] input = Console.ReadLine().Split();
- string name = input[0];
- double salary = double.Parse(input[1]);
- string dep = input[2];
- var newEmployee = new Employee(name, salary, dep);
- listOfEmployees.Add(newEmployee);
- }
- //CALCULATE THE HIGHEST AVERAGE SALARY
- listOfEmployees = listOfEmployees.OrderBy(x => x.Department).ToList();
- var departments = new Dictionary<string, double>();
- int count = 1;
- string department = string.Empty;
- double maxAvarage = double.MinValue;
- for (int i = 0; i < listOfEmployees.Count; i++)
- {
- if (!departments.ContainsKey(listOfEmployees[i].Department))
- {
- string newDepartment = listOfEmployees[i].Department;
- double newSlary = listOfEmployees[i].Salary;
- departments.Add(newDepartment, newSlary);
- }
- else
- {
- departments[listOfEmployees[i].Department] += listOfEmployees[i].Salary;
- count++;
- if (listOfEmployees[i].Department != listOfEmployees[i+1].Department || i==listOfEmployees.Count-1)
- {
- departments[listOfEmployees[i].Department] /= count;
- if (departments[listOfEmployees[i].Department]>maxAvarage)
- {
- maxAvarage = departments[listOfEmployees[i].Department];
- department = listOfEmployees[i].Department;
- }
- count = 1;
- }
- }
- }
- //PRINT OUTPUT
- listOfEmployees = listOfEmployees
- .Where(x => x.Department == department)
- .OrderByDescending(x=>x.Salary)
- .ToList();
- Console.WriteLine($"Highest Average Salary: {department}");
- foreach (var men in listOfEmployees)
- {
- Console.WriteLine($"{men.Name} {men.Salary:f2}");
- }
- }
- }
- class Employee
- {
- public string Name { get; set; }
- public double Salary { get; set; }
- public string Department { get; set; }
- public Employee(string name, double salary, string department)
- {
- this.Name = name;
- this.Salary = salary;
- this.Department = department;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement