Advertisement
shady_obeyd

03.Mankind-Human.cs

Feb 27th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. public class Human
  5. {
  6.     private string firstName;
  7.  
  8.     public string FirstName
  9.     {
  10.         get { return firstName; }
  11.         protected set
  12.         {
  13.             ValidateName(value, "firstName", 4);
  14.             firstName = value;
  15.         }
  16.     }
  17.  
  18.     private string lastName;
  19.  
  20.     public string LastName
  21.     {
  22.         get { return lastName; }
  23.         protected set
  24.         {
  25.             ValidateName(value, "lastName", 3);
  26.             lastName = value;
  27.         }
  28.     }
  29.  
  30.     public Human(string firstName, string lastName)
  31.     {
  32.         FirstName = firstName;
  33.         LastName = lastName;
  34.     }
  35.  
  36.     public override string ToString()
  37.     {
  38.         StringBuilder builder = new StringBuilder();
  39.  
  40.         builder.AppendLine($"First Name: {FirstName}");
  41.         builder.AppendLine($"Last Name: {LastName}");
  42.  
  43.         return builder.ToString().TrimEnd();
  44.     }
  45.  
  46.     private void ValidateName(string value, string argument, int MinNameLenght)
  47.     {
  48.         if (!char.IsUpper(value[0]))
  49.         {
  50.             throw new ArgumentException($"Expected upper case letter! Argument: {argument}");
  51.         }
  52.         else if (value?.Length < MinNameLenght)
  53.         {
  54.             throw new ArgumentException($"Expected length at least {MinNameLenght} symbols! Argument: {argument}");
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement