Advertisement
dimipan80

Persons

Jun 1st, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. /* Define a class Person that has name, age and email. The name and age are mandatory. The email is optional. Define properties that accept non-empty name and age in the range [1 ... 100]. In case of invalid arguments, throw an exception. Define a property for the email that accepts either null or non-empty string containing '@'. Define two constructors. The first constructor should take name, age and email. The second constructor should take name and age only and call the first constructor. Implement the ToString() method to enable printing persons at the console. */
  2.  
  3. namespace _01.Persons
  4. {
  5.     using System;
  6.  
  7.     class Person
  8.     {
  9.         #region Fields
  10.         private string name;
  11.         private byte age;
  12.         private string email;
  13.         #endregion
  14.  
  15.         #region Constructor Chaining
  16.         public Person(string name, byte age, string email)
  17.         {
  18.             this.Name = name;
  19.             this.Age = age;
  20.             this.Email = email;
  21.         }
  22.  
  23.         public Person(string name, byte age)
  24.             : this(name, age, null)
  25.         {
  26.         }
  27.         #endregion
  28.  
  29.         #region Properties
  30.         public string Name
  31.         {
  32.             get { return this.name; }
  33.             set
  34.             {
  35.                 if (string.IsNullOrEmpty(value))
  36.                 {
  37.                     throw new ArgumentException("Invalid name!");
  38.                 }
  39.  
  40.                 this.name = value;
  41.             }
  42.         }
  43.  
  44.         public byte Age
  45.         {
  46.             get { return this.age; }
  47.             set
  48.             {
  49.                 if (value < 1 || value > 100)
  50.                 {
  51.                     throw new ArgumentException("Invalid age!");
  52.                 }
  53.  
  54.                 this.age = value;
  55.             }
  56.         }
  57.  
  58.         public string Email
  59.         {
  60.             get { return this.email; }
  61.             set
  62.             {
  63.                 if (!string.IsNullOrEmpty(value) && value.IndexOf('@') == -1)
  64.                 {
  65.                     throw new ArgumentException("Invalid email!");
  66.                 }
  67.  
  68.                 this.email = value;
  69.             }
  70.         }
  71.         #endregion
  72.  
  73.         public override string ToString()
  74.         {
  75.             return string.Format("{0} is {1} years old. Email: {2}",
  76.                 this.Name, this.Age, this.Email ?? "none");
  77.         }
  78.     }
  79.  
  80.     class Persons
  81.     {
  82.         static void Main(string[] args)
  83.         {
  84.             Person p1 = new Person("Svetlin", 36);
  85.             Console.WriteLine(p1);
  86.  
  87.             Person p2 = new Person("Nakov", 36, "nakov@gmail.com");
  88.             Console.WriteLine(p2);
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement