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
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"program": "${workspaceFolder}/CashRegister/bin/Debug/net5.0/CashRegister.dll",
"name": "CashRegister",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"console": "internalConsole",
"stopAtEntry": false,
"args": ["TEST"]
}

]
}
42 changes: 42 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/CashRegister/CashRegister.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/CashRegister/CashRegister.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/CashRegister/CashRegister.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
46 changes: 46 additions & 0 deletions CashRegister.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CashRegister", "CashRegister\CashRegister.csproj", "{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}.Debug|x64.ActiveCfg = Debug|Any CPU
{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}.Debug|x64.Build.0 = Debug|Any CPU
{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}.Debug|x86.ActiveCfg = Debug|Any CPU
{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}.Debug|x86.Build.0 = Debug|Any CPU
{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}.Release|Any CPU.Build.0 = Release|Any CPU
{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}.Release|x64.ActiveCfg = Release|Any CPU
{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}.Release|x64.Build.0 = Release|Any CPU
{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}.Release|x86.ActiveCfg = Release|Any CPU
{C7AFDA7B-9017-4D1F-AFA3-CF30D2FF1774}.Release|x86.Build.0 = Release|Any CPU
{837BEA06-DC8A-48A9-942F-1F9C7B4D907A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{837BEA06-DC8A-48A9-942F-1F9C7B4D907A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{837BEA06-DC8A-48A9-942F-1F9C7B4D907A}.Debug|x64.ActiveCfg = Debug|Any CPU
{837BEA06-DC8A-48A9-942F-1F9C7B4D907A}.Debug|x64.Build.0 = Debug|Any CPU
{837BEA06-DC8A-48A9-942F-1F9C7B4D907A}.Debug|x86.ActiveCfg = Debug|Any CPU
{837BEA06-DC8A-48A9-942F-1F9C7B4D907A}.Debug|x86.Build.0 = Debug|Any CPU
{837BEA06-DC8A-48A9-942F-1F9C7B4D907A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{837BEA06-DC8A-48A9-942F-1F9C7B4D907A}.Release|Any CPU.Build.0 = Release|Any CPU
{837BEA06-DC8A-48A9-942F-1F9C7B4D907A}.Release|x64.ActiveCfg = Release|Any CPU
{837BEA06-DC8A-48A9-942F-1F9C7B4D907A}.Release|x64.Build.0 = Release|Any CPU
{837BEA06-DC8A-48A9-942F-1F9C7B4D907A}.Release|x86.ActiveCfg = Release|Any CPU
{837BEA06-DC8A-48A9-942F-1F9C7B4D907A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
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>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.IO" Version="4.3.0" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions CashRegister/InputLine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace CashRegister
{
public class InputLine
{
public decimal TotalDue;
public decimal AmountPaid;

public InputLine(decimal totalDue, decimal amountPaid)
{
TotalDue = totalDue;
AmountPaid = amountPaid;
}
}
}
210 changes: 210 additions & 0 deletions CashRegister/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace CashRegister
{
public static class Program
{
public static string myConsoleText;
public static string InputFile;
public static StreamReader InputFileReader;
public static List<InputLine> InputLines = new List<InputLine>();
public static List<String> OutputLines = new List<string>();
public static string OutputFile;
private static Random _random = new Random();


static void Main(string[] args)
{
try
{
if (HasValidArgs(args) && IsValidFile())
{
OutputChange();
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}

public static bool HasValidArgs(string[] args)
{
if (args == null)
{
myConsoleText = "Please provide an input and output file. Ex: CashRegister input.txt output.txt";
Console.WriteLine(myConsoleText);
return false;
}

if ((args.Length == 1) && (args[0] == "TEST"))
{
CashRegisterLibrary.Tests.RunTests();
return false;
}

if (args.Length != 2)
{
myConsoleText = "Please provide an input and output file. Ex: CashRegister input.txt output.txt";
Console.WriteLine(myConsoleText);
return false;
}

InputFile = args[0];
if (!File.Exists(InputFile))
{
myConsoleText = "Input file doesn't exist.";
Console.WriteLine(myConsoleText);
return false;
}

OutputFile = args[1];
File.Delete(OutputFile);

return true;
}

public static bool IsValidFile()
{
InputFileReader = new StreamReader(InputFile);

while (InputFileReader.Peek() >= 0)
{
if (!IsValidLine(InputFileReader.ReadLine())) return false;
}

return true;
}
public static bool IsValidLine(string line)
{
string[] subStrings = line.Split(",");

if (subStrings.Length != 2)
{
myConsoleText = "Malformed line: Each line must contain the total due and the amount paid separated by a comma (for example: 2.13,3.00)";
Console.WriteLine(myConsoleText);
return false;
}

decimal totalDue;
if (decimal.TryParse(subStrings[0], out totalDue) == false)
{
myConsoleText = "Malformed line: Total Due must be a decimal";
Console.WriteLine(myConsoleText);
return false;
}

decimal amountPaid;
if (decimal.TryParse(subStrings[1], out amountPaid) == false)
{
myConsoleText = "Malformed line: Amount Paid must be a decimal";
Console.WriteLine(myConsoleText);
return false;
}

InputLines.Add(new InputLine(totalDue, amountPaid));

return true;
}
public static void OutputChange()
{
foreach (InputLine currentLine in InputLines)
{
OutputLines.Add(GenerateOutputLine(currentLine));
}

File.WriteAllLines(OutputFile, OutputLines);
}

public static string GenerateOutputLine(InputLine inputLine)
{
int remainingPennies = (int) (100 * (inputLine.AmountPaid - inputLine.TotalDue));

bool isNormal = remainingPennies % 3 != 0;

int hundreds = (isNormal ? remainingPennies/10000 : _random.Next(0, remainingPennies/10000));
remainingPennies -= 10000*hundreds;

int twenties = (isNormal ? remainingPennies/2000 : _random.Next(0, remainingPennies/2000));
remainingPennies -= 2000*twenties;

int fives = (isNormal ? remainingPennies/500 : _random.Next(0, remainingPennies/500));
remainingPennies -= 500*fives;

int dollars = (isNormal ? remainingPennies/100 : _random.Next(0, remainingPennies/100));
remainingPennies -= 100*dollars;

int quarters = (isNormal ? remainingPennies/25 : _random.Next(0, remainingPennies/25));
remainingPennies -= 25*quarters;

int dimes = (isNormal ? remainingPennies/10 : _random.Next(0, remainingPennies/10));
remainingPennies -= 10*dimes;

int nickles = (isNormal ? remainingPennies/5 : _random.Next(0, remainingPennies/5));
remainingPennies -= 5*nickles;

StringBuilder outputLine = new StringBuilder();
if (hundreds > 0)
{
outputLine.Append(hundreds);
outputLine.Append(hundreds > 1 ? " hundreds" : " hundred");
}

if (twenties > 0)
{
if (outputLine.Length > 0) outputLine.Append(",");
outputLine.Append(twenties);
outputLine.Append(twenties > 1 ? " twenties" : " twenty");
}

if (fives > 0)
{
if (outputLine.Length > 0) outputLine.Append(",");
outputLine.Append(fives);
outputLine.Append(fives > 1 ? " fives" : " five");
}

if (dollars > 0)
{
if (outputLine.Length > 0) outputLine.Append(",");
outputLine.Append(dollars);
outputLine.Append(dollars > 1 ? " dollars" : " dollar");
}

if (quarters > 0)
{
if (outputLine.Length > 0) outputLine.Append(",");
outputLine.Append(quarters);
outputLine.Append(quarters > 1 ? " quarters" : " quarter");
}

if (dimes > 0)
{
if (outputLine.Length > 0) outputLine.Append(",");
outputLine.Append(dimes);
outputLine.Append(dimes > 1 ? " dimes" : " dime");
}

if (nickles > 0)
{
if (outputLine.Length > 0) outputLine.Append(",");
outputLine.Append(nickles);
outputLine.Append(nickles > 1 ? " nickles" : " nickle");
}

if (remainingPennies > 0)
{
if (outputLine.Length > 0) outputLine.Append(",");
outputLine.Append(remainingPennies);
outputLine.Append(remainingPennies > 1 ? " pennies" : " penny");
}

return outputLine.ToString();
}


}
}
2 changes: 1 addition & 1 deletion README.md → CashRegister/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Cash Register

The Problem
-----------
Creative Cash Draw Solutions is a client who wants to provide something different for the cashiers who use their system. The function of the application is to tell the cashier how much change is owed and what denominations should be used. In most cases the app should return the minimum amount of physical change, but the client would like to add a twist. If the total due in cents is divisible by 3, the app should randomly generate the change denominations (but the math still needs to be right :))
Creative Cash Draw Solutions is a client who wants to provide something different for the cashiers who use their system. The function of the application is to tell the cashier how much change is owed and what denominations should be used. In most cases the app should return the minimum amount of physical change, but the client would like to add a twist. If the total cost in cents is divisible by 3, the app should randomly generate the change denominations (but the math still needs to be right :))

Please write a program which accomplishes the clients goals. The program should:

Expand Down
Loading