ex-inh-02-pets/PetsAndFleas/Dog.cs
2025-03-05 18:55:45 +01:00

49 lines
1.4 KiB
C#

namespace PetsAndFleas;
/// <summary>
/// Represents a dog
/// </summary>
public sealed class Dog : Pet
{
private static readonly TimeSpan huntingWaitInterval = TimeSpan.FromMinutes(1);
private readonly DateTimeProvider _dateTimeProvider;
private DateTime? _lastHuntedTime;
/// <summary>
/// Gets the number of animals this dog has hunted
/// </summary>
public int HuntedAnimals { get; private set; }
/// <summary>
/// Initializes a new <see cref="Dog"/> class
/// </summary>
/// <param name="dateTimeProvider">The datetime provider for checking hunting times</param>
public Dog(DateTimeProvider dateTimeProvider)
{
_dateTimeProvider = dateTimeProvider;
PrintCtorInfo("Dog");
}
/// <summary>
/// Tries to hunt an animal.
/// Dog needs to rest 1 min after a hunt.
/// </summary>
/// <returns>True if hunting was successful, false if dog needs to rest.</returns>
public bool HuntAnimal()
{
if (_lastHuntedTime.HasValue && _dateTimeProvider.Now - _lastHuntedTime.Value < huntingWaitInterval)
{
return false;
}
_lastHuntedTime = _dateTimeProvider.Now;
HuntedAnimals++;
return true;
}
/// <summary>
/// Returns a string representation of the dog
/// </summary>
/// <returns>The string that represents the dog</returns>
public override string ToString() => "I'm a dog";
}