commit a33cf50a4c3e628ed959bff685d6532e5fa7ba22 Author: KäseToatz Date: Wed Sep 25 22:24:42 2024 +0200 Part 1 & 2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cbec65c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.vs +bin +obj \ No newline at end of file diff --git a/Bucket.sln b/Bucket.sln new file mode 100644 index 0000000..1620d0c --- /dev/null +++ b/Bucket.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34701.34 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Buckets", "Buckets\Buckets.csproj", "{0B0CE8F2-0665-42A4-AD91-7F2F6F67A5D0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Buckets.Logic", "Buckets.Logic\Buckets.Logic.csproj", "{55E89D29-6A4E-4A7F-B1E3-08DA8C5AAC53}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Buckets.Test", "Buckets.Test\Buckets.Test.csproj", "{176D4C26-03A8-4C33-802D-D80294C0E909}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0B0CE8F2-0665-42A4-AD91-7F2F6F67A5D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0B0CE8F2-0665-42A4-AD91-7F2F6F67A5D0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0B0CE8F2-0665-42A4-AD91-7F2F6F67A5D0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0B0CE8F2-0665-42A4-AD91-7F2F6F67A5D0}.Release|Any CPU.Build.0 = Release|Any CPU + {55E89D29-6A4E-4A7F-B1E3-08DA8C5AAC53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {55E89D29-6A4E-4A7F-B1E3-08DA8C5AAC53}.Debug|Any CPU.Build.0 = Debug|Any CPU + {55E89D29-6A4E-4A7F-B1E3-08DA8C5AAC53}.Release|Any CPU.ActiveCfg = Release|Any CPU + {55E89D29-6A4E-4A7F-B1E3-08DA8C5AAC53}.Release|Any CPU.Build.0 = Release|Any CPU + {176D4C26-03A8-4C33-802D-D80294C0E909}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {176D4C26-03A8-4C33-802D-D80294C0E909}.Debug|Any CPU.Build.0 = Debug|Any CPU + {176D4C26-03A8-4C33-802D-D80294C0E909}.Release|Any CPU.ActiveCfg = Release|Any CPU + {176D4C26-03A8-4C33-802D-D80294C0E909}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {76573F70-1533-4DC6-8AFC-8C46B5EFCC12} + EndGlobalSection +EndGlobal diff --git a/Buckets.Logic/Bucket.cs b/Buckets.Logic/Bucket.cs new file mode 100644 index 0000000..5a05913 --- /dev/null +++ b/Buckets.Logic/Bucket.cs @@ -0,0 +1,23 @@ +namespace Buckets.Logic +{ + public class Bucket : Container + { + private const int DEFAULT_CAPACITY = 12; + private const int MAX_CAPACITY = 2500; + private const int MIN_CAPACITY = 10; + + public Bucket(int content) : base(DEFAULT_CAPACITY, content) {} + + public Bucket(int capacity, int content) : base(capacity, content) + { + ArgumentOutOfRangeException.ThrowIfGreaterThan(capacity, MAX_CAPACITY, nameof(capacity)); + ArgumentOutOfRangeException.ThrowIfLessThan(capacity, MIN_CAPACITY, nameof(capacity)); + } + + public void Fill(Bucket bucket) + { + Content += bucket.Content; + bucket.Empty(); + } + } +} \ No newline at end of file diff --git a/Buckets.Logic/Buckets.Logic.csproj b/Buckets.Logic/Buckets.Logic.csproj new file mode 100644 index 0000000..fa71b7a --- /dev/null +++ b/Buckets.Logic/Buckets.Logic.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/Buckets.Logic/Container.cs b/Buckets.Logic/Container.cs new file mode 100644 index 0000000..8d300b9 --- /dev/null +++ b/Buckets.Logic/Container.cs @@ -0,0 +1,32 @@ +namespace Buckets.Logic +{ + public abstract class Container + { + private int content; + + public int Capacity { get; } + public int Content {get => content; set { ArgumentOutOfRangeException.ThrowIfNegative(value, nameof(Content)); ArgumentOutOfRangeException.ThrowIfGreaterThan(value, Capacity, nameof(Content)); content = value; } } + + public Container(int capacity, int content) + { + ArgumentOutOfRangeException.ThrowIfNegative(capacity, nameof(Capacity)); + Capacity = capacity; + Content = content; + } + + public void Fill(int amount) + { + Content += amount; + } + + public void Empty(int amount) + { + Content -= amount; + } + + public void Empty() + { + Content = 0; + } + } +} \ No newline at end of file diff --git a/Buckets.Logic/OilBarrel.cs b/Buckets.Logic/OilBarrel.cs new file mode 100644 index 0000000..48ae69d --- /dev/null +++ b/Buckets.Logic/OilBarrel.cs @@ -0,0 +1,7 @@ +namespace Buckets.Logic +{ + public class OilBarrel(int content) : Container(CAPACITY, content) + { + public const int CAPACITY = 159; + } +} \ No newline at end of file diff --git a/Buckets.Logic/Rainbarrel.cs b/Buckets.Logic/Rainbarrel.cs new file mode 100644 index 0000000..0bf809c --- /dev/null +++ b/Buckets.Logic/Rainbarrel.cs @@ -0,0 +1,15 @@ +namespace Buckets.Logic +{ + public class Rainbarrel : Container + { + private readonly int[] ALLOWED_CAPACITIES = [80, 100, 120]; + + public Rainbarrel(int capacity, int content) : base(capacity, content) + { + if (!ALLOWED_CAPACITIES.Contains(capacity)) + { + throw new ArgumentException($"Capacity must be one of the following values: {string.Join(", ", ALLOWED_CAPACITIES)}."); + } + } + } +} \ No newline at end of file diff --git a/Buckets.Test/BucketTest.cs b/Buckets.Test/BucketTest.cs new file mode 100644 index 0000000..6be0d52 --- /dev/null +++ b/Buckets.Test/BucketTest.cs @@ -0,0 +1,62 @@ +using Buckets.Logic; + +namespace Buckets.Test +{ + [TestClass] + public class BucketTest + { + [TestMethod] + public void Bucket_DefaultCapacity_ShouldEqual12() + { + Bucket bucket = new(0); + Assert.AreEqual(12, bucket.Capacity); + } + + [TestMethod] + public void Bucket_LowCapacity_ShouldThrow() + { + Assert.ThrowsException(() => + { + Bucket bucket = new(5, 0); + }); + } + + [TestMethod] + public void Bucket_HighCapacity_ShouldThrow() + { + Assert.ThrowsException(() => + { + Bucket bucket = new(2501, 0); + }); + } + + [TestMethod] + public void Fill_BucketOverCapacity_ShouldThrow() + { + Bucket bucket = new(10, 0); + Bucket bucket2 = new(20, 20); + Assert.ThrowsException(() => + { + bucket.Fill(bucket2); + }); + } + + [TestMethod] + public void Fill_SecondBucket_ShouldEqual0() + { + Bucket bucket = new(20, 0); + Bucket bucket2 = new(20, 10); + bucket.Fill(bucket2); + Assert.AreEqual(0, bucket2.Content); + } + + [TestMethod] + public void Fill_Bucket_ShouldEqual20() + { + Bucket bucket = new(20, 10); + Bucket bucket2 = new(20, 10); + bucket.Fill(bucket2); + Assert.AreEqual(20, bucket.Content); + } + } +} \ No newline at end of file diff --git a/Buckets.Test/Buckets.Test.csproj b/Buckets.Test/Buckets.Test.csproj new file mode 100644 index 0000000..e1ed038 --- /dev/null +++ b/Buckets.Test/Buckets.Test.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + diff --git a/Buckets.Test/ContainerTest.cs b/Buckets.Test/ContainerTest.cs new file mode 100644 index 0000000..a4651ed --- /dev/null +++ b/Buckets.Test/ContainerTest.cs @@ -0,0 +1,86 @@ +using Buckets.Logic; + +namespace Buckets.Test +{ + [TestClass] + public class ContainerTest + { + [TestMethod] + public void Container_NegativeCapacity_ShouldThrow() + { + Assert.ThrowsException(() => + { + Bucket container = new(-1, 0); + }); + } + + [TestMethod] + public void Container_NegativeContent_ShouldThrow() + { + Assert.ThrowsException(() => + { + Bucket container = new(0, -1); + }); + } + + [TestMethod] + public void Container_ContentLargerThanCapacity_ShouldThrow() + { + Assert.ThrowsException(() => + { + Bucket container = new(0, 1); + }); + } + + [TestMethod] + public void Container_ValidParameters_ShouldEqual10() + { + Bucket container = new(20, 10); + Assert.AreEqual(10, container.Content); + } + + [TestMethod] + public void Fill_ContentLargerThanCapacity_ShouldThrow() + { + Bucket container = new(20, 10); + Assert.ThrowsException(() => + { + container.Fill(20); + }); + } + + [TestMethod] + public void Fill_ValidParameters_ShouldEqual15() + { + Bucket container = new(20, 10); + container.Fill(5); + Assert.AreEqual(15, container.Content); + } + + [TestMethod] + public void Empty_NegativeContent_ShouldThrow() + { + Bucket container = new(20, 10); + Assert.ThrowsException(() => + { + container.Empty(20); + }); + } + + [TestMethod] + public void Empty_ValidParameters_ShouldEqual5() + { + Bucket container = new(20, 10); + container.Empty(5); + Assert.AreEqual(5, container.Content); + } + + [TestMethod] + public void Empty_ValidParameters_ShouldEqual0() + { + Bucket container = new(20, 10); + container.Empty(); + Assert.AreEqual(0, container.Content); + } + } +} \ No newline at end of file diff --git a/Buckets.Test/OilBarrelTest.cs b/Buckets.Test/OilBarrelTest.cs new file mode 100644 index 0000000..215453f --- /dev/null +++ b/Buckets.Test/OilBarrelTest.cs @@ -0,0 +1,15 @@ +using Buckets.Logic; + +namespace Buckets.Test +{ + [TestClass] + public class OilBarrelTest + { + [TestMethod] + public void OilBarrel_ValidParams_ShouldEqualDefault() + { + OilBarrel oilbarrel = new(10); + Assert.AreEqual(OilBarrel.CAPACITY, oilbarrel.Capacity); + } + } +} \ No newline at end of file diff --git a/Buckets.Test/RainBarrelTest.cs b/Buckets.Test/RainBarrelTest.cs new file mode 100644 index 0000000..40baa18 --- /dev/null +++ b/Buckets.Test/RainBarrelTest.cs @@ -0,0 +1,24 @@ +using Buckets.Logic; + +namespace Buckets.Test +{ + [TestClass] + public class RainBarrelTest + { + [TestMethod] + public void RainBarrel_DisallowedCapacity_ShouldThrow() + { + Assert.ThrowsException(() => + { + Rainbarrel rainbarrel = new(90, 0); + }); + } + + [TestMethod] + public void RainBarrel_AllowedCapacity_ShouldEqual100() + { + Rainbarrel rainbarrel = new(100, 0); + Assert.AreEqual(100, rainbarrel.Capacity); + } + } +} \ No newline at end of file diff --git a/Buckets/Buckets.csproj b/Buckets/Buckets.csproj new file mode 100644 index 0000000..0c2aec9 --- /dev/null +++ b/Buckets/Buckets.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/Buckets/Program.cs b/Buckets/Program.cs new file mode 100644 index 0000000..4f46e2e --- /dev/null +++ b/Buckets/Program.cs @@ -0,0 +1,28 @@ +using Buckets.Logic; + +namespace Buckets +{ + internal class Program + { + static void Main() + { + Bucket bucket = new(50, 0); + bucket.Fill(10); + bucket.Empty(); + bucket.Fill(20); + bucket.Empty(10); + + OilBarrel oilbarrel = new(0); + oilbarrel.Fill(10); + oilbarrel.Empty(); + oilbarrel.Fill(20); + oilbarrel.Empty(10); + + Rainbarrel rainbarrel = new(100, 0); + rainbarrel.Fill(10); + rainbarrel.Empty(); + rainbarrel.Fill(20); + rainbarrel.Empty(10); + } + } +} \ No newline at end of file