Advertisement
desislava_topuzakova

Hotel.cs

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