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