Vdzhambazova

Person

Jul 4th, 2016
577
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Text;
  3.  
  4. namespace ExercisesInheritance
  5. {
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. string name = Console.ReadLine();
  11. int age = int.Parse(Console.ReadLine());
  12.  
  13. try
  14. {
  15. Child child = new Child(name, age);
  16. Console.WriteLine(child);
  17. }
  18. catch (ArgumentException ae)
  19. {
  20. Console.WriteLine(ae.Message);
  21. }
  22. }
  23.  
  24. }
  25.  
  26. public class Child : Person
  27. {
  28. public Child(string name, int age) : base(name, age)
  29. {
  30.  
  31. }
  32.  
  33. public override int Age
  34. {
  35. get { return base.Age; }
  36. protected set
  37. {
  38. if (value >= 15)
  39. {
  40. throw new ArgumentException("Child’s age must be less than 15!");
  41. }
  42.  
  43. base.Age = value;
  44. }
  45. }
  46. }
  47.  
  48. public class Person
  49. {
  50. private string name;
  51. private int age;
  52.  
  53. public Person(string name, int age)
  54. {
  55. this.Age = age;
  56. this.Name = name;
  57. }
  58.  
  59. public virtual string Name
  60. {
  61. get { return this.name; }
  62. protected set
  63. {
  64. if (value.Length < 3 || string.IsNullOrWhiteSpace(value))
  65. {
  66. throw new ArgumentException("Name’s length should not be less than 3 symbols!");
  67. }
  68. this.name = value;
  69. }
  70. }
  71.  
  72. public virtual int Age
  73. {
  74. get { return this.age; }
  75. protected set
  76. {
  77. if (value < 0)
  78. {
  79. throw new ArgumentException("Age must be positive!");
  80. }
  81. this.age = value;
  82. }
  83. }
  84.  
  85. public override string ToString()
  86. {
  87. StringBuilder sb = new StringBuilder();
  88. sb.Append($"Name: {this.Name}, Age: {this.Age}");
  89.  
  90. return sb.ToString();
  91. }
  92. }
  93. }
RAW Paste Data