/// <summary> /// Add new animals on field if user press corresponding buttons. /// </summary> private void HandlePlayerCommands() { while (_console.KeyAvailable()) { AnimalType type = (AnimalType)char.Parse(_console.ConsoleKey().ToString()); _animalFactory.Create(_field, type); } }
public async Task Adopt_WhenAnimalNotFound_ShouldReturnBadRequest() { var user = UserBuilder.NewInstance() .WithId(1) .WithUsername("fakeuser") .Build(); var controller = GetUserAnimalsController(); A.CallTo(() => _userRepository.GetByIdAsync(A <int> ._)).Returns(Task.FromResult <User>(user)); A.CallTo(() => _animalFactory.Create(A <int> ._)).Returns(null); var result = await controller.Adopt(1, new UserCommands.Adopt { Code = 2 }); Assert.NotNull(result); Assert.IsType <BadRequestObjectResult>(result); }
public string RegisterAnimal(string type, string name, int energy, int happiness, int procedureTime) { IAnimal animal = animalFactory.Create(type, name, energy, happiness, procedureTime); if (this.hotel.Animals.ContainsKey(name)) { throw new ArgumentException($"Animal {name} already exist"); } this.hotel.Accommodate(animal); return($"Animal {animal.Name} registered successfully"); }
/// <summary> /// Check every animal if its mating count is equal to 3, create new animal on a field. /// </summary> public void GiveBirthToAnimal(Field field, IAnimalFactory animalFactory) { foreach (var animal in field.Animals) { if (animal.MatingCount == 3) { animal.MatingCount = 0; if (animal.ClosestPartner.ClosestPartner == animal) { animal.ClosestPartner.MatingCount = 0; } animalFactory.Create(field, animal.AnimalType); } } }
public async Task <IActionResult> Adopt(int userId, [FromBody] UserCommands.Adopt adopt) { var user = await _userRepository.GetByIdAsync(userId); if (user == null) { return(BadRequest($"User with id:{userId} doesn't exist.")); } var animal = _animalFactory.Create(adopt.Code); if (animal == null) { return(BadRequest($"{adopt.Code} doesn't correspond to any animal.")); } await _userRepository.Adopt(user, animal); _hub.Publish(new UserEvents.AnimalAdopted { Animal = animal }); return(CreatedAtRoute(RouteNames.GetUserAnimal, new { userId = user.Id, animalId = animal.Id }, animal)); }