From a5de8eebec9095d1ed047e4168b225799d10a02e Mon Sep 17 00:00:00 2001
From: MarcUs7i <96580944+MarcUs7i@users.noreply.github.com>
Date: Wed, 5 Mar 2025 18:55:45 +0100
Subject: [PATCH] Completed the assignment
---
PetsAndFleas.Test/CatTests.cs | 2 --
PetsAndFleas.Test/DogTests.cs | 2 --
PetsAndFleas.Test/FleaTests.cs | 2 --
PetsAndFleas.Test/PetTests.cs | 2 --
PetsAndFleas/Cat.cs | 57 ++++++++++++++++++++++++++++++++++
PetsAndFleas/Dog.cs | 49 +++++++++++++++++++++++++++++
PetsAndFleas/Flea.cs | 44 ++++++++++++++++++++++++++
PetsAndFleas/Pet.cs | 57 ++++++++++++++++++++++++++++++++++
8 files changed, 207 insertions(+), 8 deletions(-)
create mode 100644 PetsAndFleas/Cat.cs
create mode 100644 PetsAndFleas/Dog.cs
create mode 100644 PetsAndFleas/Flea.cs
create mode 100644 PetsAndFleas/Pet.cs
diff --git a/PetsAndFleas.Test/CatTests.cs b/PetsAndFleas.Test/CatTests.cs
index 6ee6a05..63908b7 100644
--- a/PetsAndFleas.Test/CatTests.cs
+++ b/PetsAndFleas.Test/CatTests.cs
@@ -2,7 +2,6 @@
public sealed class CatTests
{
- /*
private readonly Cat _cat = new();
[Fact]
@@ -61,5 +60,4 @@ public sealed class CatTests
[Fact]
public void StringRepresentation() => new Cat().ToString().Should().Be("I'm a cat");
- */
}
diff --git a/PetsAndFleas.Test/DogTests.cs b/PetsAndFleas.Test/DogTests.cs
index 52a0f8d..1588dae 100644
--- a/PetsAndFleas.Test/DogTests.cs
+++ b/PetsAndFleas.Test/DogTests.cs
@@ -2,7 +2,6 @@
public sealed class DogTests
{
- /*
private readonly Dog _dog = new(new DateTimeProvider());
[Fact]
@@ -52,5 +51,4 @@ public sealed class DogTests
[Fact]
public void StringRepresentation() => _dog.ToString().Should().Be("I'm a dog");
- */
}
diff --git a/PetsAndFleas.Test/FleaTests.cs b/PetsAndFleas.Test/FleaTests.cs
index 4a6e182..b141d11 100644
--- a/PetsAndFleas.Test/FleaTests.cs
+++ b/PetsAndFleas.Test/FleaTests.cs
@@ -2,7 +2,6 @@
public sealed class FleaTests
{
- /*
private readonly Flea _flea = new();
[Fact]
@@ -118,5 +117,4 @@ public sealed class FleaTests
}
#endregion
- */
}
diff --git a/PetsAndFleas.Test/PetTests.cs b/PetsAndFleas.Test/PetTests.cs
index 55a3b6a..8244dcf 100644
--- a/PetsAndFleas.Test/PetTests.cs
+++ b/PetsAndFleas.Test/PetTests.cs
@@ -2,7 +2,6 @@
public sealed class PetTests
{
- /*
[Fact]
public void PetId()
{
@@ -86,5 +85,4 @@ public sealed class PetTests
}
#endregion
- */
}
diff --git a/PetsAndFleas/Cat.cs b/PetsAndFleas/Cat.cs
new file mode 100644
index 0000000..4f00b46
--- /dev/null
+++ b/PetsAndFleas/Cat.cs
@@ -0,0 +1,57 @@
+namespace PetsAndFleas;
+
+///
+/// Represents a cat
+///
+public sealed class Cat : Pet
+{
+ private bool _isOnTree;
+
+ ///
+ /// The number of trees the cat has climbed
+ ///
+ public int TreesClimbed { get; private set; }
+
+ public Cat()
+ {
+ PrintCtorInfo("cat");
+ }
+
+ ///
+ /// Tries to climb a tree
+ ///
+ /// True if climbing was successful, false if cat is already on a tree
+ public bool ClimbOnTree()
+ {
+ if (_isOnTree)
+ {
+ return false;
+ }
+
+ TreesClimbed++;
+ _isOnTree = true;
+ return true;
+ }
+
+ ///
+ /// Tries to climb a tree
+ ///
+ /// True if climbing was successful, false if cat is not on a tree
+
+ public bool ClimbDown()
+ {
+ if (!_isOnTree)
+ {
+ return false;
+ }
+
+ _isOnTree = false;
+ return true;
+ }
+
+ ///
+ /// Returns a string representation of the cat
+ ///
+ /// The string that represents the cat
+ public override string ToString() => "I'm a cat";
+}
diff --git a/PetsAndFleas/Dog.cs b/PetsAndFleas/Dog.cs
new file mode 100644
index 0000000..3331d97
--- /dev/null
+++ b/PetsAndFleas/Dog.cs
@@ -0,0 +1,49 @@
+namespace PetsAndFleas;
+
+///
+/// Represents a dog
+///
+public sealed class Dog : Pet
+{
+ private static readonly TimeSpan huntingWaitInterval = TimeSpan.FromMinutes(1);
+ private readonly DateTimeProvider _dateTimeProvider;
+ private DateTime? _lastHuntedTime;
+
+ ///
+ /// Gets the number of animals this dog has hunted
+ ///
+ public int HuntedAnimals { get; private set; }
+
+ ///
+ /// Initializes a new class
+ ///
+ /// The datetime provider for checking hunting times
+ public Dog(DateTimeProvider dateTimeProvider)
+ {
+ _dateTimeProvider = dateTimeProvider;
+ PrintCtorInfo("Dog");
+ }
+
+ ///
+ /// Tries to hunt an animal.
+ /// Dog needs to rest 1 min after a hunt.
+ ///
+ /// True if hunting was successful, false if dog needs to rest.
+ public bool HuntAnimal()
+ {
+ if (_lastHuntedTime.HasValue && _dateTimeProvider.Now - _lastHuntedTime.Value < huntingWaitInterval)
+ {
+ return false;
+ }
+
+ _lastHuntedTime = _dateTimeProvider.Now;
+ HuntedAnimals++;
+ return true;
+ }
+
+ ///
+ /// Returns a string representation of the dog
+ ///
+ /// The string that represents the dog
+ public override string ToString() => "I'm a dog";
+}
diff --git a/PetsAndFleas/Flea.cs b/PetsAndFleas/Flea.cs
new file mode 100644
index 0000000..e181f2d
--- /dev/null
+++ b/PetsAndFleas/Flea.cs
@@ -0,0 +1,44 @@
+namespace PetsAndFleas;
+
+///
+/// Represents a flea
+///
+public sealed class Flea
+{
+ ///
+ /// The current selected
+ ///
+ public Pet? CurrentPet { get; private set; }
+
+ ///
+ /// The total bites taken
+ ///
+ public int TotalBites { get; private set; }
+
+ ///
+ /// Sets the current
+ ///
+ /// The pet to select
+ public void JumpOnPet(Pet? pet)
+ {
+ CurrentPet = pet;
+ }
+
+ ///
+ /// Bites the current
+ ///
+ /// How many bites to take
+ /// The number of bites taken
+ public int BitePet(int bites)
+ {
+ var bitesTaken = CurrentPet?.GetBitten(bites) ?? 0;
+ TotalBites += bitesTaken;
+ return bitesTaken;
+ }
+
+ ///
+ /// Returns a string representation of the flea
+ ///
+ /// The string that represents the flea
+ public override string ToString() => "I'm a flea";
+}
diff --git a/PetsAndFleas/Pet.cs b/PetsAndFleas/Pet.cs
new file mode 100644
index 0000000..b161ff4
--- /dev/null
+++ b/PetsAndFleas/Pet.cs
@@ -0,0 +1,57 @@
+namespace PetsAndFleas;
+
+///
+/// Abstract class for pets
+///
+public abstract class Pet
+{
+ ///
+ /// Next available pet ID
+ ///
+ public static int NextPetId { get; private set; }
+
+ ///
+ /// The unique pet ID
+ ///
+ public int PetId { get; }
+
+ ///
+ /// The remaining bites for this pet
+ ///
+ public int RemainingBites { get; private set; } = 100;
+
+ ///
+ /// Initializes a new class
+ ///
+ protected Pet()
+ {
+ NextPetId++;
+ PetId = NextPetId;
+ }
+
+ ///
+ /// Reduces the remaining bites for this pet
+ ///
+ /// Number of bites to take
+ /// The actual number of bites taken
+ public int GetBitten(int bites)
+ {
+ if (bites <= 0)
+ {
+ return 0;
+ }
+
+ int actualBites = Math.Min(bites, RemainingBites);
+ RemainingBites -= actualBites;
+ return actualBites;
+ }
+
+ ///
+ /// Prints information about the constructor
+ ///
+ /// The type of pet being created
+ protected static void PrintCtorInfo(string petType)
+ {
+ Console.WriteLine($"Creating a new {petType}");
+ }
+}