Completed the assignment
This commit is contained in:
parent
a0d916b84e
commit
a5de8eebec
8 changed files with 207 additions and 8 deletions
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
public sealed class CatTests
|
public sealed class CatTests
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
private readonly Cat _cat = new();
|
private readonly Cat _cat = new();
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -61,5 +60,4 @@ public sealed class CatTests
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void StringRepresentation() => new Cat().ToString().Should().Be("I'm a cat");
|
public void StringRepresentation() => new Cat().ToString().Should().Be("I'm a cat");
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
public sealed class DogTests
|
public sealed class DogTests
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
private readonly Dog _dog = new(new DateTimeProvider());
|
private readonly Dog _dog = new(new DateTimeProvider());
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -52,5 +51,4 @@ public sealed class DogTests
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void StringRepresentation() => _dog.ToString().Should().Be("I'm a dog");
|
public void StringRepresentation() => _dog.ToString().Should().Be("I'm a dog");
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
public sealed class FleaTests
|
public sealed class FleaTests
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
private readonly Flea _flea = new();
|
private readonly Flea _flea = new();
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -118,5 +117,4 @@ public sealed class FleaTests
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
public sealed class PetTests
|
public sealed class PetTests
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void PetId()
|
public void PetId()
|
||||||
{
|
{
|
||||||
|
|
@ -86,5 +85,4 @@ public sealed class PetTests
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
57
PetsAndFleas/Cat.cs
Normal file
57
PetsAndFleas/Cat.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
namespace PetsAndFleas;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a cat
|
||||||
|
/// </summary>
|
||||||
|
public sealed class Cat : Pet
|
||||||
|
{
|
||||||
|
private bool _isOnTree;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The number of trees the cat has climbed
|
||||||
|
/// </summary>
|
||||||
|
public int TreesClimbed { get; private set; }
|
||||||
|
|
||||||
|
public Cat()
|
||||||
|
{
|
||||||
|
PrintCtorInfo("cat");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to climb a tree
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>True if climbing was successful, false if cat is already on a tree</returns>
|
||||||
|
public bool ClimbOnTree()
|
||||||
|
{
|
||||||
|
if (_isOnTree)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
TreesClimbed++;
|
||||||
|
_isOnTree = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to climb a tree
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>True if climbing was successful, false if cat is not on a tree</returns>
|
||||||
|
|
||||||
|
public bool ClimbDown()
|
||||||
|
{
|
||||||
|
if (!_isOnTree)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_isOnTree = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a string representation of the cat
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The string that represents the cat</returns>
|
||||||
|
public override string ToString() => "I'm a cat";
|
||||||
|
}
|
||||||
49
PetsAndFleas/Dog.cs
Normal file
49
PetsAndFleas/Dog.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
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";
|
||||||
|
}
|
||||||
44
PetsAndFleas/Flea.cs
Normal file
44
PetsAndFleas/Flea.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
namespace PetsAndFleas;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a flea
|
||||||
|
/// </summary>
|
||||||
|
public sealed class Flea
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The current selected <see cref="Pet"/>
|
||||||
|
/// </summary>
|
||||||
|
public Pet? CurrentPet { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The total bites taken
|
||||||
|
/// </summary>
|
||||||
|
public int TotalBites { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the current <see cref="Pet"/>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="pet">The pet to select</param>
|
||||||
|
public void JumpOnPet(Pet? pet)
|
||||||
|
{
|
||||||
|
CurrentPet = pet;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Bites the current <see cref="Pet"/>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bites">How many bites to take</param>
|
||||||
|
/// <returns>The number of bites taken</returns>
|
||||||
|
public int BitePet(int bites)
|
||||||
|
{
|
||||||
|
var bitesTaken = CurrentPet?.GetBitten(bites) ?? 0;
|
||||||
|
TotalBites += bitesTaken;
|
||||||
|
return bitesTaken;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a string representation of the flea
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The string that represents the flea</returns>
|
||||||
|
public override string ToString() => "I'm a flea";
|
||||||
|
}
|
||||||
57
PetsAndFleas/Pet.cs
Normal file
57
PetsAndFleas/Pet.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
namespace PetsAndFleas;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Abstract class for pets
|
||||||
|
/// </summary>
|
||||||
|
public abstract class Pet
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Next available pet ID
|
||||||
|
/// </summary>
|
||||||
|
public static int NextPetId { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The unique pet ID
|
||||||
|
/// </summary>
|
||||||
|
public int PetId { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The remaining bites for this pet
|
||||||
|
/// </summary>
|
||||||
|
public int RemainingBites { get; private set; } = 100;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new <see cref="Pet"/> class
|
||||||
|
/// </summary>
|
||||||
|
protected Pet()
|
||||||
|
{
|
||||||
|
NextPetId++;
|
||||||
|
PetId = NextPetId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reduces the remaining bites for this pet
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bites">Number of bites to take</param>
|
||||||
|
/// <returns>The actual number of bites taken</returns>
|
||||||
|
public int GetBitten(int bites)
|
||||||
|
{
|
||||||
|
if (bites <= 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int actualBites = Math.Min(bites, RemainingBites);
|
||||||
|
RemainingBites -= actualBites;
|
||||||
|
return actualBites;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prints information about the constructor
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="petType">The type of pet being created</param>
|
||||||
|
protected static void PrintCtorInfo(string petType)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Creating a new {petType}");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue