From 6176f4da3396b8008f12e4ec00e396ad6bdab898 Mon Sep 17 00:00:00 2001 From: BoolPurist <fnxa@posteo.de> Date: Sat, 26 Feb 2022 19:53:36 +0100 Subject: [PATCH] Added solution for possible prog task It is about structures and functions --- .gitignore | 2 + Code/Money_Bridge/Bridge_Money.sln | 22 ++ .../Bridge_Money.sln.DotSettings.user | 4 + .../Bridge_Money_Console.csproj | 8 + .../Bridge_Money_Console/Money.cs | 13 ++ .../Bridge_Money_Console/MoneyUtility.cs | 88 ++++++++ .../Bridge_Money_Console/Program.cs | 13 ++ .../Bridge_Money_Tests.csproj | 20 ++ .../TestCases_MoneyUtility.cs | 213 ++++++++++++++++++ .../Bridge_Money_Tests/TestMoneyUtility.cs | 89 ++++++++ Code/Money_Bridge/global.json | 7 + 11 files changed, 479 insertions(+) create mode 100644 .gitignore create mode 100644 Code/Money_Bridge/Bridge_Money.sln create mode 100644 Code/Money_Bridge/Bridge_Money.sln.DotSettings.user create mode 100644 Code/Money_Bridge/Bridge_Money_Console/Bridge_Money_Console.csproj create mode 100644 Code/Money_Bridge/Bridge_Money_Console/Money.cs create mode 100644 Code/Money_Bridge/Bridge_Money_Console/MoneyUtility.cs create mode 100644 Code/Money_Bridge/Bridge_Money_Console/Program.cs create mode 100644 Code/Money_Bridge/Bridge_Money_Tests/Bridge_Money_Tests.csproj create mode 100644 Code/Money_Bridge/Bridge_Money_Tests/TestCases_MoneyUtility.cs create mode 100644 Code/Money_Bridge/Bridge_Money_Tests/TestMoneyUtility.cs create mode 100644 Code/Money_Bridge/global.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d86ba9f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +obj/ +bin/ \ No newline at end of file diff --git a/Code/Money_Bridge/Bridge_Money.sln b/Code/Money_Bridge/Bridge_Money.sln new file mode 100644 index 0000000..cb418a5 --- /dev/null +++ b/Code/Money_Bridge/Bridge_Money.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bridge_Money_Console", "Bridge_Money_Console\Bridge_Money_Console.csproj", "{C545E045-C885-4BDC-BF60-7078DF93BD98}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bridge_Money_Tests", "Bridge_Money_Tests\Bridge_Money_Tests.csproj", "{5BC73D57-39BC-46F0-9B07-C7AFC65635FD}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C545E045-C885-4BDC-BF60-7078DF93BD98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C545E045-C885-4BDC-BF60-7078DF93BD98}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C545E045-C885-4BDC-BF60-7078DF93BD98}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C545E045-C885-4BDC-BF60-7078DF93BD98}.Release|Any CPU.Build.0 = Release|Any CPU + {5BC73D57-39BC-46F0-9B07-C7AFC65635FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5BC73D57-39BC-46F0-9B07-C7AFC65635FD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5BC73D57-39BC-46F0-9B07-C7AFC65635FD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5BC73D57-39BC-46F0-9B07-C7AFC65635FD}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Code/Money_Bridge/Bridge_Money.sln.DotSettings.user b/Code/Money_Bridge/Bridge_Money.sln.DotSettings.user new file mode 100644 index 0000000..62b82ac --- /dev/null +++ b/Code/Money_Bridge/Bridge_Money.sln.DotSettings.user @@ -0,0 +1,4 @@ +<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> + <s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=7f881cf9_002D5d3d_002D4ec7_002D9a80_002Dab4711aa407c/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="Test_GetTextFrom" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <Solution /> +</SessionState></s:String></wpf:ResourceDictionary> \ No newline at end of file diff --git a/Code/Money_Bridge/Bridge_Money_Console/Bridge_Money_Console.csproj b/Code/Money_Bridge/Bridge_Money_Console/Bridge_Money_Console.csproj new file mode 100644 index 0000000..2c0dacc --- /dev/null +++ b/Code/Money_Bridge/Bridge_Money_Console/Bridge_Money_Console.csproj @@ -0,0 +1,8 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + </PropertyGroup> + +</Project> diff --git a/Code/Money_Bridge/Bridge_Money_Console/Money.cs b/Code/Money_Bridge/Bridge_Money_Console/Money.cs new file mode 100644 index 0000000..e0259f7 --- /dev/null +++ b/Code/Money_Bridge/Bridge_Money_Console/Money.cs @@ -0,0 +1,13 @@ +using System; + +namespace Bridge_Money_Console +{ + public struct Money + { + // Ranges from 0 to 99 + public int Cents; + + public int Euro; + } + +} \ No newline at end of file diff --git a/Code/Money_Bridge/Bridge_Money_Console/MoneyUtility.cs b/Code/Money_Bridge/Bridge_Money_Console/MoneyUtility.cs new file mode 100644 index 0000000..4260195 --- /dev/null +++ b/Code/Money_Bridge/Bridge_Money_Console/MoneyUtility.cs @@ -0,0 +1,88 @@ +using System; + +namespace Bridge_Money_Console +{ + + public static class MoneyUtility + { + public static Money CreateFrom(int euro, int cents) + { + Money result = new Money(); + + int euroInCents = euro * 100; + int totalCents = cents + euroInCents; + + result.Euro = totalCents / 100; + result.Cents = totalCents % 100; + + return result; + } + + public static Money Add(Money leftAmount, Money rightAmount) + { + int totalCents = MoneyUtility.GetTotalCentsFrom(leftAmount) + + MoneyUtility.GetTotalCentsFrom(rightAmount); + + Money result = new Money(); + result.Euro = totalCents / 100; + result.Cents = totalCents % 100; + + return result; + } + + public static Money Subtract(Money toSubstractFrom, Money toSubstract) + { + int totalCents = MoneyUtility.GetTotalCentsFrom(toSubstractFrom) + - MoneyUtility.GetTotalCentsFrom(toSubstract); + + Money result = new Money(); + + result.Euro = totalCents / 100; + result.Cents = totalCents % 100; + + return result; + } + + public static Money GetChange(Money price, Money actualPayment) + { + Money change = Subtract(actualPayment, price); + + if (change.Euro < 0 || change.Cents < 0) + { + change.Euro = 0; + change.Cents = 0; + } + + return change; + } + + public static bool IsEqual(Money leftAmount, Money rightAmount) + { + return leftAmount.Euro == rightAmount.Euro && + leftAmount.Cents == rightAmount.Cents; + } + + public static string GetTextFrom(Money toConvertToText) + { + // Make sure that the numbers are positive to prevents things like + // texts like 0.-45 + int absoluteCents = Math.Abs(toConvertToText.Cents); + int absoluteEuro = Math.Abs(toConvertToText.Euro); + + // If one cents or euro is negative then the whole amount is negative + // Covers the case in which the euro amount is zero but the cents is negative. + bool isNegative = toConvertToText.Euro < 0 || toConvertToText.Cents < 0; + string sign = isNegative ? "-" : ""; + + return $"{sign}{absoluteEuro}.{absoluteCents} Euro"; + } + + public static int GetTotalCentsFrom(Money toGetCentsFrom) + { + int totalCents = toGetCentsFrom.Cents; + int euroInCents = toGetCentsFrom.Euro * 100; + return totalCents + euroInCents; + } + } + +} \ No newline at end of file diff --git a/Code/Money_Bridge/Bridge_Money_Console/Program.cs b/Code/Money_Bridge/Bridge_Money_Console/Program.cs new file mode 100644 index 0000000..b247f81 --- /dev/null +++ b/Code/Money_Bridge/Bridge_Money_Console/Program.cs @@ -0,0 +1,13 @@ +using System; + +namespace Bridge_Money_Console +{ + internal static class Program + { + private static void Main(string[] args) + { + Money amount = MoneyUtility.CreateFrom(-2, 20); + Console.WriteLine($"Amount is {MoneyUtility.GetTextFrom(amount)}"); + } + } +} \ No newline at end of file diff --git a/Code/Money_Bridge/Bridge_Money_Tests/Bridge_Money_Tests.csproj b/Code/Money_Bridge/Bridge_Money_Tests/Bridge_Money_Tests.csproj new file mode 100644 index 0000000..6a04292 --- /dev/null +++ b/Code/Money_Bridge/Bridge_Money_Tests/Bridge_Money_Tests.csproj @@ -0,0 +1,20 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>netcoreapp3.1</TargetFramework> + + <IsPackable>false</IsPackable> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> + <PackageReference Include="xunit" Version="2.4.0" /> + <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> + <PackageReference Include="coverlet.collector" Version="1.2.0" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\Bridge_Money_Console\Bridge_Money_Console.csproj" /> + </ItemGroup> + +</Project> diff --git a/Code/Money_Bridge/Bridge_Money_Tests/TestCases_MoneyUtility.cs b/Code/Money_Bridge/Bridge_Money_Tests/TestCases_MoneyUtility.cs new file mode 100644 index 0000000..ea42a4a --- /dev/null +++ b/Code/Money_Bridge/Bridge_Money_Tests/TestCases_MoneyUtility.cs @@ -0,0 +1,213 @@ +using Xunit; + +using Bridge_Money_Console; + +namespace Bridge_Money_Tests +{ + public partial class TestMoneyUtility + { + public static TheoryData<int, int, Money> TestData_CreateFrom + => new TheoryData<int, int, Money>() + { + { + 5, 0, new Money() {Euro = 5, Cents = 0} + }, + { + 0, 33, new Money() {Euro = 0, Cents = 33} + }, + { + 2, -33, new Money() {Euro = 1, Cents = 67} + }, + { + 2, 20, new Money() {Euro = 2, Cents = 20} + }, + }; + + /// <summary> + /// 1. Parameter as Money + /// Given money amount to be converted to a text. + /// 2. Parameter as string + /// Expected text from money amount + /// </summary> + /// <returns></returns> + public static TheoryData<Money, string> TestData_GetTextFrom + => new TheoryData<Money, string>() + { + { + new Money() {Euro = 0, Cents = 0}, + "0.0 Euro" + }, + { + new Money() {Euro = 2, Cents = 0}, + "2.0 Euro" + }, + { + new Money() {Euro = 0, Cents = 45}, + "0.45 Euro" + }, + { + new Money() {Euro = 2, Cents = 50}, + "2.50 Euro" + }, + { + new Money() {Euro = -2, Cents = 0}, + "-2.0 Euro" + }, + { + new Money() {Euro = 0, Cents = -45}, + "-0.45 Euro" + }, + { + new Money() {Euro = -2, Cents = -50}, + "-2.50 Euro" + }, + }; + + /// <summary> + /// 1. Parameter as Money + /// Given money amount to be converted to cents only. + /// 2. Parameter as int + /// Expected amount of total cents + /// </summary> + /// <returns></returns> + public static TheoryData<Money, int> TestData_GetTotalCentsFrom + => new TheoryData<Money, int>() + { + { + new Money() {Euro = 2, Cents = 0}, + 200 + }, + { + new Money() {Euro = 0, Cents = 45}, + 45 + }, + { + new Money() {Euro = 2, Cents = 20}, + 220 + }, + { + new Money() {Euro = -2, Cents = 0}, + -200 + }, + { + new Money() {Euro = 0, Cents = -45}, + -45 + }, + { + new Money() {Euro = -2, Cents = -20}, + -220 + }, + }; + + + public static TheoryData<Money, Money, bool> TestData_IsEqual + => new TheoryData<Money, Money, bool>() + { + { + new Money() {Euro = 2, Cents = 20}, + new Money() {Euro = 2, Cents = 20}, + true + }, + { + new Money() {Euro = 2, Cents = 10}, + new Money() {Euro = 2, Cents = 20}, + false + }, + { + new Money() {Euro = -2, Cents = -20}, + new Money() {Euro = 2, Cents = 20}, + false + }, + { + new Money() {Euro = -2, Cents = -20}, + new Money() {Euro = -2, Cents = -20}, + true + } + }; + + public static TheoryData<Money, Money, Money> TestData_Add + => new TheoryData<Money, Money, Money>() + { + { + new Money() {Euro = 0, Cents = 0}, + new Money() {Euro = 0, Cents = 0}, + new Money() {Euro = 0, Cents = 0} + }, + { + new Money() {Euro = 2, Cents = 0}, + new Money() {Euro = 5, Cents = 0}, + new Money() {Euro = 7, Cents = 0} + }, + { + new Money() {Euro = 0, Cents = 50}, + new Money() {Euro = 0, Cents = 80}, + new Money() {Euro = 1, Cents = 30} + }, + { + new Money() {Euro = 2, Cents = 50}, + new Money() {Euro = 1, Cents = 80}, + new Money() {Euro = 4, Cents = 30} + }, + { + new Money() {Euro = 2, Cents = 0}, + new Money() {Euro = -5, Cents = 0}, + new Money() {Euro = -3, Cents = 0} + }, + { + new Money() {Euro = 0, Cents = 50}, + new Money() {Euro = 0, Cents = -40}, + new Money() {Euro = 0, Cents = 10} + }, + { + new Money() {Euro = 2, Cents = 50}, + new Money() {Euro = -4, Cents = -40}, + new Money() {Euro = -1, Cents = -90} + }, + }; + + public static TheoryData<Money, Money, Money> TestData_Subtract + => new TheoryData<Money, Money, Money>() + { + { + new Money(), + new Money(), + new Money() + }, + { + new Money() { Euro = 12, Cents = 0}, + new Money() { Euro = 7, Cents = 0}, + new Money() { Euro = 5, Cents = 0} + }, + { + new Money() { Euro = 5, Cents = 20}, + new Money() { Euro = 7, Cents = 0}, + new Money() { Euro = -1, Cents = -80} + }, + }; + + public static TheoryData<Money, Money, Money> TestData_GetChange + => new TheoryData<Money, Money, Money>() + { + { + new Money() {Euro = 2, Cents = 0}, + new Money() {Euro = 3, Cents = 0}, + new Money() {Euro = 1, Cents = 0} + }, + { + new Money() {Euro = 0, Cents = 80}, + new Money() {Euro = 0, Cents = 90}, + new Money() {Euro = 0, Cents = 10} + }, + { + new Money() {Euro = 2, Cents = 50}, + new Money() {Euro = 4, Cents = 0}, + new Money() {Euro = 1, Cents = 50} + }, + { + new Money() {Euro = 5, Cents = 0}, + new Money() {Euro = 4, Cents = 20}, + new Money() {Euro = 0, Cents = 0} + } + }; + } +} \ No newline at end of file diff --git a/Code/Money_Bridge/Bridge_Money_Tests/TestMoneyUtility.cs b/Code/Money_Bridge/Bridge_Money_Tests/TestMoneyUtility.cs new file mode 100644 index 0000000..b79d95b --- /dev/null +++ b/Code/Money_Bridge/Bridge_Money_Tests/TestMoneyUtility.cs @@ -0,0 +1,89 @@ +using System; +using Xunit; + +using Bridge_Money_Console; + + +namespace Bridge_Money_Tests +{ + public partial class TestMoneyUtility + { + [Theory] + [MemberData(nameof(TestData_CreateFrom))] + public void Test_CreateFrom(int givenEuro, int givenCents, Money expectedAmount) + { + // Act + Money actualAmount = MoneyUtility.CreateFrom(givenEuro, givenCents); + + // Assert + Assert.Equal(expectedAmount.Euro, actualAmount.Euro); + Assert.Equal(expectedAmount.Cents, actualAmount.Cents); + } + + [Theory] + [MemberData(nameof(TestData_GetTextFrom))] + public void Test_GetTextFrom(Money givenAmount, string expectedText) + { + // Act + string actualText = MoneyUtility.GetTextFrom(givenAmount); + + // Assert + Assert.Equal(expectedText, actualText); + } + + [Theory] + [MemberData(nameof(TestData_GetTotalCentsFrom))] + public void Test_GetTotalCentsFrom(Money givenAmount, int expectedCentsAmount) + { + // Act + int actualCentAmount = MoneyUtility.GetTotalCentsFrom(givenAmount); + // Assert + Assert.Equal(expectedCentsAmount, actualCentAmount); + } + + [Theory] + [MemberData(nameof(TestData_Add))] + public void Test_Add(Money leftAmount, Money rightAmount, Money expectedSum) + { + // Act + Money actualSum = MoneyUtility.Add(leftAmount, rightAmount); + // Assert + Assert.Equal(expectedSum.Euro, actualSum.Euro); + Assert.Equal(expectedSum.Cents, actualSum.Cents); + } + + [Theory] + [MemberData(nameof(TestData_Subtract))] + public void Test_Subtract(Money leftAmount, Money rightAmount, Money expectedDifference) + { + // Act + Money actualSum = MoneyUtility.Subtract(leftAmount, rightAmount); + // Assert + Assert.Equal(expectedDifference.Euro, actualSum.Euro); + Assert.Equal(expectedDifference.Cents, actualSum.Cents); + } + + [Theory] + [MemberData(nameof(TestData_IsEqual))] + public void Test_IsEqual(Money leftAmount, Money rightAmount, bool expected) + { + // Act + bool actual = MoneyUtility.IsEqual(leftAmount, rightAmount); + // Assert + Assert.Equal(expected, actual); + } + + [Theory] + [MemberData(nameof(TestData_GetChange))] + public void Test_GetChange(Money price, Money givenMoney, Money expectedChange) + { + // Act + Money actualChange = MoneyUtility.GetChange(price, givenMoney); + // Assert + Assert.Equal(expectedChange.Euro, actualChange.Euro); + Assert.Equal(expectedChange.Cents, actualChange.Cents); + } + + + } +} \ No newline at end of file diff --git a/Code/Money_Bridge/global.json b/Code/Money_Bridge/global.json new file mode 100644 index 0000000..5898a04 --- /dev/null +++ b/Code/Money_Bridge/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "3.1.0", + "rollForward": "latestMinor", + "allowPrerelease": false + } +} \ No newline at end of file -- GitLab