Advertisement
constk

Класс Employee для Антона (тема 10)

Nov 20th, 2021
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Task2
  6. {
  7.     public class Employee : User, IEquatable<Employee>
  8.     {
  9.         private string _title;
  10.         private DateTime _hireDate;
  11.  
  12.         public int Experience
  13.         {
  14.             get
  15.             {
  16.                 TimeSpan timeSpan = DateTime.Now - _hireDate;
  17.                 return (int)(timeSpan.TotalDays / 364.75);
  18.             }
  19.         }
  20.  
  21.         public string Title
  22.         {
  23.             get => _title;
  24.             set
  25.             {
  26.                 if (value == null)
  27.                     throw new ArgumentNullException("Должность не может быть null");
  28.                 if (value.Length < 1)
  29.                     throw new ArgumentException("Должность не может быть пустой!");
  30.  
  31.                 _title = new string(value);
  32.             }
  33.         }
  34.  
  35.         public Employee(DateTime dateOfBirth, string name, string lastName, string patronymic, string title, DateTime hireDate)
  36.             : base(dateOfBirth, name, lastName, patronymic)
  37.         {
  38.             if (hireDate > DateTime.Now)
  39.                 throw new ArgumentException("Данный пользователь ещё не мог быть нанят!");
  40.  
  41.             Title = title;
  42.             _hireDate = hireDate;
  43.         }
  44.  
  45.         public override string ToString() =>
  46.             base.ToString() +
  47.             "должность: " + Title + "\n" +
  48.             "опыт: " + Experience + " года\n" +
  49.             "нанят:" + _hireDate + "\n";
  50.  
  51.         public bool Equals(Employee other)
  52.         {
  53.             if (other is null)
  54.                 return false;
  55.  
  56.             if (ReferenceEquals(this, other))
  57.                 return true;
  58.  
  59.             if (this.GetType() != other.GetType())
  60.                 return false;
  61.  
  62.             return (base.Equals(other) && this.Experience == other.Experience
  63.                 && this.Title == other.Title);
  64.         }
  65.  
  66.         public override bool Equals(object obj)
  67.         {
  68.             if (obj is null)
  69.                 return false;
  70.  
  71.             if (ReferenceEquals(this, obj))
  72.                 return true;
  73.  
  74.             if (this.GetType() != obj.GetType())
  75.                 return false;
  76.  
  77.             return this.Equals(obj as Employee);
  78.         }
  79.  
  80.         public override int GetHashCode()
  81.         {
  82.             return HashCode.Combine(base.GetHashCode(), Experience, Title);
  83.         }
  84.  
  85.         public static bool operator ==(Employee employee1, Employee employee2)
  86.         {
  87.             if (employee1 is null || employee2 is null)
  88.                 return false;
  89.  
  90.             return employee1.Equals(employee2);
  91.         }
  92.  
  93.         public static bool operator !=(Employee employee1, Employee employee2)
  94.         {
  95.             return !(employee1 == employee2);
  96.         }
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement