desislava_topuzakova

Chicken

May 4th, 2021
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Arrays
  6. {
  7. class Chicken
  8. {
  9. //полета: име, възраст, брой яйца
  10. private string name;
  11. private int age;
  12. private int countEggs;
  13.  
  14. //конструктор
  15. public Chicken (string name, int age, int countEggs)
  16. {
  17. this.Name = name;
  18. this.Age = age;
  19. this.CountEggs = countEggs;
  20. }
  21.  
  22.  
  23. //getters и setters
  24. public string Name
  25. {
  26. get { return this.name; }
  27. set {
  28. //валидация на името -> value -> 1. != null 2. != empty
  29. if (String.IsNullOrEmpty(value))
  30. {
  31. throw new Exception("Name cannot be empty.");
  32. }
  33. this.name = value;
  34. }
  35. }
  36.  
  37. public int Age
  38. {
  39. get { return this.age; }
  40. set {
  41. //валидация на възрастта -> между 0 и 15
  42. if (value < 0 || value > 15)
  43. {
  44. throw new Exception("Age should be between 0 and 15.");
  45. }
  46. this.age = value;
  47. }
  48. }
  49.  
  50. public int CountEggs
  51. {
  52. get { return this.countEggs; }
  53. set { this.countEggs = value; }
  54. }
  55.  
  56.  
  57. }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment