diff --git a/BucketChain/Bucket.cs b/BucketChain/Bucket.cs index 5fdd186..c7da044 100644 --- a/BucketChain/Bucket.cs +++ b/BucketChain/Bucket.cs @@ -5,7 +5,8 @@ /// public sealed class Bucket { - // TODO + private readonly double _capacityLiters; + private double _contentLiters; /// /// Creates a new bucket with the given capacity. @@ -13,13 +14,14 @@ public sealed class Bucket /// Bucket capacity in liters public Bucket(double capacityLiters) { - // TODO + _capacityLiters = capacityLiters; + _contentLiters = 0D; } /// /// Gets if this bucket is currently completely empty or not. /// - public bool IsEmpty => false; // TODO + public bool IsEmpty => _contentLiters <= 0D; /// /// Fills the bucket to the brim if enough water is available. @@ -30,8 +32,17 @@ public sealed class Bucket /// The amount taken for filling the bucket public double Fill(double maxAvailable) { - // TODO - return -1D; + if (_capacityLiters - _contentLiters <= 0D) + { + return 0D; + } + + maxAvailable = Math.Abs(_capacityLiters - maxAvailable); + _contentLiters = _contentLiters + maxAvailable > _capacityLiters + ? _capacityLiters + : _contentLiters + maxAvailable; + + return maxAvailable; } /// @@ -40,7 +51,8 @@ public sealed class Bucket /// Amount emptied from the bucket, in liters public double Empty() { - // TODO - return -1D; + var content = _contentLiters; + _contentLiters = 0D; + return content; } } \ No newline at end of file diff --git a/BucketChain/Chain.cs b/BucketChain/Chain.cs index 68d81a1..167db76 100644 --- a/BucketChain/Chain.cs +++ b/BucketChain/Chain.cs @@ -5,7 +5,12 @@ /// public sealed class Chain { - // TODO + private readonly Fire _fire; + private readonly int _requiredPeople; + private readonly Well _well; + private readonly double _bucketSize; + private Person _firstPerson; + private int _availableBuckets; /// /// Creates a new bucket chain based on the supplied parameters. @@ -19,7 +24,12 @@ public sealed class Chain public Chain(int requiredPeople, int availableBuckets, double bucketSize, Well well, Fire fire, Person firstPerson) { - // TODO + _requiredPeople = requiredPeople; + _availableBuckets = availableBuckets; + _bucketSize = bucketSize; + _well = well; + _fire = fire; + _firstPerson = firstPerson; } /// diff --git a/BucketChain/Fire.cs b/BucketChain/Fire.cs index b78ee45..c6635a3 100644 --- a/BucketChain/Fire.cs +++ b/BucketChain/Fire.cs @@ -5,7 +5,7 @@ /// public sealed class Fire { - // TODO + private readonly double _growRate; /// /// Creates a new fire with the passed properties. @@ -14,19 +14,19 @@ public sealed class Fire /// Rate by which the fire grows with each (chain operation) step public Fire(double initialFireSize, double growRate) { - // TODO + _growRate = growRate; + FireSize = initialFireSize; } /// /// Gets the current size of the fire; cannot be smaller than 0. /// - // TODO - // public double FireSize ... + public double FireSize { get; private set; } /// /// Gets if the fire has been extinguished (= size is 0). /// - public bool Extinguished => false; // TODO + public bool Extinguished => FireSize <= 0D; /// /// The fire is hit by a load of water from a bucket. @@ -35,7 +35,7 @@ public sealed class Fire /// Liters of water public void GetHitByWater(double amount) { - // TODO + FireSize = FireSize - amount < 0D ? 0D : FireSize - amount; } /// @@ -44,7 +44,7 @@ public sealed class Fire /// public void BurnHigher() { - // TODO + FireSize += _growRate; } /// @@ -52,5 +52,5 @@ public sealed class Fire /// The size is rounded to whole numbers. /// /// String representation of the fire - public override string ToString() => string.Empty; // TODO + public override string ToString() => $"🔥 {Math.Round(FireSize)}"; } \ No newline at end of file diff --git a/BucketChain/Person.cs b/BucketChain/Person.cs index fdd2884..a6a96c3 100644 --- a/BucketChain/Person.cs +++ b/BucketChain/Person.cs @@ -5,7 +5,8 @@ /// public sealed class Person { - // TODO + private Bucket? _backwardBucket; + private Bucket? _forwardBucket; /// /// Creates a new person. @@ -25,21 +26,18 @@ public sealed class Person /// /// Gets the left neighbor of the person. Can be null. /// - // TODO - // public Person? LeftNeighbor ... + public Person? LeftNeighbor { get; private set; } /// /// Gets the right neighbor of the person. Can be null. /// - // TODO - // public Person? RightNeighbor ... + public Person? RightNeighbor { get; private set; } /// /// Gets the number of the last step in which this person performed a bucket move action. /// Each person can only move, at most, once per step. /// - // TODO - // public int LastStepPerformed ... + public int LastStepPerformed { get; private set; } /// /// The person joins an existing bucket chain at a specific position. diff --git a/BucketChain/Well.cs b/BucketChain/Well.cs index d3db0cf..bb9650b 100644 --- a/BucketChain/Well.cs +++ b/BucketChain/Well.cs @@ -5,7 +5,8 @@ /// public sealed class Well { - // TODO + private readonly double _maxCapacity; + private readonly double _refillRate; /// /// Creates a new well with the supplied properties. @@ -16,22 +17,24 @@ public sealed class Well /// operation step public Well(double maxCapacity, double refillRate) { - // TODO + _maxCapacity = maxCapacity; + _refillRate = refillRate; } /// /// Gets how many liters currently remain in the well. /// Cannot exceed the max. capacity of the well or drop below 0. /// - // TODO - // public double LitersRemaining ... + public double LitersRemaining { get; private set; } /// /// Refills the well by the constant rate. /// public void Refill() { - // TODO + LitersRemaining = LitersRemaining + _refillRate > _maxCapacity + ? _maxCapacity + : LitersRemaining + _refillRate; } /// @@ -42,7 +45,8 @@ public sealed class Well /// The bucket to fill public void FillBucket(Bucket bucket) { - // TODO + var litersTaken = bucket.Fill(LitersRemaining); + LitersRemaining -= litersTaken; } /// @@ -50,5 +54,5 @@ public sealed class Well /// Shows the current and max. amount of liters - rounded to whole numbers. /// /// String representation of the well - public override string ToString() => string.Empty; // TODO + public override string ToString() => $"💧 {LitersRemaining}/{_maxCapacity}"; } \ No newline at end of file