Skip to content
Snippets Groups Projects
Commit 6176f4da authored by BoolPurist's avatar BoolPurist
Browse files

Added solution for possible prog task

It is about structures and functions
parent b118aaac
No related branches found
No related tags found
No related merge requests found
Showing with 479 additions and 0 deletions
obj/
bin/
\ No newline at end of file

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
<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">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Test_GetTextFrom" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
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
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
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
<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>
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
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
{
"sdk": {
"version": "3.1.0",
"rollForward": "latestMinor",
"allowPrerelease": false
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment