Add feat: finished guided project for debugging

This commit is contained in:
Isaac Alter 2023-07-23 16:00:48 -06:00
parent 9043ee71ca
commit 7cba5db2a8
2 changed files with 14 additions and 22 deletions

View file

@ -18,7 +18,7 @@ expected.
*/ */
string? readResult = null; string? readResult = null;
bool useTestData = true; bool useTestData = false;
Console.Clear(); Console.Clear();
@ -46,7 +46,7 @@ Console.WriteLine($"Expected till value: {registerCheckTillTotal}\n\r");
var valueGenerator = new Random((int)DateTime.Now.Ticks); var valueGenerator = new Random((int)DateTime.Now.Ticks);
int transactions = 10; int transactions = 40;
if (useTestData) if (useTestData)
{ {
@ -56,7 +56,7 @@ if (useTestData)
while (transactions > 0) while (transactions > 0)
{ {
transactions -= 1; transactions -= 1;
int itemCost = valueGenerator.Next(2, 20); int itemCost = valueGenerator.Next(2, 50);
if (useTestData) if (useTestData)
{ {
@ -76,18 +76,17 @@ while (transactions > 0)
Console.WriteLine($"\t Using {paymentFives} five dollar bills"); Console.WriteLine($"\t Using {paymentFives} five dollar bills");
Console.WriteLine($"\t Using {paymentOnes} one dollar bills"); Console.WriteLine($"\t Using {paymentOnes} one dollar bills");
// MakeChange manages the transaction and updates the till try
string transactionMessage = MakeChange(itemCost, cashTill, paymentTwenties, paymentTens, paymentFives, paymentOnes);
// Backup Calculation - each transaction adds current "itemCost" to the till
if (transactionMessage == "transaction succeeded")
{ {
// MakeChange manages the transaction and updates the till
MakeChange(itemCost, cashTill, paymentTwenties, paymentTens, paymentFives, paymentOnes);
Console.WriteLine($"Transaction successfully completed."); Console.WriteLine($"Transaction successfully completed.");
registerCheckTillTotal += itemCost; registerCheckTillTotal += itemCost;
} }
else catch (InvalidCastException e)
{ {
Console.WriteLine($"Transaction unsuccessful: {transactionMessage}"); Console.WriteLine($"Could not complete transaction: {e.Message}");
} }
Console.WriteLine(TillAmountSummary(cashTill)); Console.WriteLine(TillAmountSummary(cashTill));
@ -112,10 +111,8 @@ static void LoadTillEachMorning(int[,] registerDailyStartingCash, int[] cashTill
} }
static string MakeChange(int cost, int[] cashTill, int twenties, int tens = 0, int fives = 0, int ones = 0) static void MakeChange(int cost, int[] cashTill, int twenties, int tens = 0, int fives = 0, int ones = 0)
{ {
string transactionMessage = "";
cashTill[3] += twenties; cashTill[3] += twenties;
cashTill[2] += tens; cashTill[2] += tens;
cashTill[1] += fives; cashTill[1] += fives;
@ -125,7 +122,7 @@ static string MakeChange(int cost, int[] cashTill, int twenties, int tens = 0, i
int changeNeeded = amountPaid - cost; int changeNeeded = amountPaid - cost;
if (changeNeeded < 0) if (changeNeeded < 0)
transactionMessage = "Not enough money provided."; throw new InvalidOperationException("InvalidOperationException: Not enough money provided to complete the transaction.");
Console.WriteLine("Cashier Returns:"); Console.WriteLine("Cashier Returns:");
@ -145,7 +142,7 @@ static string MakeChange(int cost, int[] cashTill, int twenties, int tens = 0, i
while ((changeNeeded > 4) && (cashTill[1] > 0)) while ((changeNeeded > 4) && (cashTill[1] > 0))
{ {
cashTill[2]--; cashTill[1]--;
changeNeeded -= 5; changeNeeded -= 5;
Console.WriteLine("\t A five"); Console.WriteLine("\t A five");
} }
@ -158,12 +155,7 @@ static string MakeChange(int cost, int[] cashTill, int twenties, int tens = 0, i
} }
if (changeNeeded > 0) if (changeNeeded > 0)
transactionMessage = "Can't make change. Do you have anything smaller?"; throw new InvalidOperationException("InvalidOperationException: The till is unable to make the correct change.");
if (transactionMessage == "")
transactionMessage = "transaction succeeded";
return transactionMessage;
} }
static void LogTillStatus(int[] cashTill) static void LogTillStatus(int[] cashTill)

View file

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>