diff --git a/CashRegister.Test/.gitignore b/CashRegister.Test/.gitignore new file mode 100644 index 00000000..e69de29b diff --git a/CashRegister.Test/CashRegister.Test.csproj b/CashRegister.Test/CashRegister.Test.csproj new file mode 100644 index 00000000..4a0c0ed7 --- /dev/null +++ b/CashRegister.Test/CashRegister.Test.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + diff --git a/CashRegister.Test/CashRegister.cs b/CashRegister.Test/CashRegister.cs new file mode 100644 index 00000000..3429f368 --- /dev/null +++ b/CashRegister.Test/CashRegister.cs @@ -0,0 +1,31 @@ +using NUnit.Framework; +using static CashRegister.Main; + +namespace CashRegister.Test +{ + public class CashRegisterTest + { + + [Test] + [TestCase(1.97, 2.00, "3 pennies")] + [TestCase(3.34, 5.00, "1 dollar, 2 quarters, 1 dime, 1 nickel, 1 penny")] + [TestCase(6.49, 7.00, "2 quarters, 1 penny")] + [TestCase(1.97, 4.00, "2 dollars, 3 pennies")] + [TestCase(9.97, 10.00, "3 pennies")] + [TestCase(1234.57, 2000.00, "7 hundreds, 1 fifty, 1 ten, 1 five, 1 quarter, 1 dime, 1 nickel, 3 pennies")] + public void GetChangeTest(decimal cost, decimal given, string expected) + { + var output = GetChange(cost, given); + Assert.AreEqual(output, expected); + } + + [Test] + [TestCase(3.00, 4.00)] + public void GetRandomChangeTest(decimal cost, decimal given) + { + var randomOutput = GetChange(cost, given); + var otherRandomOutput = GetChange(cost, given); + Assert.AreNotEqual(randomOutput, otherRandomOutput); + } + } +} \ No newline at end of file diff --git a/CashRegister/.gitignore b/CashRegister/.gitignore new file mode 100644 index 00000000..8d4a6c08 --- /dev/null +++ b/CashRegister/.gitignore @@ -0,0 +1,2 @@ +bin +obj \ No newline at end of file diff --git a/CashRegister/CashRegister.csproj b/CashRegister/CashRegister.csproj new file mode 100644 index 00000000..16cf038b --- /dev/null +++ b/CashRegister/CashRegister.csproj @@ -0,0 +1,11 @@ + + + + netcoreapp3.1 + + + + full + + + diff --git a/CashRegister/Main.cs b/CashRegister/Main.cs new file mode 100644 index 00000000..c8802e98 --- /dev/null +++ b/CashRegister/Main.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; + +namespace CashRegister +{ + public class Denominator + { + public string Name; + public string Plural; + public decimal Nominal; + } + + public static class Main + { + + private static List _coins = + new List() + { + new Denominator {Name = "hundred", Plural = "hundreds", Nominal = 100m}, + new Denominator {Name = "fifty", Plural = "fifties", Nominal = 50m}, + new Denominator {Name = "twenty", Plural = "twenties", Nominal = 20m}, + new Denominator {Name = "ten", Plural = "tens", Nominal = 10m}, + new Denominator {Name = "five", Plural = "fives", Nominal = 5m}, + new Denominator {Name = "dollar", Plural = "dollars", Nominal = 1m}, + new Denominator {Name = "quarter", Plural = "quarters", Nominal = 0.25m}, + new Denominator {Name = "dime", Plural = "dimes", Nominal = 0.10m}, + new Denominator {Name = "nickel", Plural = "nickels", Nominal = 0.05m}, + new Denominator {Name = "penny", Plural = "pennies", Nominal = 0.01m} + }; + + public static string GetChange(decimal cost, decimal given) + { + var change = given - cost; + + if (cost * 100 % 3 == 0) + return _getRandomChange(change); + + var changeDescriptor = new List(); + + foreach (var coin in _coins) + { + var count = (int) (change / coin.Nominal); + change -= count * coin.Nominal; + + var coinName = $"{(count > 1 ? coin.Plural : coin.Name )}"; + + if(count != 0) + changeDescriptor.Add($"{count} {coinName}"); + } + + return string.Join(", ", changeDescriptor); + } + + private static string _getRandomChange(decimal change) + { + var changeDescriptor = new List(); + + foreach (var coin in _coins) + { + var count = (int) (change / coin.Nominal); + decimal randomCount = coin.Name == "penny" ? count : new Random().Next(0, count); + + change -= randomCount * coin.Nominal; + + var coinName = $"{(count > 1 ? coin.Plural : coin.Name )}"; + + if(randomCount != 0) + changeDescriptor.Add($"{randomCount} {coinName}"); + } + + return string.Join(", ", changeDescriptor); + } + } +} \ No newline at end of file diff --git a/CreativeCashDrawSolutions.sln b/CreativeCashDrawSolutions.sln new file mode 100644 index 00000000..a768c908 --- /dev/null +++ b/CreativeCashDrawSolutions.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Desktop", "Desktop\Desktop.csproj", "{E622993A-01AA-4419-8863-732AB8175AA8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CashRegister", "CashRegister\CashRegister.csproj", "{5E7ECCC7-8C6D-4CDC-AE65-8B63ECF190AA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CashRegister.Test", "CashRegister.Test\CashRegister.Test.csproj", "{BEB7BFB2-AB84-49AA-A0B9-A27E643D9A61}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E622993A-01AA-4419-8863-732AB8175AA8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E622993A-01AA-4419-8863-732AB8175AA8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E622993A-01AA-4419-8863-732AB8175AA8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E622993A-01AA-4419-8863-732AB8175AA8}.Release|Any CPU.Build.0 = Release|Any CPU + {5E7ECCC7-8C6D-4CDC-AE65-8B63ECF190AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5E7ECCC7-8C6D-4CDC-AE65-8B63ECF190AA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5E7ECCC7-8C6D-4CDC-AE65-8B63ECF190AA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5E7ECCC7-8C6D-4CDC-AE65-8B63ECF190AA}.Release|Any CPU.Build.0 = Release|Any CPU + {BEB7BFB2-AB84-49AA-A0B9-A27E643D9A61}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BEB7BFB2-AB84-49AA-A0B9-A27E643D9A61}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BEB7BFB2-AB84-49AA-A0B9-A27E643D9A61}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BEB7BFB2-AB84-49AA-A0B9-A27E643D9A61}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Desktop/.gitignore b/Desktop/.gitignore new file mode 100644 index 00000000..e69de29b diff --git a/Desktop/App.xaml b/Desktop/App.xaml new file mode 100755 index 00000000..4c94a041 --- /dev/null +++ b/Desktop/App.xaml @@ -0,0 +1,8 @@ + + + + + + diff --git a/Desktop/App.xaml.cs b/Desktop/App.xaml.cs new file mode 100755 index 00000000..ad1ae38d --- /dev/null +++ b/Desktop/App.xaml.cs @@ -0,0 +1,24 @@ +using Avalonia; +using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Markup.Xaml; + +namespace Desktop +{ + public class App : Application + { + public override void Initialize() + { + AvaloniaXamlLoader.Load(this); + } + + public override void OnFrameworkInitializationCompleted() + { + if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + { + desktop.MainWindow = new MainWindow(); + } + + base.OnFrameworkInitializationCompleted(); + } + } +} \ No newline at end of file diff --git a/Desktop/Desktop.csproj b/Desktop/Desktop.csproj new file mode 100755 index 00000000..b107c757 --- /dev/null +++ b/Desktop/Desktop.csproj @@ -0,0 +1,22 @@ + + + WinExe + netcoreapp3.1 + + + + %(Filename) + + + Designer + + + + + + + + + + + diff --git a/Desktop/MainWindow.xaml b/Desktop/MainWindow.xaml new file mode 100755 index 00000000..676c10cf --- /dev/null +++ b/Desktop/MainWindow.xaml @@ -0,0 +1,14 @@ + + + + + + + \ No newline at end of file diff --git a/Desktop/MainWindow.xaml.cs b/Desktop/MainWindow.xaml.cs new file mode 100755 index 00000000..69866aac --- /dev/null +++ b/Desktop/MainWindow.xaml.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; +using Desktop.ViewModels; +using static CashRegister.Main; + +namespace Desktop +{ + public class MainWindow : Window + { + CashRegisterViewModel ViewModel = new CashRegisterViewModel(); + public MainWindow() + { + InitializeComponent(); + DataContext = ViewModel; + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + this.FindControl