Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Text;
- namespace Task2
- {
- public class User : IEquatable<User>
- {
- private string _name;
- private string _lastName;
- private string _patronymic;
- private DateTime _dateOfBirth;
- public string Name
- {
- get => _name;
- set
- {
- if (value == null)
- throw new ArgumentNullException("Имя не может быть null");
- if (value.Length < 1)
- throw new ArgumentException("Имя не может быть пустым!");
- _name = new string(value);
- }
- }
- public string LastName
- {
- get => _lastName;
- set
- {
- if (value == null)
- throw new ArgumentNullException("Имя не может быть null");
- if (value.Length < 1)
- throw new ArgumentException("Имя не может быть пустым!");
- _lastName = new string(value);
- }
- }
- public string Patronymic
- {
- get => _patronymic;
- set
- {
- if (value == null)
- throw new ArgumentNullException("Имя не может быть null");
- if (value.Length < 1)
- throw new ArgumentException("Имя не может быть пустым!");
- _patronymic = new string(value);
- }
- }
- public int Age
- {
- get
- {
- TimeSpan timeSpan = DateTime.Now - _dateOfBirth;
- return (int)(timeSpan.TotalDays / 364.75);
- }
- }
- public User(DateTime dateOfBirth, string name, string lastName, string patronymic)
- {
- if (dateOfBirth > DateTime.Now)
- throw new ArgumentException("Данный пользователь ещё не мог родиться!");
- _dateOfBirth = dateOfBirth;
- Name = name;
- LastName = lastName;
- Patronymic = patronymic;
- }
- public override string ToString() =>
- "Имя: " + Name + "\n" +
- "Фамилия: " + LastName + "\n" +
- "Отчество: " + Patronymic + "\n" +
- "Возраст: " + Age + "\n" +
- "Дата рождения: " + _dateOfBirth.ToString() + "\n";
- public bool Equals(User other)
- {
- if (other is null)
- return false;
- if (ReferenceEquals(this, other))
- return true;
- if (this.GetType() != other.GetType())
- return false;
- if (this.Name == other.Name && this.LastName == other.LastName &&
- this.Patronymic == other.Patronymic && this.Age == other.Age)
- return true;
- return false;
- }
- public override bool Equals(object obj)
- {
- if (obj is null)
- return false;
- if (ReferenceEquals(this, obj))
- return true;
- if (this.GetType() != obj.GetType())
- return false;
- return this.Equals(obj as User);
- }
- public override int GetHashCode()
- {
- return HashCode.Combine(Name, LastName, Patronymic, Age);
- }
- public static bool operator ==(User user1, User user2)
- {
- if (user1 is null || user2 is null)
- return false;
- return user1.Equals(user2);
- }
- public static bool operator !=(User user1, User user2)
- {
- return !(user1 == user2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement