From 94639f246f39ef840174a36b464ee601a0d9938f Mon Sep 17 00:00:00 2001 From: Michael Lin Date: Tue, 19 Oct 2021 16:41:01 -0400 Subject: [PATCH 1/4] Completed Solution of CashRegister --- .gitignore | 363 ++++++++++++++++++ CashRegister/CashRegister.sln | 31 ++ CashRegister/CashRegister/CashRegister.csproj | 8 + .../CashRegister/ChangeCalculations.cs | 269 +++++++++++++ CashRegister/CashRegister/Coins.cs | 100 +++++ CashRegister/CashRegister/Program.cs | 30 ++ .../Properties/launchSettings.json | 8 + .../CashRegisterTests.cs | 130 +++++++ .../CashRegisterUnitTests.csproj | 26 ++ .../Resources/textinputfile.txt | 17 + CashRegister/README.md | 18 + 11 files changed, 1000 insertions(+) create mode 100644 .gitignore create mode 100644 CashRegister/CashRegister.sln create mode 100644 CashRegister/CashRegister/CashRegister.csproj create mode 100644 CashRegister/CashRegister/ChangeCalculations.cs create mode 100644 CashRegister/CashRegister/Coins.cs create mode 100644 CashRegister/CashRegister/Program.cs create mode 100644 CashRegister/CashRegister/Properties/launchSettings.json create mode 100644 CashRegister/CashRegisterUnitTests/CashRegisterTests.cs create mode 100644 CashRegister/CashRegisterUnitTests/CashRegisterUnitTests.csproj create mode 100644 CashRegister/CashRegisterUnitTests/Resources/textinputfile.txt create mode 100644 CashRegister/README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..9491a2fd --- /dev/null +++ b/.gitignore @@ -0,0 +1,363 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd \ No newline at end of file diff --git a/CashRegister/CashRegister.sln b/CashRegister/CashRegister.sln new file mode 100644 index 00000000..7153ce7e --- /dev/null +++ b/CashRegister/CashRegister.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31005.135 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CashRegister", "CashRegister\CashRegister.csproj", "{8E37087E-1493-46E5-8257-A3C4532A3629}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CashRegisterUnitTests", "CashRegisterUnitTests\CashRegisterUnitTests.csproj", "{0E47EE61-5CCA-45E2-A3E6-7D2E008D1989}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8E37087E-1493-46E5-8257-A3C4532A3629}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8E37087E-1493-46E5-8257-A3C4532A3629}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8E37087E-1493-46E5-8257-A3C4532A3629}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8E37087E-1493-46E5-8257-A3C4532A3629}.Release|Any CPU.Build.0 = Release|Any CPU + {0E47EE61-5CCA-45E2-A3E6-7D2E008D1989}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0E47EE61-5CCA-45E2-A3E6-7D2E008D1989}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0E47EE61-5CCA-45E2-A3E6-7D2E008D1989}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0E47EE61-5CCA-45E2-A3E6-7D2E008D1989}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C06BC6D0-B2BC-4938-9E94-AC49016C0DA0} + EndGlobalSection +EndGlobal diff --git a/CashRegister/CashRegister/CashRegister.csproj b/CashRegister/CashRegister/CashRegister.csproj new file mode 100644 index 00000000..9acff6d2 --- /dev/null +++ b/CashRegister/CashRegister/CashRegister.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/CashRegister/CashRegister/ChangeCalculations.cs b/CashRegister/CashRegister/ChangeCalculations.cs new file mode 100644 index 00000000..fb6ea62c --- /dev/null +++ b/CashRegister/CashRegister/ChangeCalculations.cs @@ -0,0 +1,269 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace CashRegister +{ + public enum AbovePrice + { + Hundred = 1, + Fifty, + Twenty, + Ten, + Five, + Dollar, + Quarter, + Dime, + Nickel, + Penny + } + public class ChangeCalculations + { + /// + /// Reads a flat file and separate each entry with a comma and stores each value in its own list + /// + /// flat file + /// 1st input in the line + /// 2nd input in the line + public void Splittingvalues(string textFile, List costValue, List paidValue) + { + try + { + string line; + string[] splitLine; + + StreamReader inputFile = new StreamReader(textFile); + line = inputFile.ReadLine(); + while (line != null) + { + if (line != "") + { + splitLine = line.Split(","); + costValue.Add(splitLine[0]); + paidValue.Add(splitLine[1]); + + } + line = inputFile.ReadLine(); + } + + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + + } + /// + /// Checks whether or not if the costvalue is divisible by 3 else do normal calculations + /// + /// + /// + public void ChangeofCost(List costValue, List paidValue, List change) + { + for (int i = 0; i < paidValue.Count; i++) + { + if (double.Parse(costValue[i]) * 100 % 3 == 0) + { + change.Add(RandomizeNumberofCoins(paidValue[i], costValue[i])); + } + else + { + change.Add(CalculatingNumberofCoins(paidValue[i], costValue[i])); + } + + } + + } + /// + /// Randomize the amount of coins received + /// + /// the 2nd input of the line + /// the 1st input of the line + /// the concatenate string of change given + public string RandomizeNumberofCoins(string paid, string cost) + { + decimal difference = Math.Round(decimal.Parse(paid) - decimal.Parse(cost), 2); + if (difference < 0) + { + return "Not enough funds to give change\n"; + } + Coins coins = new Coins(); + + while (difference != 0) + { + difference = SubtractionofRandomization(RandomizationCalculation(difference), coins, difference); + + } + return coins.ChangeGiven(); + } + /// + /// Randomly subtract the change from dollar or coin value + /// + /// random number that was generated between 1-10 + /// object containing the amount of coins + /// the amount of change + /// the leftover amount of change + public decimal SubtractionofRandomization(int randomNumber, Coins coins, decimal change) + { + switch (randomNumber) + { + case (int)AbovePrice.Hundred: + { + coins.HundredBills++; + return change -= Coins.HundredValue; + } + case (int)AbovePrice.Fifty: + { + coins.FiftyBills++; + return change -= Coins.FiftyValue; + } + case (int)AbovePrice.Twenty: + { + coins.TwentyBills++; + return change -= Coins.TwentyValue; + } + case (int)AbovePrice.Ten: + { + coins.TenBills++; + return change -= Coins.TenValue; + } + case (int)AbovePrice.Five: + { + coins.FiveBills++; + return change -= Coins.FiveValue; + } + case (int)AbovePrice.Dollar: + { + coins.DollarBills++; + return change -= Coins.DollarValue; + } + case (int)AbovePrice.Quarter: + { + coins.Quarters++; + return change -= Coins.QuarterValue; + } + case (int)AbovePrice.Dime: + { + coins.Dimes++; + return change -= Coins.DimeValue; + } + case (int)AbovePrice.Nickel: + { + coins.Nickels++; + return change -= Coins.NickelValue; + } + case (int)AbovePrice.Penny: + { + coins.Pennies++; + return change -= Coins.PennyValue; + } + } + return 0; + } + + /// + /// The minimum amount of change owed + /// + /// 2nd input of the flat file line + /// 1st input of the flat file line + /// the concatenate string of change given + public string CalculatingNumberofCoins(string paid, string cost) + { + Coins coins = new Coins(); + + decimal difference = Math.Round(decimal.Parse(paid) - decimal.Parse(cost), 2); + + if (difference < 0) + { + return "Not enough funds to give change\n"; + } + coins.HundredBills = (int)(difference / Coins.HundredValue); + difference %= Coins.HundredValue; + + coins.FiftyBills = (int)(difference / Coins.FiftyValue); + difference %= Coins.FiftyValue; + + coins.TwentyBills = (int)(difference / Coins.TwentyValue); + difference %= Coins.TwentyValue; + + coins.TenBills = (int)(difference / Coins.TenValue); + difference %= Coins.TenValue; + + coins.FiveBills = (int)(difference / Coins.FiveValue); + difference %= Coins.FiveValue; + + coins.DollarBills = (int)(difference / Coins.DollarValue); + difference %= Coins.DollarValue; + + coins.Quarters = (int)(difference / Coins.QuarterValue); + difference %= Coins.QuarterValue; + + coins.Dimes = (int)(difference / Coins.DimeValue); + difference %= Coins.DimeValue; + + coins.Nickels = (int)(difference / Coins.NickelValue); + difference %= Coins.NickelValue; + + coins.Pennies = (int)(difference / Coins.PennyValue); + + return coins.ChangeGiven(); + } + /// + /// Randomly grabs a number from 1-10 depending if the change is greater than the value + /// + /// total amount of change + /// a random number from 1-10 + public int RandomizationCalculation(decimal change) + { + Random rnd = new Random(); + + switch (change) + { + case decimal n when (n >= Coins.HundredValue): + { + return rnd.Next(1, 11); + } + case decimal n when (n >= Coins.FiftyValue): + { + return rnd.Next(2, 11); + } + case decimal n when (n >= Coins.TwentyValue): + { + return rnd.Next(3, 11); + } + case decimal n when (n >= Coins.TenValue): + { + return rnd.Next(4, 11); + } + case decimal n when (n >= Coins.FiveValue): + { + return rnd.Next(5, 11); + } + case decimal n when (n >= Coins.DollarValue): + { + return rnd.Next(6, 11); + } + case decimal n when (n >= Coins.QuarterValue): + { + return rnd.Next(7, 11); + } + case decimal n when (n >= Coins.DimeValue): + { + return rnd.Next(8, 11); + } + case decimal n when (n >= Coins.NickelValue): + { + return rnd.Next(9, 11); + } + case decimal n when (n >= Coins.PennyValue): + { + return rnd.Next(10, 11); + } + default: + { + return 0; + } + } + } + } +} diff --git a/CashRegister/CashRegister/Coins.cs b/CashRegister/CashRegister/Coins.cs new file mode 100644 index 00000000..5321318f --- /dev/null +++ b/CashRegister/CashRegister/Coins.cs @@ -0,0 +1,100 @@ +using System; + +namespace CashRegister +{ + public class Coins + { + public const decimal HundredValue = 100; + public const decimal FiftyValue = 50; + public const decimal TwentyValue = 20; + public const decimal TenValue = 10; + public const decimal FiveValue = 5; + public const decimal DollarValue = 1; + public const decimal QuarterValue = 0.25m; + public const decimal DimeValue = 0.10m; + public const decimal NickelValue = 0.05m; + public const decimal PennyValue = 0.01m; + + public int HundredBills { get; set; } + public int FiftyBills { get; set; } + public int TwentyBills { get; set; } + public int TenBills { get; set; } + public int FiveBills { get; set; } + public int DollarBills { get; set; } + public int Quarters { get; set; } + public int Dimes { get; set; } + public int Nickels { get; set; } + public int Pennies { get; set; } + /// + /// Checks the summation if any change is not necessary + /// + /// Adds all the integers together + /// the total added + public int SumofTotal + { + get { + return HundredBills + FiftyBills + TwentyBills + TenBills + FiveBills + DollarBills + + Quarters + Dimes + Nickels + Pennies; + } + } + + /// + /// Writing out how many dollars and/or coins is needed + /// + /// Coins object containing all the information + /// a string that has been concatenation + public string ChangeGiven() + { + String changeGiven = string.Empty; + + if (HundredBills > 0) + { + changeGiven += HundredBills == 1 ? HundredBills + " hundred dollar," : DollarBills + " hundred dollars,"; + } + if (FiftyBills > 0) + { + changeGiven += FiftyBills == 1 ? FiftyBills + " fifty dollar," : FiftyBills + " fifty dollars,"; + } + if (TwentyBills > 0) + { + changeGiven += TwentyBills == 1 ? TwentyBills + " twent dollar," : TwentyBills + " twenty dollars,"; + } + if (TenBills > 0) + { + changeGiven += TenBills == 1 ? TenBills + " ten dollar," : TenBills + " ten dollars,"; + } + if (FiveBills > 0) + { + changeGiven += FiveBills == 1 ? FiveBills + " five dollar," : FiveBills + " five dollars,"; + } + if (DollarBills > 0) + { + changeGiven += DollarBills == 1 ? DollarBills + " dollar," : DollarBills + " dollars,"; + } + if (Quarters > 0) + { + changeGiven += Quarters == 1 ? Quarters + " quarter," : Quarters + " quarters,"; + } + if (Dimes > 0) + { + changeGiven += Dimes == 1 ? Dimes + " dime," : Dimes + " dimes,"; + } + if (Nickels > 0) + { + changeGiven += Nickels == 1 ? Nickels + " nickel," : Nickels + " nickles,"; + } + if (Pennies > 0) + { + changeGiven += Pennies == 1 ? Pennies + " penny," : Pennies + " pennies,"; + } + + if (SumofTotal== 0) + { + changeGiven += "No change needed,"; + } + changeGiven = changeGiven.Remove(changeGiven.Length - 1); + changeGiven += "\n"; + return changeGiven; + } + } +} diff --git a/CashRegister/CashRegister/Program.cs b/CashRegister/CashRegister/Program.cs new file mode 100644 index 00000000..847f634d --- /dev/null +++ b/CashRegister/CashRegister/Program.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +namespace CashRegister +{ + class Program + { + static void Main(string[] args) + { + List costValue = new List(); + List paidValue = new List(); + List change = new List(); + //Accepting flat file as input + ChangeCalculations utilities = new ChangeCalculations(); + utilities.Splittingvalues(args[0], costValue, paidValue); + + //Output the change the cashier should return + utilities.ChangeofCost(costValue, paidValue,change); + + foreach (string changeNeeded in change) + { + Console.WriteLine(changeNeeded); + } + + + Console.WriteLine("Press any key to stop..."); + Console.ReadKey(); + } + + } +} diff --git a/CashRegister/CashRegister/Properties/launchSettings.json b/CashRegister/CashRegister/Properties/launchSettings.json new file mode 100644 index 00000000..97a19e94 --- /dev/null +++ b/CashRegister/CashRegister/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "CashRegister": { + "commandName": "Project", + "commandLineArgs": "\"C:\\Coding Exams\\CashRegister\\CashRegisterUnitTests\\Resources\\textinputfile.txt\"" + } + } +} \ No newline at end of file diff --git a/CashRegister/CashRegisterUnitTests/CashRegisterTests.cs b/CashRegister/CashRegisterUnitTests/CashRegisterTests.cs new file mode 100644 index 00000000..bf8a0ac9 --- /dev/null +++ b/CashRegister/CashRegisterUnitTests/CashRegisterTests.cs @@ -0,0 +1,130 @@ +using NUnit.Framework; +using CashRegister; +using System.Collections.Generic; +using System; +using System.IO; + +namespace CashRegisterUnitTests +{ + public class CashRegisterTests + { + /// + /// Reading multiple inputs from a text file + /// + [Test] + public void Reading_Multiple_Lines() + { + // Arrange + var utilities = new ChangeCalculations(); + List costValue = new List(); + List paidValue = new List(); + // Act + utilities.Splittingvalues("Resources\\textinputfile.txt", costValue, paidValue); + + //Assert + if((costValue.Count==paidValue.Count)&&(costValue.Count > 2) && (paidValue.Count > 2)) + { + Assert.Pass(); + } + + Assert.Fail(); + } + + /// + /// checks to see randomization does occur + /// + [Test] + public void Randomize_Change_Value() + { + // Arrange + var utilities = new ChangeCalculations(); + string paid = "3.50"; + string cost = "2.10"; + + // Act + var test1 = utilities.RandomizeNumberofCoins(paid, cost); + var test2 = utilities.RandomizeNumberofCoins(paid, cost); + + //Assert + Assert.AreNotEqual(test1, test2); + } + + /// + /// Minimum amount of demonination is needed + /// + [Test] + public void Exact_Change_Value() + { + // Arrange + var utilities = new ChangeCalculations(); + string paid = "3.50"; + string cost = "2.10"; + + // Act + var test1 = utilities.CalculatingNumberofCoins(paid, cost); + var test2 = "1 dollar,1 quarter,1 dime,1 nickel\n"; + + //Assert + Assert.AreEqual(test1, test2); + } + + /// + /// If paid was less than cost + /// + [Test] + public void Paid_LessThan_Cost() + { + // Arrange + var utilities = new ChangeCalculations(); + string paid = "1.50"; + string cost = "2.10"; + + // Act + var test = utilities.CalculatingNumberofCoins(paid, cost); + //Assert + Assert.AreEqual(test, "Not enough funds to give change\n"); + } + /// + /// randomization of paid was less than cost + /// + [Test] + public void Randomize_Paid_LessThan_Cost() + { + // Arrange + var utilities = new ChangeCalculations(); + string paid = "1.50"; + string cost = "2.10"; + + // Act + var test = utilities.RandomizeNumberofCoins(paid, cost); + //Assert + Assert.AreEqual(test, "Not enough funds to give change\n"); + } + + /// + /// cost divisible by 3 + /// + [Test] + public void Divisibleby3() + { + // Arrange + var utilities = new ChangeCalculations(); + + List paid = new List(); + List cost = new List(); + List change = new List(); + paid.Add("3.00"); + cost.Add("2.10"); + + + + // Act + utilities.ChangeofCost(cost, paid, change); + + //Assert + Assert.AreNotEqual(change[0], "3 quarters,1 dime,1 nickel\n"); + + } + + } +} \ No newline at end of file diff --git a/CashRegister/CashRegisterUnitTests/CashRegisterUnitTests.csproj b/CashRegister/CashRegisterUnitTests/CashRegisterUnitTests.csproj new file mode 100644 index 00000000..5154b34e --- /dev/null +++ b/CashRegister/CashRegisterUnitTests/CashRegisterUnitTests.csproj @@ -0,0 +1,26 @@ + + + + net5.0 + + false + + + + + + + + + + + + + + + + Always + + + + diff --git a/CashRegister/CashRegisterUnitTests/Resources/textinputfile.txt b/CashRegister/CashRegisterUnitTests/Resources/textinputfile.txt new file mode 100644 index 00000000..a42cac8d --- /dev/null +++ b/CashRegister/CashRegisterUnitTests/Resources/textinputfile.txt @@ -0,0 +1,17 @@ +2.12,3.00 + +1.97,2.00 + +3.33,5.00 + +1.50,2.00 + +1.45,2.00 + +3.00,6.00 + +1.50,1.50 + +1.40,1.20 + +30.50,100.20 \ No newline at end of file diff --git a/CashRegister/README.md b/CashRegister/README.md new file mode 100644 index 00000000..2fbcc59a --- /dev/null +++ b/CashRegister/README.md @@ -0,0 +1,18 @@ +Cash Register +============ + +possible limitations on input when greater over 999.00. I added 5, 10,20,50,100 bills, wasn't sure if it was necessary. + +if input is 1,000.00,1,200.00 there will be issues when separated by comma. + +making sure the 1st input is less than the 2nd input. +I added an error check just in case if a scenario happened. + +Drag a text file with the sample input to the executable will run the program or modify the launchsettings.json + +In the UnitTests Project Resources folder is the textfile I used to sample my input. + +I had fun with this coding test. + +Thank you, +Michael \ No newline at end of file From 2dc55357f670521a1cbd96bb675e0b1d9a952d07 Mon Sep 17 00:00:00 2001 From: Michael Lin Date: Wed, 20 Oct 2021 00:54:26 -0400 Subject: [PATCH 2/4] Implemented recursion on the randomization method --- .../CashRegister/ChangeCalculations.cs | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/CashRegister/CashRegister/ChangeCalculations.cs b/CashRegister/CashRegister/ChangeCalculations.cs index fb6ea62c..8c2a1da7 100644 --- a/CashRegister/CashRegister/ChangeCalculations.cs +++ b/CashRegister/CashRegister/ChangeCalculations.cs @@ -89,11 +89,8 @@ public string RandomizeNumberofCoins(string paid, string cost) } Coins coins = new Coins(); - while (difference != 0) - { - difference = SubtractionofRandomization(RandomizationCalculation(difference), coins, difference); + SubtractionofRandomization(RandomizationCalculation(difference), coins, difference); - } return coins.ChangeGiven(); } /// @@ -110,52 +107,52 @@ public decimal SubtractionofRandomization(int randomNumber, Coins coins, decimal case (int)AbovePrice.Hundred: { coins.HundredBills++; - return change -= Coins.HundredValue; + return SubtractionofRandomization(RandomizationCalculation(change -= Coins.HundredValue),coins,change); } case (int)AbovePrice.Fifty: { coins.FiftyBills++; - return change -= Coins.FiftyValue; + return SubtractionofRandomization(RandomizationCalculation(change -= Coins.FiftyValue), coins, change); } case (int)AbovePrice.Twenty: { coins.TwentyBills++; - return change -= Coins.TwentyValue; + return SubtractionofRandomization(RandomizationCalculation(change -= Coins.TwentyValue), coins, change); } case (int)AbovePrice.Ten: { coins.TenBills++; - return change -= Coins.TenValue; + return SubtractionofRandomization(RandomizationCalculation(change -= Coins.TenValue), coins, change); } case (int)AbovePrice.Five: { coins.FiveBills++; - return change -= Coins.FiveValue; + return SubtractionofRandomization(RandomizationCalculation(change -= Coins.FiveValue), coins, change); } case (int)AbovePrice.Dollar: { coins.DollarBills++; - return change -= Coins.DollarValue; + return SubtractionofRandomization(RandomizationCalculation(change -= Coins.DollarValue), coins, change); } case (int)AbovePrice.Quarter: { coins.Quarters++; - return change -= Coins.QuarterValue; + return SubtractionofRandomization(RandomizationCalculation(change -= Coins.QuarterValue), coins, change); } case (int)AbovePrice.Dime: { coins.Dimes++; - return change -= Coins.DimeValue; + return SubtractionofRandomization(RandomizationCalculation(change -= Coins.DimeValue), coins, change); } case (int)AbovePrice.Nickel: { coins.Nickels++; - return change -= Coins.NickelValue; + return SubtractionofRandomization(RandomizationCalculation(change -= Coins.NickelValue), coins, change); } case (int)AbovePrice.Penny: { coins.Pennies++; - return change -= Coins.PennyValue; + return SubtractionofRandomization(RandomizationCalculation(change -= Coins.PennyValue), coins, change); } } return 0; From 099487d4cd9a62c5000134fff8dfb9cd9c9578cb Mon Sep 17 00:00:00 2001 From: Michael Lin Date: Wed, 20 Oct 2021 01:26:12 -0400 Subject: [PATCH 3/4] Forgot to writeout a file for the receipt. Originally kept it in the console window --- .../CashRegister/ChangeCalculations.cs | 11 ++++++ CashRegister/CashRegister/Program.cs | 8 ++--- .../CashRegisterTests.cs | 36 +++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/CashRegister/CashRegister/ChangeCalculations.cs b/CashRegister/CashRegister/ChangeCalculations.cs index 8c2a1da7..3dfb2240 100644 --- a/CashRegister/CashRegister/ChangeCalculations.cs +++ b/CashRegister/CashRegister/ChangeCalculations.cs @@ -262,5 +262,16 @@ public int RandomizationCalculation(decimal change) } } } + + public void Receiptfile(List changeNeeded) + { + using(StreamWriter writer = new StreamWriter("Resources\\ReceiptFile.txt")) + { + foreach(string change in changeNeeded) + { + writer.WriteLine(change); + } + } + } } } diff --git a/CashRegister/CashRegister/Program.cs b/CashRegister/CashRegister/Program.cs index 847f634d..6a32c0dd 100644 --- a/CashRegister/CashRegister/Program.cs +++ b/CashRegister/CashRegister/Program.cs @@ -13,13 +13,11 @@ static void Main(string[] args) ChangeCalculations utilities = new ChangeCalculations(); utilities.Splittingvalues(args[0], costValue, paidValue); - //Output the change the cashier should return + //change the cashier should return utilities.ChangeofCost(costValue, paidValue,change); - foreach (string changeNeeded in change) - { - Console.WriteLine(changeNeeded); - } + //Write text output for change needed + utilities.Receiptfile(change); Console.WriteLine("Press any key to stop..."); diff --git a/CashRegister/CashRegisterUnitTests/CashRegisterTests.cs b/CashRegister/CashRegisterUnitTests/CashRegisterTests.cs index bf8a0ac9..534d6864 100644 --- a/CashRegister/CashRegisterUnitTests/CashRegisterTests.cs +++ b/CashRegister/CashRegisterUnitTests/CashRegisterTests.cs @@ -126,5 +126,41 @@ public void Divisibleby3() } + /// + /// Writing out text file + /// + [Test] + public void WriteOutTextFile() + { + // Arrange + var utilities = new ChangeCalculations(); + + List paid = new List(); + List cost = new List(); + List change = new List(); + List line = new List(); + List testLine = new List(); + paid.Add("3.00"); + cost.Add("2.20"); + + + + // Act + utilities.ChangeofCost(cost, paid, change); + utilities.Receiptfile(change); + foreach(string changeNeeded in change) + { + testLine.Add(changeNeeded.Remove(changeNeeded.Length - 1)); + } + + StreamReader inputFile = new StreamReader("Resources\\ReceiptFile.txt"); + + line.Add(inputFile.ReadLine()); + + //Assert + Assert.AreEqual(testLine, line); + + } + } } \ No newline at end of file From 787869192d8a7ade080bd2813c16d05ce263a774 Mon Sep 17 00:00:00 2001 From: Michael Lin Date: Wed, 20 Oct 2021 05:22:33 -0400 Subject: [PATCH 4/4] Added check to create folder if the directory didn't exist --- CashRegister/CashRegister/ChangeCalculations.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CashRegister/CashRegister/ChangeCalculations.cs b/CashRegister/CashRegister/ChangeCalculations.cs index 3dfb2240..a222bfd3 100644 --- a/CashRegister/CashRegister/ChangeCalculations.cs +++ b/CashRegister/CashRegister/ChangeCalculations.cs @@ -265,6 +265,12 @@ public int RandomizationCalculation(decimal change) public void Receiptfile(List changeNeeded) { + string folderName = "Resources"; + if(!Directory.Exists(folderName)) + { + Directory.CreateDirectory(folderName); + } + using(StreamWriter writer = new StreamWriter("Resources\\ReceiptFile.txt")) { foreach(string change in changeNeeded)