ex-col-02-bucket-chain/BucketChain/Well.cs

58 lines
No EOL
1.9 KiB
C#

namespace BucketChain;
/// <summary>
/// Represents a well which provides water for fighting the fire.
/// </summary>
public sealed class Well
{
private readonly double _maxCapacity;
private readonly double _refillRate;
/// <summary>
/// Creates a new well with the supplied properties.
/// Initially the well is filled to the brim.
/// </summary>
/// <param name="maxCapacity">The max. fill level of the well in liters.</param>
/// <param name="refillRate">The amount of liters by which this well is refilled each (chain)
/// operation step</param>
public Well(double maxCapacity, double refillRate)
{
_maxCapacity = maxCapacity;
_refillRate = refillRate;
}
/// <summary>
/// Gets how many liters currently remain in the well.
/// Cannot exceed the max. capacity of the well or drop below 0.
/// </summary>
public double LitersRemaining { get; private set; }
/// <summary>
/// Refills the well by the constant rate.
/// </summary>
public void Refill()
{
LitersRemaining = LitersRemaining + _refillRate > _maxCapacity
? _maxCapacity
: LitersRemaining + _refillRate;
}
/// <summary>
/// Fills the passed bucket.
/// Can provide at most the <see cref="LitersRemaining"/> and only subtracts the
/// amount actually required to fill the bucket.
/// </summary>
/// <param name="bucket">The bucket to fill</param>
public void FillBucket(Bucket bucket)
{
var litersTaken = bucket.Fill(LitersRemaining);
LitersRemaining -= litersTaken;
}
/// <summary>
/// Creates a string representation of this well.
/// Shows the current and max. amount of liters - rounded to whole numbers.
/// </summary>
/// <returns>String representation of the well</returns>
public override string ToString() => $"💧 {LitersRemaining}/{_maxCapacity}";
}