ex-col-02-bucket-chain/BucketChain/Bucket.cs
MarcUs7i b7367aeb20 Removed bugs from Bucket.cs and Well.cs
Added working FightFire method from Person.cs
2024-12-13 22:49:41 +01:00

59 lines
No EOL
1.8 KiB
C#

namespace BucketChain;
/// <summary>
/// Represents a bucket used to transport water.
/// </summary>
public sealed class Bucket
{
private readonly double _capacityLiters;
private double _contentLiters;
/// <summary>
/// Creates a new bucket with the given capacity.
/// </summary>
/// <param name="capacityLiters">Bucket capacity in liters</param>
public Bucket(double capacityLiters)
{
_capacityLiters = capacityLiters;
_contentLiters = 0D;
}
/// <summary>
/// Gets if this bucket is currently completely empty or not.
/// </summary>
public bool IsEmpty => _contentLiters <= 0D;
/// <summary>
/// Fills the bucket to the brim if enough water is available.
/// If there is not enough water to fill the bucket completely it takes as much as possible.
/// If the bucket is already full no water is taken.
/// </summary>
/// <param name="maxAvailable">Max. available water, in liters</param>
/// <returns>The amount taken for filling the bucket</returns>
public double Fill(double maxAvailable)
{
if (_capacityLiters - _contentLiters <= 0D || maxAvailable <= 0D)
{
return 0D;
}
var amountTaken = _contentLiters;
_contentLiters = _contentLiters + maxAvailable > _capacityLiters
? _capacityLiters
: _contentLiters + maxAvailable;
amountTaken -= _contentLiters;
return Math.Abs(amountTaken);
}
/// <summary>
/// Empties the bucket. The amount stored within before emptying is returned.
/// </summary>
/// <returns>Amount emptied from the bucket, in liters</returns>
public double Empty()
{
var content = _contentLiters;
_contentLiters = 0D;
return content;
}
}