From 7cba5db2a8170fba5d01c356f5c745808af56491 Mon Sep 17 00:00:00 2001 From: Isaac Alter Date: Sun, 23 Jul 2023 16:00:48 -0600 Subject: [PATCH] Add feat: finished guided project for debugging --- .../guided-project/Starter/Program.cs | 34 +++++++------------ .../guided-project/Starter/Starter.csproj | 2 +- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/2-csharp/lesson-6-projects/guided-project/Starter/Program.cs b/2-csharp/lesson-6-projects/guided-project/Starter/Program.cs index 9cc76e1..f5f6fb5 100644 --- a/2-csharp/lesson-6-projects/guided-project/Starter/Program.cs +++ b/2-csharp/lesson-6-projects/guided-project/Starter/Program.cs @@ -18,7 +18,7 @@ expected. */ string? readResult = null; -bool useTestData = true; +bool useTestData = false; Console.Clear(); @@ -46,7 +46,7 @@ Console.WriteLine($"Expected till value: {registerCheckTillTotal}\n\r"); var valueGenerator = new Random((int)DateTime.Now.Ticks); -int transactions = 10; +int transactions = 40; if (useTestData) { @@ -56,7 +56,7 @@ if (useTestData) while (transactions > 0) { transactions -= 1; - int itemCost = valueGenerator.Next(2, 20); + int itemCost = valueGenerator.Next(2, 50); if (useTestData) { @@ -76,18 +76,17 @@ while (transactions > 0) Console.WriteLine($"\t Using {paymentFives} five dollar bills"); Console.WriteLine($"\t Using {paymentOnes} one dollar bills"); - // MakeChange manages the transaction and updates the till - string transactionMessage = MakeChange(itemCost, cashTill, paymentTwenties, paymentTens, paymentFives, paymentOnes); - - // Backup Calculation - each transaction adds current "itemCost" to the till - if (transactionMessage == "transaction succeeded") + try { + // MakeChange manages the transaction and updates the till + MakeChange(itemCost, cashTill, paymentTwenties, paymentTens, paymentFives, paymentOnes); + Console.WriteLine($"Transaction successfully completed."); registerCheckTillTotal += itemCost; } - else + catch (InvalidCastException e) { - Console.WriteLine($"Transaction unsuccessful: {transactionMessage}"); + Console.WriteLine($"Could not complete transaction: {e.Message}"); } 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[2] += tens; cashTill[1] += fives; @@ -125,7 +122,7 @@ static string MakeChange(int cost, int[] cashTill, int twenties, int tens = 0, i int changeNeeded = amountPaid - cost; if (changeNeeded < 0) - transactionMessage = "Not enough money provided."; + throw new InvalidOperationException("InvalidOperationException: Not enough money provided to complete the transaction."); 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)) { - cashTill[2]--; + cashTill[1]--; changeNeeded -= 5; 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) - transactionMessage = "Can't make change. Do you have anything smaller?"; - - if (transactionMessage == "") - transactionMessage = "transaction succeeded"; - - return transactionMessage; + throw new InvalidOperationException("InvalidOperationException: The till is unable to make the correct change."); } static void LogTillStatus(int[] cashTill) diff --git a/2-csharp/lesson-6-projects/guided-project/Starter/Starter.csproj b/2-csharp/lesson-6-projects/guided-project/Starter/Starter.csproj index f02677b..74abf5c 100644 --- a/2-csharp/lesson-6-projects/guided-project/Starter/Starter.csproj +++ b/2-csharp/lesson-6-projects/guided-project/Starter/Starter.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net6.0 enable enable