Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace Training.DomainClasses
- {
- public class PetShop
- {
- private IList<Pet> _petsInTheStore;
- public static IEnumerable<Pet> filter;
- public PetShop(IList<Pet> petsInTheStore)
- {
- this._petsInTheStore = petsInTheStore;
- }
- public IEnumerable<Pet> AllPets()
- {
- foreach (var pet in _petsInTheStore)
- {
- yield return pet;
- }
- }
- public void Add(Pet newPet)
- {
- if (!_petsInTheStore.Contains(newPet))
- _petsInTheStore.Add(newPet);
- }
- public IEnumerable<Pet> AllCats()
- {
- foreach (var pet in _petsInTheStore)
- {
- if (pet.species == Species.Cat) yield return pet;
- }
- }
- public IEnumerable<Pet> AllPetsSortedByName()
- {
- List<Pet> petsSortedByName = new List<Pet>(_petsInTheStore);
- petsSortedByName.Sort((i1, i2) => i1.name.CompareTo(i2.name) );
- return petsSortedByName;
- }
- public IEnumerable<Pet> AllMice()
- {
- return PetsThatSatisfy(p => p.species == Species.Mouse);
- }
- public static IEnumerable<Pet> PetsThatSatisfy(Predicate<Pet> condition)
- {
- foreach (var pet in _petsInTheStore)
- {
- if (condition(pet))
- yield return pet;
- }
- }
- public IEnumerable<Pet> AllFemalePets()
- {
- return PetsThatSatisfy(p => p.sex == Sex.Female);
- }
- public IEnumerable<Pet> AllCatsOrDogs()
- {
- return PetsThatSatisfy(pet => pet.species.Equals(Species.Dog) || pet.species.Equals(Species.Cat));
- }
- public IEnumerable<Pet> AllPetsButNotMice()
- {
- return PetsThatSatisfy(pet => !pet.species.Equals(Species.Mouse));
- }
- public IEnumerable<Pet> AllPetsBornAfter2010()
- {
- return PetsThatSatisfy(p => p.yearOfBirth > 2010);
- }
- public IEnumerable<Pet> AllDogsBornAfter2010()
- {
- return PetsThatSatisfy(p => p.yearOfBirth > 2010 && p.species.Equals(Species.Dog));
- }
- public IEnumerable<Pet> AllMaleDogs()
- {
- return PetsThatSatisfy(p => p.sex == Sex.Male && p.species.Equals(Species.Dog));
- }
- public IEnumerable<Pet> AllPetsBornAfter2011OrRabbits()
- {
- return PetsThatSatisfy(p => p.yearOfBirth > 2010 || p.species.Equals(Species.Rabbit));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement