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
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

#ignore thumbnails created by windows
Thumbs.db
#Ignore files build by Visual Studio
*.obj
*.exe
*.pdb
*.user
*.aps
*.pch
*.vspscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.cache
*.ilk
*.log
*.dll
*.lib
*.sbr
bin/
obj/
packages/
.vs/


# .NET
obj/
bin/
.vs/
*.user

# local env files
.env.local
.env.*.local
31 changes: 31 additions & 0 deletions CashRegister.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31321.278
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CashRegister", "CashRegister\CashRegister.csproj", "{0665BD27-5519-4B5D-A292-FE2FC9932ADD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CashRegisterTests", "CashRegisterTests\CashRegisterTests.csproj", "{F1439C9C-5A59-4716-9189-36A5D2F5CEFC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0665BD27-5519-4B5D-A292-FE2FC9932ADD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0665BD27-5519-4B5D-A292-FE2FC9932ADD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0665BD27-5519-4B5D-A292-FE2FC9932ADD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0665BD27-5519-4B5D-A292-FE2FC9932ADD}.Release|Any CPU.Build.0 = Release|Any CPU
{F1439C9C-5A59-4716-9189-36A5D2F5CEFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F1439C9C-5A59-4716-9189-36A5D2F5CEFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1439C9C-5A59-4716-9189-36A5D2F5CEFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1439C9C-5A59-4716-9189-36A5D2F5CEFC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4370CBE7-F5AC-48A3-995F-3A0818052959}
EndGlobalSection
EndGlobal
118 changes: 118 additions & 0 deletions CashRegister/CashRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CashRegister
{
public class CashRegister : ICashRegister
{
private decimal Change;

public Change GetChange(decimal price, decimal totalPaid)
{
// Return Empty Change Object if inputs are less than 0
if(price < 0 || totalPaid < 0)
{
return new Change();
}
this.Change = totalPaid - price;
if ((price * 100) % 3 == 0)
{
return RandomChange();
}
else
{
return RegularChange();
}

}

private Change RandomChange()
{
Change ChangeReturn = new Change();
int randomNumber;
while (Change > 0)
{

randomNumber = new Random().Next(1, 6);

switch (randomNumber)
{
case 1:
if (Change >= 1.00M)
{
ChangeReturn.AddDollar();
Change -= 1.00M;
}
break;
case 2:
if (Change >= 0.25M)
{
ChangeReturn.AddQuarter();
Change -= 0.25M;
}
break;
case 3:
if (Change >= 0.10M)
{
ChangeReturn.AddDime();
Change -= 0.10M;
}
break;
case 4:
if (Change >= 0.05M)
{
ChangeReturn.AddNickel();
Change -= 0.05M;
}
break;
case 5:
if (Change >= 0.01M)
{
ChangeReturn.AddPenny();
Change -= 0.01M;
}
break;
default:
break;
}
}

return ChangeReturn;
}

private Change RegularChange()
{
Change ChangeReturn = new Change();
while (Change > 0)
{
if (Change >= 1.00M)
{
ChangeReturn.AddDollar();
Change -= 1.00M;
}
else if (Change >= 0.25M)
{
ChangeReturn.AddQuarter();
Change -= 0.25M;
}
else if (Change >= 0.10M)
{
ChangeReturn.AddDime();
Change -= 0.10M;
}
else if (Change >= 0.05M)
{
ChangeReturn.AddNickel();
Change -= 0.05M;
}
else if (Change >= 0.01M)
{
ChangeReturn.AddPenny();
Change -= 0.01M;
}
}
return ChangeReturn;
}
}
}
8 changes: 8 additions & 0 deletions CashRegister/CashRegister.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
39 changes: 39 additions & 0 deletions CashRegister/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;

namespace CashRegister
{
class Program
{
static void Main(string[] args)
{

ICashRegister cr = new CashRegister();
IReadService reader = new ReadService();
IWriteService writer = new WriteService();

List<decimal> output = new List<decimal>();

try
{
output = reader.ReadFile();
} catch (Exception e)
{
Console.WriteLine(e.Message);
}


List<Change> changeList = new List<Change>();

for (int i = 0; i < output.Count - 1; i += 2)
{
Change change = new Change();
change = cr.GetChange(output[i], output[i + 1]);
changeList.Add(change);
}

writer.WriteFile(changeList);

}
}
}
49 changes: 49 additions & 0 deletions CashRegister/Services/ReadService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

namespace CashRegister
{
public class ReadService : IReadService
{
private static string FileName { get; } = "input.txt";
//private static string Dir { get; } = Environment.CurrentDirectory;
//private static string outputDir { get; } = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

private static readonly string WorkingDirectory = Directory.GetParent(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName).Parent.FullName;

private readonly string FilePath = Path.Combine(WorkingDirectory, FileName);

public List<decimal> ReadFile()
{



List<decimal> output = new List<decimal>();

try
{
using(StreamReader streamReader = new StreamReader(FilePath))
{
while (!streamReader.EndOfStream)
{
string line = streamReader.ReadLine();
string[] temp = new string[2];
temp = line.Split(',');

output.Add(decimal.Parse(temp[0]));
output.Add(decimal.Parse(temp[1]));
}
}
}
catch (Exception e)
{

throw e;
}
return output;
}
}
}
Loading