-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorm.cs
73 lines (64 loc) · 2.38 KB
/
Worm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
namespace Animals;
/// <summary>
/// Represents a worm, a type of animal.
/// </summary>
internal class Worm : Animal
{
private int segmentCount;
/// <summary>
/// Gets or sets the number of segments of the worm.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the segment count is not positive.</exception>
public int SegmentCount
{
get => segmentCount;
internal set
{
ValidateSegmentCount(value);
segmentCount = value;
}
}
/// <summary>
/// Constructs a new instance of the Worm class.
/// </summary>
/// <param name="name">The name of the worm.</param>
/// <param name="weight">The weight of the worm.</param>
/// <param name="age">The age of the worm.</param>
/// <param name="habitat">The habitat of the worm.</param>
/// <param name="segmentCount">The number of segments of the worm. Defaults to 10.</param>
/// <exception cref="ArgumentException">Thrown when the name or habitat is empty or null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the weight or segment count is non-positive or the age is negative.</exception>
public Worm(string name, double weight, int age, string habitat, int segmentCount = 10) : base(name, weight, age, habitat)
{
ValidateSegmentCount(segmentCount);
SegmentCount = segmentCount;
}
/// <summary>
/// Produces the characteristic sound of the worm.
/// </summary>
public override void DoSound()
{
Console.WriteLine($"{Name} the wiggly worm whispers, \"Squirm-squirm!\"");
}
/// <summary>
/// Provides statistics about the worm.
/// </summary>
/// <returns>Statistics string.</returns>
public override string Stats()
{
return $"{Name}'s segment count={SegmentCount}";
}
// Validation method
/// <summary>
/// Validates the segment count of the worm.
/// </summary>
/// <param name="segmentCount">The segment count to validate.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the segment count is not positive.</exception>
private void ValidateSegmentCount(int segmentCount)
{
if (segmentCount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(segmentCount), "Segment count must be greater than 0.");
}
}
}