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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/.vs/CashRegister/v16/Server/sqlite3
/SWCashRegisterSB/.vs/SWCashRegisterSB/v16
/SWCashRegisterSB/bin/Debug
/SWCashRegisterSB/obj/Debug
/.vs/slnx.sqlite
Binary file added .vs/CashRegister/v16/.suo
Binary file not shown.
6 changes: 6 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ExpandedNodes": [
""
],
"PreviewInSolutionExplorer": false
}
6 changes: 6 additions & 0 deletions SWCashRegisterSB/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
31 changes: 31 additions & 0 deletions SWCashRegisterSB/Calculators/ChangeCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using SWCashRegisterSB.Calculators.Interfaces;
using SWCashRegisterSB.Models;
using SWCashRegisterSB.Models.Interfaces;
using SWCashRegisterSB.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SWCashRegisterSB.Calculators
{
public class ChangeCalculator : IChangeCalculator
{
public List<IChangeResult> CalculateChange(decimal changeAmount)
{
var result = new List<IChangeResult>();

while (changeAmount > 0)
{
var denomination = DenominationUtils.OrderedDenominations.First(x => x.Value <= changeAmount);
var count = decimal.ToInt32(changeAmount / denomination.Value);

result.Add(new ChangeResult(denomination, count));
changeAmount -= denomination.Value * count;
}

return result;
}
}
}
14 changes: 14 additions & 0 deletions SWCashRegisterSB/Calculators/Interfaces/IChangeCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using SWCashRegisterSB.Models.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SWCashRegisterSB.Calculators.Interfaces
{
public interface IChangeCalculator
{
List<IChangeResult> CalculateChange(decimal changeAmount);
}
}
40 changes: 40 additions & 0 deletions SWCashRegisterSB/Calculators/RandomChangeCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using SWCashRegisterSB.Calculators.Interfaces;
using SWCashRegisterSB.Models;
using SWCashRegisterSB.Models.Interfaces;
using SWCashRegisterSB.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SWCashRegisterSB.Calculators
{
public class RandomChangeCalculator : IChangeCalculator
{

public List<IChangeResult> CalculateChange(decimal changeAmount)
{
var result = new Dictionary<string,IChangeResult>();
var random = new Random();

while(changeAmount > 0)
{
var denominations = DenominationUtils.OrderedDenominations.Where(x => x.Value <= changeAmount).ToList();
var index = random.Next(0, denominations.Count());
var denomination = denominations[index];
var count = random.Next(1, decimal.ToInt32(changeAmount / denomination.Value));

if (result.ContainsKey(denomination.Name))
result[denomination.Name].Quantity += count;
else
result.Add(denomination.Name, new ChangeResult(denomination, count));

changeAmount -= denomination.Value * count;

}

return result.Values.ToList();
}
}
}
16 changes: 16 additions & 0 deletions SWCashRegisterSB/Models/ChangeDenomination.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using SWCashRegisterSB.Models.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SWCashRegisterSB.Models
{
public class ChangeDenomination : IDenomination
{
public string Name { get; set; }
public string PluralName { get; set; }
public decimal Value { get; set; }
}
}
20 changes: 20 additions & 0 deletions SWCashRegisterSB/Models/ChangeResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using SWCashRegisterSB.Models.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SWCashRegisterSB.Models
{
public class ChangeResult : IChangeResult
{
public ChangeResult(IDenomination denomination, int quantity)
{
Denomination = denomination;
Quantity = quantity;
}
public IDenomination Denomination { get; set; }
public int Quantity { get; set; }
}
}
14 changes: 14 additions & 0 deletions SWCashRegisterSB/Models/Interfaces/IChangeResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SWCashRegisterSB.Models.Interfaces
{
public interface IChangeResult
{
IDenomination Denomination { get; set; }
int Quantity { get; set; }
}
}
15 changes: 15 additions & 0 deletions SWCashRegisterSB/Models/Interfaces/IDenomination.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SWCashRegisterSB.Models.Interfaces
{
public interface IDenomination
{
string Name { get; set; }
string PluralName { get; set; }
decimal Value { get; set; }
}
}
94 changes: 94 additions & 0 deletions SWCashRegisterSB/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using SWCashRegisterSB.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SWCashRegisterSB
{
/// <summary>
/// Softwriters Cash Register by Stephen Bierly
/// Input file location is: "{RepoPath}\\SWCashRegisterSB\\input.txt"
/// Output file location is: "{RepoPath}\\SWCashRegisterSB\\output.txt"
/// </summary>
class Program
{
static void Main(string[] args)
{
var basePath = Path.GetFullPath(@"..\..\");
var inputPath = basePath + "input.txt";
var outputPath = basePath + "output.txt";


if (!File.Exists(inputPath))
{
Console.WriteLine($"Could not find input file. Expected to be '{inputPath}'");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
return;
}

try
{
using (StreamWriter outWriter = new StreamWriter(outputPath, false))
{
var currentLineNum = 1;
foreach (var line in File.ReadLines(inputPath))
{
var parts = line.Split(',');
decimal amountDue;
decimal amountPaid;
if(parts.Length != 2 || !decimal.TryParse(parts[0], out amountDue) || !decimal.TryParse(parts[1], out amountPaid))
{
Console.WriteLine($"Improperly formated line '{line}' on line '{currentLineNum}");
currentLineNum++;
continue;
}

//Excluding the possibility of refunds currently
if(amountDue < 0 || amountPaid < 0)
{
Console.WriteLine($"Amount Paid ('{amountPaid}') and Amount Due ('{amountDue}') must be positive.");
continue;
}

var changeDue = amountPaid - amountDue;

if(changeDue < 0)
{
Console.WriteLine($"Insufficient payment on line '{currentLineNum}', payment is '{Math.Abs(changeDue)}' less than required.");
currentLineNum++;
continue;
}

if (changeDue == 0)
{
Console.WriteLine($"No change due on line {currentLineNum}");
currentLineNum++;
continue;
}

var calculator = DenominationUtils.GetChangeCalculator(amountDue);

var change = calculator.CalculateChange(changeDue);

var changeOutputLine = DenominationUtils.GetChangeOutput(change);

outWriter.WriteLine(changeOutputLine);
currentLineNum++;
}

}
}
catch(Exception e)
{
Console.WriteLine($"Unexpected Error : '{e.Message}'");
}

Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
36 changes: 36 additions & 0 deletions SWCashRegisterSB/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SWCashRegisterSB")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SWCashRegisterSB")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("5cf33ad0-25e5-4d11-954f-ad914ae820ac")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
62 changes: 62 additions & 0 deletions SWCashRegisterSB/SWCashRegisterSB.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{5CF33AD0-25E5-4D11-954F-AD914AE820AC}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>SWCashRegisterSB</RootNamespace>
<AssemblyName>SWCashRegisterSB</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Calculators\ChangeCalculator.cs" />
<Compile Include="Calculators\Interfaces\IChangeCalculator.cs" />
<Compile Include="Calculators\RandomChangeCalculator.cs" />
<Compile Include="Models\ChangeDenomination.cs" />
<Compile Include="Models\ChangeResult.cs" />
<Compile Include="Models\Interfaces\IChangeResult.cs" />
<Compile Include="Models\Interfaces\IDenomination.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\DenominationUtils.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Loading