Advertisement
desislava_topuzakova

Company.cs

May 9th, 2020
1,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Exam_Preparation1
  7. {
  8.     public class Company
  9.     {
  10.         private string name;
  11.         private List<Employee> employees;
  12.  
  13.         public Company(string name)
  14.         {
  15.             Name = name;
  16.             employees = new List<Employee>();
  17.         }
  18.  
  19.         public string Name
  20.         {
  21.             get
  22.             {
  23.                 return name;
  24.             }
  25.             set
  26.             {
  27.                 if (value.Length < 3)
  28.                 {
  29.                     throw ArgumentException("Invalid name");
  30.                 }
  31.                 name = value;
  32.             }
  33.         }
  34.  
  35.         private Exception ArgumentException(string v)
  36.         {
  37.             throw new NotImplementedException();
  38.         }
  39.  
  40.         public List<Employee> Employees
  41.         {
  42.             get
  43.             {
  44.                 return employees;
  45.             }
  46.             set
  47.             {
  48.                 employees = value;
  49.             }
  50.         }
  51.  
  52.         public override string ToString()
  53.         {
  54.             string result = "";
  55.             if (employees.Count <= 0)
  56.             {
  57.                 result += $"Company {name} has 0 employees and they are:\n";
  58.                 result += "N/A";              
  59.             }
  60.             else
  61.             {
  62.                 result += $"Company {name} has {employees.Count} employees and they are:\n";
  63.                 foreach(Employee employee in employees)
  64.                 {
  65.                     result += employee.ToString() + "\n";
  66.                 }
  67.             }
  68.  
  69.             return result;
  70.         }
  71.  
  72.         public void Hire(Employee employee)
  73.         {
  74.             employees.Add(employee);
  75.         }
  76.  
  77.         public bool Fire(Employee employee)
  78.         {
  79.             return employees.Remove(employee);
  80.         }
  81.  
  82.         public double CalculateSalaries()
  83.         {
  84.             //return employees.Sum(employee => employee.Salary);
  85.             double totalSalaries = 0;
  86.             foreach(Employee employee in employees)
  87.             {
  88.                 totalSalaries += employee.Salary;
  89.             }
  90.  
  91.             return totalSalaries;
  92.         }
  93.  
  94.         public Employee GetEmployeeWithHighestSalary()
  95.         {
  96.             return employees.OrderByDescending(emp => emp.Salary).First();
  97.         }
  98.  
  99.         public bool CheckEmployeeIsHired(string name)
  100.         {
  101.             foreach(Employee employee in employees)
  102.             {
  103.                 if (name == employee.Name)
  104.                 {
  105.                     return true;
  106.                 }
  107.             }
  108.  
  109.             return false;
  110.         }
  111.  
  112.         public void RenameCompany(string newName)
  113.         {
  114.             Name = newName;
  115.         }
  116.  
  117.         public void FireAllEmployees()
  118.         {
  119.             employees.Clear();
  120.         }
  121.  
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement