Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace Training.DomainClasses
  6. {
  7. public class PetShop
  8. {
  9. private IList<Pet> _petsInTheStore;
  10. public static IEnumerable<Pet> filter;
  11.  
  12. public PetShop(IList<Pet> petsInTheStore)
  13. {
  14. this._petsInTheStore = petsInTheStore;
  15. }
  16.  
  17. public IEnumerable<Pet> AllPets()
  18. {
  19. foreach (var pet in _petsInTheStore)
  20. {
  21. yield return pet;
  22. }
  23. }
  24.  
  25. public void Add(Pet newPet)
  26. {
  27. if (!_petsInTheStore.Contains(newPet))
  28. _petsInTheStore.Add(newPet);
  29. }
  30.  
  31. public IEnumerable<Pet> AllCats()
  32. {
  33. foreach (var pet in _petsInTheStore)
  34. {
  35. if (pet.species == Species.Cat) yield return pet;
  36. }
  37. }
  38.  
  39. public IEnumerable<Pet> AllPetsSortedByName()
  40. {
  41. List<Pet> petsSortedByName = new List<Pet>(_petsInTheStore);
  42. petsSortedByName.Sort((i1, i2) => i1.name.CompareTo(i2.name) );
  43. return petsSortedByName;
  44. }
  45.  
  46. public IEnumerable<Pet> AllMice()
  47. {
  48. return PetsThatSatisfy(p => p.species == Species.Mouse);
  49. }
  50.  
  51. public static IEnumerable<Pet> PetsThatSatisfy(Predicate<Pet> condition)
  52. {
  53. foreach (var pet in _petsInTheStore)
  54. {
  55. if (condition(pet))
  56. yield return pet;
  57. }
  58. }
  59.  
  60. public IEnumerable<Pet> AllFemalePets()
  61. {
  62. return PetsThatSatisfy(p => p.sex == Sex.Female);
  63.  
  64. }
  65.  
  66. public IEnumerable<Pet> AllCatsOrDogs()
  67. {
  68. return PetsThatSatisfy(pet => pet.species.Equals(Species.Dog) || pet.species.Equals(Species.Cat));
  69. }
  70.  
  71. public IEnumerable<Pet> AllPetsButNotMice()
  72. {
  73. return PetsThatSatisfy(pet => !pet.species.Equals(Species.Mouse));
  74. }
  75.  
  76. public IEnumerable<Pet> AllPetsBornAfter2010()
  77. {
  78. return PetsThatSatisfy(p => p.yearOfBirth > 2010);
  79. }
  80.  
  81. public IEnumerable<Pet> AllDogsBornAfter2010()
  82. {
  83. return PetsThatSatisfy(p => p.yearOfBirth > 2010 && p.species.Equals(Species.Dog));
  84. }
  85.  
  86. public IEnumerable<Pet> AllMaleDogs()
  87. {
  88. return PetsThatSatisfy(p => p.sex == Sex.Male && p.species.Equals(Species.Dog));
  89.  
  90. }
  91.  
  92. public IEnumerable<Pet> AllPetsBornAfter2011OrRabbits()
  93. {
  94. return PetsThatSatisfy(p => p.yearOfBirth > 2010 || p.species.Equals(Species.Rabbit));
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement