Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added CashRegister.Test/.gitignore
Empty file.
19 changes: 19 additions & 0 deletions CashRegister.Test/CashRegister.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CashRegister\CashRegister.csproj" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions CashRegister.Test/CashRegister.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 2 additions & 0 deletions CashRegister/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin
obj
11 changes: 11 additions & 0 deletions CashRegister/CashRegister.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugType>full</DebugType>
</PropertyGroup>

</Project>
74 changes: 74 additions & 0 deletions CashRegister/Main.cs
Original file line number Diff line number Diff line change
@@ -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<Denominator> _coins =
new List<Denominator>()
{
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<string>();

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<string>();

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);
}
}
}
28 changes: 28 additions & 0 deletions CreativeCashDrawSolutions.sln
Original file line number Diff line number Diff line change
@@ -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
Empty file added Desktop/.gitignore
Empty file.
8 changes: 8 additions & 0 deletions Desktop/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Desktop.App">
<Application.Styles>
<StyleInclude Source="avares://Avalonia.Themes.Default/DefaultTheme.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Accents/BaseLight.xaml"/>
</Application.Styles>
</Application>
24 changes: 24 additions & 0 deletions Desktop/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
22 changes: 22 additions & 0 deletions Desktop/Desktop.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Update="**\*.xaml.cs">
<DependentUpon>%(Filename)</DependentUpon>
</Compile>
<AvaloniaResource Include="**\*.xaml">
<SubType>Designer</SubType>
</AvaloniaResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.9.0" />
<PackageReference Include="Avalonia.Desktop" Version="0.9.0" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.9.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CashRegister\CashRegister.csproj" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions Desktop/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:Desktop.ViewModels"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Desktop.MainWindow"
Title="Cash Register">
<StackPanel Orientation="Vertical" Spacing="4" Margin="25">
<Button FontSize="20" Name="OpenFile">OPEN FILE</Button>
<TextBlock Text="{Binding Path=Input}"/>
<TextBlock Text="{Binding Path=Output}"/>
</StackPanel>
</Window>
47 changes: 47 additions & 0 deletions Desktop/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -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<Button>("OpenFile").Click += async delegate
{
OpenFileDialog dialog = new OpenFileDialog();

string[] result = await dialog.ShowAsync(this);
string fileContents = File.ReadAllText(result.First());
string[] items = fileContents.Split("\n");

List<string> changeOutput = items.Select(item =>
{
string[] input = item.Split(",");
decimal cost = decimal.Parse(input[0]);
decimal given = decimal.Parse(input[1]);

return GetChange(cost, given);
}).ToList();

ViewModel.Input = string.Join("\n", items);;
ViewModel.Output = string.Join("\n", changeOutput);

};
}
}
}
23 changes: 23 additions & 0 deletions Desktop/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Logging.Serilog;

namespace Desktop
{
class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);

// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToDebug();
}
}
29 changes: 29 additions & 0 deletions Desktop/ViewModels/CashRegisterViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Threading;
using ReactiveUI;

namespace Desktop.ViewModels
{
public class CashRegisterViewModel : ReactiveObject
{
private string _input = "";
private string _output = "";

public string Input
{
get { return _input; }
set { this.RaiseAndSetIfChanged(ref this._input, value); }
}

public string Output
{
get { return _output; }
set { this.RaiseAndSetIfChanged(ref this._output, value); }
}
}
}
6 changes: 6 additions & 0 deletions Desktop/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="AvaloniaCI" value="https://www.myget.org/F/avalonia-ci/api/v2" />
</packageSources>
</configuration>