TheBulgarianWolf

CompanyRoster

Jan 15th, 2021
721
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CompanyRoster
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<Department> departments= new List<Department>();
  12.             Console.WriteLine("Enter the number of employees you want to enter: ");
  13.             int numberOfEmployees = int.Parse(Console.ReadLine());
  14.             for(int i = 0; i < numberOfEmployees; i++)
  15.             {
  16.                 string[] input = Console.ReadLine().Split(" ");
  17.                 if(!departments.Any(d => d.DepartmentName == input[2]))
  18.                 {
  19.                     departments.Add(new Department(input[2]));
  20.                 }
  21.  
  22.                 departments.Find(d => d.DepartmentName == input[2]).AddNewEmployee(input[0], decimal.Parse(input[1]));
  23.             }
  24.  
  25.             Department bestDepartment = departments.OrderByDescending(d => d.TotalSalaries / d.Employees.Count()).First();
  26.  
  27.             Console.WriteLine("Highest average salary: " + bestDepartment.DepartmentName);
  28.             foreach(var employee in bestDepartment.Employees.OrderByDescending(e => e.Salary))
  29.             {
  30.                 Console.WriteLine(employee.Name + " {0:F2}",employee.Salary);
  31.             }
  32.  
  33.            
  34.         }
  35.     }
  36.  
  37.     class Employee
  38.     {
  39.         public Employee(string name,decimal salary)
  40.         {
  41.             Name = name;
  42.             Salary = salary;
  43.         }
  44.         public string Name { get; set; }
  45.         public decimal Salary { get; set; }
  46.  
  47.     }
  48.  
  49.     class Department
  50.     {
  51.         public string DepartmentName { get; set; }
  52.         public List<Employee> Employees { get; set; } = new List<Employee>();
  53.         public decimal TotalSalaries { get; set; }
  54.  
  55.         public void AddNewEmployee(string empName,decimal empSalary)
  56.         {
  57.             this.TotalSalaries += empSalary;
  58.             this.Employees.Add(new Employee(empName, empSalary));
  59.         }
  60.  
  61.         public Department(string departmentName)
  62.         {
  63.             this.DepartmentName = departmentName;
  64.         }
  65.  
  66.     }
  67. }
  68.  
RAW Paste Data