Advertisement
Filkolev

Persons

Dec 25th, 2014
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. class Persons
  5. {
  6.     class Person
  7.     {
  8.         private string name;
  9.         private int age;
  10.         private string email;
  11.  
  12.         public string Name
  13.         {
  14.             get
  15.             {
  16.                 return this.name;
  17.             }
  18.             set
  19.             {
  20.                 if (String.IsNullOrWhiteSpace(value))
  21.                 {
  22.                     throw new ArgumentException("Name cannot be empty.");
  23.                 }
  24.  
  25.                 if (!ValidateName(value))
  26.                 {
  27.                     throw new ArgumentException("Name should contain only Latin letters, spaces and hyphens and start with an uppercase letter.");
  28.                 }
  29.  
  30.                 this.name = value;
  31.             }
  32.         }
  33.  
  34.         public int Age
  35.         {
  36.             get
  37.             {
  38.                 return this.age;
  39.             }
  40.             set
  41.             {
  42.                 if (!ValidateAge(value))
  43.                 {
  44.                     throw new ArgumentOutOfRangeException("Age should be an integer between 1 and 100 inclusive.");
  45.                 }
  46.  
  47.                 this.age = value;
  48.             }
  49.         }
  50.  
  51.         public string Email
  52.         {
  53.             get
  54.             {
  55.                 return this.email;
  56.             }
  57.             set
  58.             {
  59.                 if (String.IsNullOrWhiteSpace(value))
  60.                 {
  61.                     throw new ArgumentException("Email cannot be empty.");
  62.                 }
  63.  
  64.                 if (!ValidateEmail(value))
  65.                 {
  66.                     throw new ArgumentException("Email could not be validated."); // Put a more descriptive message
  67.                 }
  68.  
  69.                 this.email = value;
  70.             }
  71.         }
  72.  
  73.         private bool ValidateName(string name)
  74.         {
  75.             Regex nameMatcher = new Regex(@"^[A-Z][a-zA-Z- ]+$");
  76.             return nameMatcher.IsMatch(name);
  77.         }
  78.  
  79.         private bool ValidateAge(int age)
  80.         {
  81.             if (age < 1 || age > 100)
  82.             {
  83.                 return false;
  84.             }
  85.             return true;
  86.         }
  87.  
  88.         private bool ValidateEmail(string email)
  89.         {
  90.             // For info on the regex check out http://www.regular-expressions.info/email.html
  91.             Regex emailMatcher = new Regex(@"^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");
  92.             return emailMatcher.IsMatch(email);
  93.         }
  94.  
  95.         public Person(string name, int age)
  96.         {
  97.             this.Name = name;
  98.             this.Age = age;
  99.         }
  100.  
  101.         public Person(string name, int age, string email)
  102.         {
  103.             this.Name = name;
  104.             this.Age = age;
  105.             this.Email = email;
  106.         }
  107.  
  108.         public override string ToString()
  109.         {
  110.             return "This is a person called " + this.name + ". He/she is " + this.age + " years old. His/her email address is " + (string.IsNullOrEmpty(this.email) ? "not set." : this.email);
  111.         }
  112.  
  113.         public void IntroducePerson()
  114.         {
  115.             Console.WriteLine("Hi, my name is {0} and I'm {1} years old.", this.name, this.age);
  116.         }
  117.     }
  118.  
  119.     static void Main()
  120.     {
  121.         Person gosho = new Person("Gosho", 29); // using first constructor
  122.         Person pesho = new Person("Pesho", 33, "pesho@mail.bg"); // using second constructor
  123.  
  124.         Console.WriteLine(gosho); // print a person using the overridden ToString() method for the Person class
  125.         Console.WriteLine(pesho);
  126.         Console.WriteLine(pesho.Name);
  127.         pesho.IntroducePerson(); // use a non-static method from the Person class
  128.  
  129.         // Person withBadName = new Person("few", 12); // throws exception because name is not valid
  130.         // Person withBadAge = new Person("Hey", "not age"); // compile-time error - no overload for the Person constructor takes two strings; age is mandatory
  131.         // Person withOutOfRangeAge = new Person("Hello", 122); // throws ArgumentOutOfRangeException because age is not in the accepted interval [1 ... 100]
  132.         // Person withBadEmail = new Person("Hey", 100, "asdf"); // throws exception because email is not valid
  133.         // Person withEmptyName = new Person("", 22); // throws exception because name is empty
  134.         // Person withEmptyEmail = new Person("Baba Ilyitsa", 1, ""); // throws exception because email is empty
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement