Advertisement
Guest User

Untitled

a guest
Jan 13th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. class Persons
  10. {
  11. private string name = null;
  12. private int age = 0;
  13. private string email = null;
  14.  
  15. public string Name
  16. {
  17.  
  18. get { return this.name; }
  19. set
  20. {
  21. if (String.IsNullOrEmpty(value))
  22. {
  23. throw new Exception("Name cannot be empty!");
  24. }
  25.  
  26. this.name = value;
  27. }
  28. }
  29. public int Age
  30. {
  31.  
  32. get { return this.age; }
  33. set
  34. {
  35. if (this.age < 1 || this.age > 100)
  36. {
  37.  
  38. throw new ArgumentOutOfRangeException ("Age shoud be in range 1...100!");
  39. }
  40.  
  41. this.age = value;
  42. }
  43. }
  44. public string Email
  45. {
  46.  
  47. get { return this.email; }
  48. set
  49. {
  50. if (ValidateEmail(this.email) == false)
  51. {
  52.  
  53. throw new Exception("Invalid email. Email should contain \"@\"");
  54. }
  55.  
  56. this.email = value;
  57. }
  58. }
  59.  
  60. private bool ValidateEmail(string email)
  61.  
  62. {
  63. bool isValidEmail = false;
  64.  
  65. for (int i = 0; i < email.Length; i++)
  66. {
  67. if (email[i] == '@')
  68. {
  69. isValidEmail = true;
  70. break;
  71. }
  72. }
  73. return isValidEmail;
  74.  
  75. }
  76.  
  77. public Persons (string name, int age)
  78. {
  79. this.Name = name;
  80. this.Age = age;
  81. }
  82. public Persons(string name, int age, string email)
  83. {
  84. this.Name = name;
  85. this.Age = age;
  86. this.Email = email;
  87. }
  88.  
  89. public override string ToString()
  90. {
  91. return "First person's name is " + this.name + "he/she is " + this.age + "years old and his/her email is " + this.email;
  92.  
  93. }
  94.  
  95.  
  96.  
  97. static void Main(string[] args)
  98. {
  99. Persons Ani = new Persons("Ani", 35);
  100. Persons Lili = new Persons("Lili", 3, "Lili@baby.tv");
  101. // Persons Rumi = new Persons ("Rumi", 102);
  102. // Persons Mitko = new Persons ("", 40, "noemail");
  103.  
  104. Console.WriteLine(Ani);
  105. Console.WriteLine(Lili);
  106. // Console.WriteLine(Rumi);
  107. // Console.WriteLine(Mitko);
  108.  
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement