Advertisement
felix_de_suza

1.Persons

Sep 17th, 2014
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Persons
  4. {
  5. class ShowPerson
  6. {
  7. public static void Main()
  8. {
  9. Person FirstPerson = new Person("Traqn", 10);
  10. Console.WriteLine(FirstPerson);
  11. }
  12. }
  13.  
  14. public class Person
  15. {
  16. private string name;
  17. private int age;
  18. private string email;
  19.  
  20. public Person(string name, int age, string email)
  21. {
  22. this.Name = name;
  23. this.Age = age;
  24. this.Email = email;
  25. }
  26.  
  27. public Person(string name, int age)
  28. : this(name, age, null)
  29. {
  30. }
  31.  
  32. public string Name
  33. {
  34. get { return this.name; }
  35. set
  36. {
  37. if (String.IsNullOrEmpty(value))
  38. {
  39. throw new ArgumentException("Name cannot be empty!");
  40. }
  41. this.name = value;
  42. }
  43. }
  44.  
  45. public int Age
  46. {
  47. get { return this.age; }
  48. set
  49. {
  50. if (value < 1 || value > 100)
  51. {
  52. throw new ArgumentException("Invalid age! It should be in the range [1..100]");
  53. }
  54. this.age = value;
  55. }
  56. }
  57.  
  58. public string Email
  59. {
  60. get { return this.email; }
  61. set
  62. {
  63. if (IsValidEmail(value) == false && value != null)
  64. {
  65. throw new ArgumentException("Invalid email address");
  66. }
  67. this.email = value;
  68. }
  69. }
  70.  
  71. public override string ToString()
  72. {
  73. return string.Format("Name: {0}, age: {1}, email: {2}", this.name, this.age, this.email ?? "[no email]");
  74. }
  75.  
  76. private bool IsValidEmail(string email)
  77. {
  78. bool isValid = false;
  79. foreach (char ch in email)
  80. {
  81. if (ch == '@')
  82. {
  83. isValid = true;
  84. break;
  85. }
  86. }
  87. return isValid;
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement