Advertisement
constk

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

Nov 20th, 2021
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Text;
  5.  
  6. namespace Task2
  7. {
  8.     public class User : IEquatable<User>
  9.     {
  10.         private string _name;
  11.         private string _lastName;
  12.         private string _patronymic;
  13.         private DateTime _dateOfBirth;
  14.  
  15.         public string Name
  16.         {
  17.             get => _name;
  18.             set
  19.             {
  20.                 if (value == null)
  21.                     throw new ArgumentNullException("Имя не может быть null");
  22.                 if (value.Length < 1)
  23.                     throw new ArgumentException("Имя не может быть пустым!");
  24.  
  25.                 _name = new string(value);
  26.             }
  27.         }
  28.  
  29.         public string LastName
  30.         {
  31.             get => _lastName;
  32.             set
  33.             {
  34.                 if (value == null)
  35.                     throw new ArgumentNullException("Имя не может быть null");
  36.                 if (value.Length < 1)
  37.                     throw new ArgumentException("Имя не может быть пустым!");
  38.  
  39.                 _lastName = new string(value);
  40.             }
  41.         }
  42.  
  43.         public string Patronymic
  44.         {
  45.             get => _patronymic;
  46.             set
  47.             {
  48.                 if (value == null)
  49.                     throw new ArgumentNullException("Имя не может быть null");
  50.                 if (value.Length < 1)
  51.                     throw new ArgumentException("Имя не может быть пустым!");
  52.  
  53.                 _patronymic = new string(value);
  54.             }
  55.         }
  56.  
  57.         public int Age
  58.         {
  59.             get
  60.             {
  61.                 TimeSpan timeSpan = DateTime.Now - _dateOfBirth;
  62.                 return (int)(timeSpan.TotalDays / 364.75);
  63.             }
  64.         }
  65.  
  66.         public User(DateTime dateOfBirth, string name, string lastName, string patronymic)
  67.         {
  68.             if (dateOfBirth > DateTime.Now)
  69.                 throw new ArgumentException("Данный пользователь ещё не мог родиться!");
  70.  
  71.             _dateOfBirth = dateOfBirth;
  72.             Name = name;
  73.             LastName = lastName;
  74.             Patronymic = patronymic;
  75.         }
  76.  
  77.         public override string ToString() =>
  78.             "Имя: " + Name + "\n" +
  79.             "Фамилия: " + LastName + "\n" +
  80.             "Отчество: " + Patronymic + "\n" +
  81.             "Возраст: " + Age + "\n" +
  82.             "Дата рождения: " + _dateOfBirth.ToString() + "\n";
  83.  
  84.         public bool Equals(User other)
  85.         {
  86.             if (other is null)
  87.                 return false;
  88.  
  89.             if (ReferenceEquals(this, other))
  90.                 return true;
  91.  
  92.             if (this.GetType() != other.GetType())
  93.                 return false;
  94.  
  95.             if (this.Name == other.Name && this.LastName == other.LastName &&
  96.                 this.Patronymic == other.Patronymic && this.Age == other.Age)
  97.                 return true;
  98.            
  99.             return false;
  100.         }
  101.  
  102.         public override bool Equals(object obj)
  103.         {
  104.             if (obj is null)
  105.                 return false;
  106.  
  107.             if (ReferenceEquals(this, obj))
  108.                 return true;
  109.  
  110.             if (this.GetType() != obj.GetType())
  111.                 return false;
  112.  
  113.             return this.Equals(obj as User);
  114.         }
  115.  
  116.         public override int GetHashCode()
  117.         {
  118.             return HashCode.Combine(Name, LastName, Patronymic, Age);
  119.         }
  120.  
  121.         public static bool operator ==(User user1, User user2)
  122.         {
  123.             if (user1 is null || user2 is null)
  124.                 return false;
  125.            
  126.             return user1.Equals(user2);
  127.         }
  128.  
  129.         public static bool operator !=(User user1, User user2)
  130.         {
  131.             return !(user1 == user2);
  132.         }
  133.     }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement