Advertisement
desislava_topuzakova

Untitled

Jul 7th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace AlgorithmsRetakeExam
  5. {
  6. public class Hotel
  7. {
  8. private string name;
  9. private List<Dog> dogs;
  10.  
  11. public Hotel()
  12. {
  13.  
  14. }
  15.  
  16. public Hotel(string name)
  17. {
  18. Name = name;
  19. Dogs = new List<Dog>();
  20. }
  21.  
  22. public string Name
  23. {
  24. get
  25. {
  26. return name;
  27. }
  28. set
  29. {
  30. name = value;
  31. }
  32. }
  33.  
  34. public List<Dog> Dogs
  35. {
  36. get
  37. {
  38. return dogs;
  39. }
  40. set
  41. {
  42. dogs = value;
  43. }
  44. }
  45.  
  46. public void AddDog(string name, int age)
  47. {
  48. Dog dog = new Dog(name, age);
  49. Dogs.Add(dog);
  50. }
  51.  
  52. public double AverageAge()
  53. {
  54. double sum = 0;
  55. int count = 0;
  56. foreach (Dog dog in Dogs)
  57. {
  58. sum += dog.Age;
  59. count++;
  60. }
  61.  
  62. return sum / count;
  63. }
  64.  
  65. public List<string> RemoveDogsByAge(int age)
  66. {
  67. List<string> dogs = new List<string>();
  68. foreach (Dog dog in Dogs)
  69. {
  70. if (dog.Age < age)
  71. {
  72. dogs.Add(dog.Name);
  73. }
  74. }
  75. return dogs;
  76. }
  77.  
  78. public List<Dog> SortAscendingByName()
  79. {
  80. List<Dog> sorted = Dogs.OrderBy(dog => dog.Name).ToList();
  81. Dogs = sorted;
  82. return sorted;
  83. }
  84.  
  85. public List<Dog> SortDescendingByAge()
  86. {
  87. List<Dog> sorted = Dogs.OrderByDescending(dog => dog.Age).ToList();
  88. Dogs = sorted;
  89. return sorted;
  90. }
  91.  
  92. public bool CheckDogIsInHotel(string name)
  93. {
  94. foreach (Dog dog in Dogs)
  95. {
  96. if (dog.Name == name)
  97. {
  98. return true;
  99. }
  100. }
  101.  
  102. return false;
  103. }
  104.  
  105. public string[] ProvideInformationAboutAllDogs()
  106. {
  107. List<string> infoList = new List<string>();
  108. foreach (Dog dog in Dogs)
  109. {
  110. infoList.Add(dog.ToString());
  111. }
  112. return infoList.ToArray();
  113. }
  114. }
  115. }
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement