mirror of
https://github.com/dotnet/intro-to-dotnet-web-dev.git
synced 2026-04-06 15:04:10 +00:00
Add links to C# content and project code (#37)
* started adding C# content * begin adding csharp project code * add csharp project code * added codespaces instructions * add license files * add license files * add csharp project 6
This commit is contained in:
parent
c65d9a369c
commit
66100c7304
59 changed files with 3811 additions and 4 deletions
|
|
@ -0,0 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
182
2-csharp/lesson-6-projects/challenge-project/Final/Program.cs
Normal file
182
2-csharp/lesson-6-projects/challenge-project/Final/Program.cs
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
/*
|
||||
This application manages transactions at a store check-out line. The
|
||||
check-out line has a cash register, and the register has a cash till
|
||||
that is prepared with a number of bills each morning. The till includes
|
||||
bills of four denominations: $1, $5, $10, and $20. The till is used
|
||||
to provide the customer with change during the transaction. The item
|
||||
cost is a randomly generated number between 2 and 49. The customer
|
||||
offers payment based on an algorithm that determines a number of bills
|
||||
in each denomination.
|
||||
|
||||
Each day, the cash till is loaded at the start of the day. As transactions
|
||||
occur, the cash till is managed in a method named MakeChange (customer
|
||||
payments go in and the change returned to the customer comes out). A
|
||||
separate "safety check" calculation that's used to verify the amount of
|
||||
money in the till is performed in the "main program". This safety check
|
||||
is used to ensure that logic in the MakeChange method is working as
|
||||
expected.
|
||||
*/
|
||||
|
||||
string? readResult = null;
|
||||
bool useTestData = false;
|
||||
|
||||
Console.Clear();
|
||||
|
||||
int[] cashTill = new int[] { 0, 0, 0, 0 };
|
||||
int registerCheckTillTotal = 0;
|
||||
|
||||
// registerDailyStartingCash: $1 x 50, $5 x 20, $10 x 10, $20 x 5 => ($350 total)
|
||||
int[,] registerDailyStartingCash = new int[,] { { 1, 50 }, { 5, 20 }, { 10, 10 }, { 20, 5 } };
|
||||
|
||||
int[] testData = new int[] { 6, 10, 17, 20, 31, 36, 40, 41 };
|
||||
int testCounter = 0;
|
||||
|
||||
LoadTillEachMorning(registerDailyStartingCash, cashTill);
|
||||
|
||||
registerCheckTillTotal = registerDailyStartingCash[0, 0] * registerDailyStartingCash[0, 1] + registerDailyStartingCash[1, 0] * registerDailyStartingCash[1, 1] + registerDailyStartingCash[2, 0] * registerDailyStartingCash[2, 1] + registerDailyStartingCash[3, 0] * registerDailyStartingCash[3, 1];
|
||||
|
||||
// display the number of bills of each denomination currently in the till
|
||||
LogTillStatus(cashTill);
|
||||
|
||||
// display a message showing the amount of cash in the till
|
||||
Console.WriteLine(TillAmountSummary(cashTill));
|
||||
|
||||
// display the expected registerDailyStartingCash total
|
||||
Console.WriteLine($"Expected till value: {registerCheckTillTotal}");
|
||||
Console.WriteLine();
|
||||
|
||||
var valueGenerator = new Random((int)DateTime.Now.Ticks);
|
||||
|
||||
int transactions = 100;
|
||||
|
||||
if (useTestData)
|
||||
{
|
||||
transactions = testData.Length;
|
||||
}
|
||||
|
||||
while (transactions > 0)
|
||||
{
|
||||
transactions -= 1;
|
||||
int itemCost = valueGenerator.Next(2, 50);
|
||||
|
||||
if (useTestData)
|
||||
{
|
||||
itemCost = testData[testCounter];
|
||||
testCounter += 1;
|
||||
}
|
||||
|
||||
int paymentOnes = itemCost % 2; // value is 1 when itemCost is odd, value is 0 when itemCost is even
|
||||
int paymentFives = (itemCost % 10 > 7) ? 1 : 0; // value is 1 when itemCost ends with 8 or 9, otherwise value is 0
|
||||
int paymentTens = (itemCost % 20 > 13) ? 1 : 0; // value is 1 when 13 < itemCost < 20 OR 33 < itemCost < 40, otherwise value is 0
|
||||
int paymentTwenties = (itemCost < 20) ? 1 : 2; // value is 1 when itemCost < 20, otherwise value is 2
|
||||
|
||||
// display messages describing the current transaction
|
||||
Console.WriteLine($"Customer is making a ${itemCost} purchase");
|
||||
Console.WriteLine($"\t Using {paymentTwenties} twenty dollar bills");
|
||||
Console.WriteLine($"\t Using {paymentTens} ten dollar bills");
|
||||
Console.WriteLine($"\t Using {paymentFives} five dollar bills");
|
||||
Console.WriteLine($"\t Using {paymentOnes} one dollar bills");
|
||||
|
||||
try
|
||||
{
|
||||
// MakeChange manages the transaction and updates the till
|
||||
MakeChange(itemCost, cashTill, paymentTwenties, paymentTens, paymentFives, paymentOnes);
|
||||
|
||||
// Backup Calculation - each transaction adds current "itemCost" to the till
|
||||
registerCheckTillTotal += itemCost;
|
||||
}
|
||||
catch (InvalidOperationException e)
|
||||
{
|
||||
Console.WriteLine($"Could not complete transaction: {e.Message}");
|
||||
}
|
||||
|
||||
Console.WriteLine(TillAmountSummary(cashTill));
|
||||
Console.WriteLine($"Expected till value: {registerCheckTillTotal}");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
Console.WriteLine("Press the Enter key to exit");
|
||||
do
|
||||
{
|
||||
readResult = Console.ReadLine();
|
||||
|
||||
} while (readResult == null);
|
||||
|
||||
|
||||
static void LoadTillEachMorning(int[,] registerDailyStartingCash, int[] cashTill)
|
||||
{
|
||||
cashTill[0] = registerDailyStartingCash[0, 1];
|
||||
cashTill[1] = registerDailyStartingCash[1, 1];
|
||||
cashTill[2] = registerDailyStartingCash[2, 1];
|
||||
cashTill[3] = registerDailyStartingCash[3, 1];
|
||||
}
|
||||
|
||||
|
||||
static void MakeChange(int cost, int[] cashTill, int twenties, int tens = 0, int fives = 0, int ones = 0)
|
||||
{
|
||||
int availableTwenties = cashTill[3] + twenties;
|
||||
int availableTens = cashTill[2] + tens;
|
||||
int availableFives = cashTill[1] + fives;
|
||||
int availableOnes = cashTill[0] + ones;
|
||||
|
||||
int amountPaid = twenties * 20 + tens * 10 + fives * 5 + ones;
|
||||
int changeNeeded = amountPaid - cost;
|
||||
|
||||
if (changeNeeded < 0)
|
||||
throw new InvalidOperationException("InvalidOperationException: Not enough money provided to complete the transaction.");
|
||||
|
||||
Console.WriteLine("Cashier prepares the following change:");
|
||||
|
||||
while ((changeNeeded > 19) && (availableTwenties > 0))
|
||||
{
|
||||
availableTwenties--;
|
||||
changeNeeded -= 20;
|
||||
Console.WriteLine("\t A twenty");
|
||||
}
|
||||
|
||||
while ((changeNeeded > 9) && (availableTens > 0))
|
||||
{
|
||||
availableTens--;
|
||||
changeNeeded -= 10;
|
||||
Console.WriteLine("\t A ten");
|
||||
}
|
||||
|
||||
while ((changeNeeded > 4) && (availableFives > 0))
|
||||
{
|
||||
availableFives--;
|
||||
changeNeeded -= 5;
|
||||
Console.WriteLine("\t A five");
|
||||
}
|
||||
|
||||
while ((changeNeeded > 0) && (availableOnes > 0))
|
||||
{
|
||||
availableOnes--;
|
||||
changeNeeded -= 1;
|
||||
Console.WriteLine("\t A one");
|
||||
}
|
||||
|
||||
if (changeNeeded > 0)
|
||||
throw new InvalidOperationException("InvalidOperationException: The till is unable to make change for the cash provided.");
|
||||
|
||||
cashTill[0] = availableOnes;
|
||||
cashTill[1] = availableFives;
|
||||
cashTill[2] = availableTens;
|
||||
cashTill[3] = availableTwenties;
|
||||
|
||||
}
|
||||
|
||||
static void LogTillStatus(int[] cashTill)
|
||||
{
|
||||
Console.WriteLine("The till currently has:");
|
||||
Console.WriteLine($"{cashTill[3] * 20} in twenties");
|
||||
Console.WriteLine($"{cashTill[2] * 10} in tens");
|
||||
Console.WriteLine($"{cashTill[1] * 5} in fives");
|
||||
Console.WriteLine($"{cashTill[0]} in ones");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
static string TillAmountSummary(int[] cashTill)
|
||||
{
|
||||
return $"The till has {cashTill[3] * 20 + cashTill[2] * 10 + cashTill[1] * 5 + cashTill[0]} dollars";
|
||||
|
||||
}
|
||||
21
2-csharp/lesson-6-projects/challenge-project/LICENSE
Normal file
21
2-csharp/lesson-6-projects/challenge-project/LICENSE
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2023 Microsoft Learning
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
2
2-csharp/lesson-6-projects/challenge-project/README.md
Normal file
2
2-csharp/lesson-6-projects/challenge-project/README.md
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# Challenge-project-debugging-CSharp
|
||||
Starter and Solution code for the Challenge project: "Debug a C# console application using Visual Studio Code" from the Microsoft Learn collection "Getting started with C#"
|
||||
178
2-csharp/lesson-6-projects/challenge-project/Starter/Program.cs
Normal file
178
2-csharp/lesson-6-projects/challenge-project/Starter/Program.cs
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
/*
|
||||
This application manages transactions at a store check-out line. The
|
||||
check-out line has a cash register, and the register has a cash till
|
||||
that is prepared with a number of bills each morning. The till includes
|
||||
bills of four denominations: $1, $5, $10, and $20. The till is used
|
||||
to provide the customer with change during the transaction. The item
|
||||
cost is a randomly generated number between 2 and 49. The customer
|
||||
offers payment based on an algorithm that determines a number of bills
|
||||
in each denomination.
|
||||
|
||||
Each day, the cash till is loaded at the start of the day. As transactions
|
||||
occur, the cash till is managed in a method named MakeChange (customer
|
||||
payments go in and the change returned to the customer comes out). A
|
||||
separate "safety check" calculation that's used to verify the amount of
|
||||
money in the till is performed in the "main program". This safety check
|
||||
is used to ensure that logic in the MakeChange method is working as
|
||||
expected.
|
||||
*/
|
||||
|
||||
|
||||
string? readResult = null;
|
||||
bool useTestData = false;
|
||||
|
||||
Console.Clear();
|
||||
|
||||
int[] cashTill = new int[] { 0, 0, 0, 0 };
|
||||
int registerCheckTillTotal = 0;
|
||||
|
||||
// registerDailyStartingCash: $1 x 50, $5 x 20, $10 x 10, $20 x 5 => ($350 total)
|
||||
int[,] registerDailyStartingCash = new int[,] { { 1, 50 }, { 5, 20 }, { 10, 10 }, { 20, 5 } };
|
||||
|
||||
int[] testData = new int[] { 6, 10, 17, 20, 31, 36, 40, 41 };
|
||||
int testCounter = 0;
|
||||
|
||||
LoadTillEachMorning(registerDailyStartingCash, cashTill);
|
||||
|
||||
registerCheckTillTotal = registerDailyStartingCash[0, 0] * registerDailyStartingCash[0, 1] + registerDailyStartingCash[1, 0] * registerDailyStartingCash[1, 1] + registerDailyStartingCash[2, 0] * registerDailyStartingCash[2, 1] + registerDailyStartingCash[3, 0] * registerDailyStartingCash[3, 1];
|
||||
|
||||
// display the number of bills of each denomination currently in the till
|
||||
LogTillStatus(cashTill);
|
||||
|
||||
// display a message showing the amount of cash in the till
|
||||
Console.WriteLine(TillAmountSummary(cashTill));
|
||||
|
||||
// display the expected registerDailyStartingCash total
|
||||
Console.WriteLine($"Expected till value: {registerCheckTillTotal}");
|
||||
Console.WriteLine();
|
||||
|
||||
var valueGenerator = new Random((int)DateTime.Now.Ticks);
|
||||
|
||||
int transactions = 100;
|
||||
|
||||
if (useTestData)
|
||||
{
|
||||
transactions = testData.Length;
|
||||
}
|
||||
|
||||
while (transactions > 0)
|
||||
{
|
||||
transactions -= 1;
|
||||
int itemCost = valueGenerator.Next(2, 50);
|
||||
|
||||
if (useTestData)
|
||||
{
|
||||
itemCost = testData[testCounter];
|
||||
testCounter += 1;
|
||||
}
|
||||
|
||||
int paymentOnes = itemCost % 2; // value is 1 when itemCost is odd, value is 0 when itemCost is even
|
||||
int paymentFives = (itemCost % 10 > 7) ? 1 : 0; // value is 1 when itemCost ends with 8 or 9, otherwise value is 0
|
||||
int paymentTens = (itemCost % 20 > 13) ? 1 : 0; // value is 1 when 13 < itemCost < 20 OR 33 < itemCost < 40, otherwise value is 0
|
||||
int paymentTwenties = (itemCost < 20) ? 1 : 2; // value is 1 when itemCost < 20, otherwise value is 2
|
||||
|
||||
// display messages describing the current transaction
|
||||
Console.WriteLine($"Customer is making a ${itemCost} purchase");
|
||||
Console.WriteLine($"\t Using {paymentTwenties} twenty dollar bills");
|
||||
Console.WriteLine($"\t Using {paymentTens} ten dollar bills");
|
||||
Console.WriteLine($"\t Using {paymentFives} five dollar bills");
|
||||
Console.WriteLine($"\t Using {paymentOnes} one dollar bills");
|
||||
|
||||
try
|
||||
{
|
||||
// MakeChange manages the transaction and updates the till
|
||||
MakeChange(itemCost, cashTill, paymentTwenties, paymentTens, paymentFives, paymentOnes);
|
||||
|
||||
// Backup Calculation - each transaction adds current "itemCost" to the till
|
||||
registerCheckTillTotal += itemCost;
|
||||
}
|
||||
catch (InvalidOperationException e)
|
||||
{
|
||||
Console.WriteLine($"Could not complete transaction: {e.Message}");
|
||||
}
|
||||
|
||||
Console.WriteLine(TillAmountSummary(cashTill));
|
||||
Console.WriteLine($"Expected till value: {registerCheckTillTotal}");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
Console.WriteLine("Press the Enter key to exit");
|
||||
do
|
||||
{
|
||||
readResult = Console.ReadLine();
|
||||
|
||||
} while (readResult == null);
|
||||
|
||||
|
||||
static void LoadTillEachMorning(int[,] registerDailyStartingCash, int[] cashTill)
|
||||
{
|
||||
cashTill[0] = registerDailyStartingCash[0, 1];
|
||||
cashTill[1] = registerDailyStartingCash[1, 1];
|
||||
cashTill[2] = registerDailyStartingCash[2, 1];
|
||||
cashTill[3] = registerDailyStartingCash[3, 1];
|
||||
}
|
||||
|
||||
|
||||
static void MakeChange(int cost, int[] cashTill, int twenties, int tens = 0, int fives = 0, int ones = 0)
|
||||
{
|
||||
cashTill[3] += twenties;
|
||||
cashTill[2] += tens;
|
||||
cashTill[1] += fives;
|
||||
cashTill[0] += ones;
|
||||
|
||||
int amountPaid = twenties * 20 + tens * 10 + fives * 5 + ones;
|
||||
int changeNeeded = amountPaid - cost;
|
||||
|
||||
if (changeNeeded < 0)
|
||||
throw new InvalidOperationException("InvalidOperationException: Not enough money provided to complete the transaction.");
|
||||
|
||||
Console.WriteLine("Cashier prepares the following change:");
|
||||
|
||||
while ((changeNeeded > 19) && (cashTill[3] > 0))
|
||||
{
|
||||
cashTill[3]--;
|
||||
changeNeeded -= 20;
|
||||
Console.WriteLine("\t A twenty");
|
||||
}
|
||||
|
||||
while ((changeNeeded > 9) && (cashTill[2] > 0))
|
||||
{
|
||||
cashTill[2]--;
|
||||
changeNeeded -= 10;
|
||||
Console.WriteLine("\t A ten");
|
||||
}
|
||||
|
||||
while ((changeNeeded > 4) && (cashTill[1] > 0))
|
||||
{
|
||||
cashTill[1]--;
|
||||
changeNeeded -= 5;
|
||||
Console.WriteLine("\t A five");
|
||||
}
|
||||
|
||||
while ((changeNeeded > 0) && (cashTill[0] > 0))
|
||||
{
|
||||
cashTill[0]--;
|
||||
changeNeeded -= 1;
|
||||
Console.WriteLine("\t A one");
|
||||
}
|
||||
|
||||
if (changeNeeded > 0)
|
||||
throw new InvalidOperationException("InvalidOperationException: The till is unable to make change for the cash provided.");
|
||||
|
||||
}
|
||||
|
||||
static void LogTillStatus(int[] cashTill)
|
||||
{
|
||||
Console.WriteLine("The till currently has:");
|
||||
Console.WriteLine($"{cashTill[3] * 20} in twenties");
|
||||
Console.WriteLine($"{cashTill[2] * 10} in tens");
|
||||
Console.WriteLine($"{cashTill[1] * 5} in fives");
|
||||
Console.WriteLine($"{cashTill[0]} in ones");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
static string TillAmountSummary(int[] cashTill)
|
||||
{
|
||||
return $"The till has {cashTill[3] * 20 + cashTill[2] * 10 + cashTill[1] * 5 + cashTill[0]} dollars";
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
10
2-csharp/lesson-6-projects/guided-project/Final/Final.csproj
Normal file
10
2-csharp/lesson-6-projects/guided-project/Final/Final.csproj
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
178
2-csharp/lesson-6-projects/guided-project/Final/Program.cs
Normal file
178
2-csharp/lesson-6-projects/guided-project/Final/Program.cs
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
/*
|
||||
This application manages transactions at a store check-out line. The
|
||||
check-out line has a cash register, and the register has a cash till
|
||||
that is prepared with a number of bills each morning. The till includes
|
||||
bills of four denominations: $1, $5, $10, and $20. The till is used
|
||||
to provide the customer with change during the transaction. The item
|
||||
cost is a randomly generated number between 2 and 49. The customer
|
||||
offers payment based on an algorithm that determines a number of bills
|
||||
in each denomination.
|
||||
|
||||
Each day, the cash till is loaded at the start of the day. As transactions
|
||||
occur, the cash till is managed in a method named MakeChange (customer
|
||||
payments go in and the change returned to the customer comes out). A
|
||||
separate "safety check" calculation that's used to verify the amount of
|
||||
money in the till is performed in the "main program". This safety check
|
||||
is used to ensure that logic in the MakeChange method is working as
|
||||
expected.
|
||||
*/
|
||||
|
||||
|
||||
string? readResult = null;
|
||||
bool useTestData = false;
|
||||
|
||||
Console.Clear();
|
||||
|
||||
int[] cashTill = new int[] { 0, 0, 0, 0 };
|
||||
int registerCheckTillTotal = 0;
|
||||
|
||||
// registerDailyStartingCash: $1 x 50, $5 x 20, $10 x 10, $20 x 5 => ($350 total)
|
||||
int[,] registerDailyStartingCash = new int[,] { { 1, 50 }, { 5, 20 }, { 10, 10 }, { 20, 5 } };
|
||||
|
||||
int[] testData = new int[] { 6, 10, 17, 20, 31, 36, 40, 41 };
|
||||
int testCounter = 0;
|
||||
|
||||
LoadTillEachMorning(registerDailyStartingCash, cashTill);
|
||||
|
||||
registerCheckTillTotal = registerDailyStartingCash[0, 0] * registerDailyStartingCash[0, 1] + registerDailyStartingCash[1, 0] * registerDailyStartingCash[1, 1] + registerDailyStartingCash[2, 0] * registerDailyStartingCash[2, 1] + registerDailyStartingCash[3, 0] * registerDailyStartingCash[3, 1];
|
||||
|
||||
// display the number of bills of each denomination currently in the till
|
||||
LogTillStatus(cashTill);
|
||||
|
||||
// display a message showing the amount of cash in the till
|
||||
Console.WriteLine(TillAmountSummary(cashTill));
|
||||
|
||||
// display the expected registerDailyStartingCash total
|
||||
Console.WriteLine($"Expected till value: {registerCheckTillTotal}");
|
||||
Console.WriteLine();
|
||||
|
||||
var valueGenerator = new Random((int)DateTime.Now.Ticks);
|
||||
|
||||
int transactions = 10;
|
||||
|
||||
if (useTestData)
|
||||
{
|
||||
transactions = testData.Length;
|
||||
}
|
||||
|
||||
while (transactions > 0)
|
||||
{
|
||||
transactions -= 1;
|
||||
int itemCost = valueGenerator.Next(2, 50);
|
||||
|
||||
if (useTestData)
|
||||
{
|
||||
itemCost = testData[testCounter];
|
||||
testCounter += 1;
|
||||
}
|
||||
|
||||
int paymentOnes = itemCost % 2; // value is 1 when itemCost is odd, value is 0 when itemCost is even
|
||||
int paymentFives = (itemCost % 10 > 7) ? 1 : 0; // value is 1 when itemCost ends with 8 or 9, otherwise value is 0
|
||||
int paymentTens = (itemCost % 20 > 13) ? 1 : 0; // value is 1 when 13 < itemCost < 20 OR 33 < itemCost < 40, otherwise value is 0
|
||||
int paymentTwenties = (itemCost < 20) ? 1 : 2; // value is 1 when itemCost < 20, otherwise value is 2
|
||||
|
||||
// display messages describing the current transaction
|
||||
Console.WriteLine($"Customer is making a ${itemCost} purchase");
|
||||
Console.WriteLine($"\t Using {paymentTwenties} twenty dollar bills");
|
||||
Console.WriteLine($"\t Using {paymentTens} ten dollar bills");
|
||||
Console.WriteLine($"\t Using {paymentFives} five dollar bills");
|
||||
Console.WriteLine($"\t Using {paymentOnes} one dollar bills");
|
||||
|
||||
try
|
||||
{
|
||||
// MakeChange manages the transaction and updates the till
|
||||
MakeChange(itemCost, cashTill, paymentTwenties, paymentTens, paymentFives, paymentOnes);
|
||||
|
||||
// Backup Calculation - each transaction adds current "itemCost" to the till
|
||||
registerCheckTillTotal += itemCost;
|
||||
}
|
||||
catch (InvalidOperationException e)
|
||||
{
|
||||
Console.WriteLine($"Could not make transaction: {e.Message}");
|
||||
}
|
||||
|
||||
Console.WriteLine(TillAmountSummary(cashTill));
|
||||
Console.WriteLine($"Expected till value: {registerCheckTillTotal}");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
Console.WriteLine("Press the Enter key to exit");
|
||||
do
|
||||
{
|
||||
readResult = Console.ReadLine();
|
||||
|
||||
} while (readResult == null);
|
||||
|
||||
|
||||
static void LoadTillEachMorning(int[,] registerDailyStartingCash, int[] cashTill)
|
||||
{
|
||||
cashTill[0] = registerDailyStartingCash[0, 1];
|
||||
cashTill[1] = registerDailyStartingCash[1, 1];
|
||||
cashTill[2] = registerDailyStartingCash[2, 1];
|
||||
cashTill[3] = registerDailyStartingCash[3, 1];
|
||||
}
|
||||
|
||||
|
||||
static void MakeChange(int cost, int[] cashTill, int twenties, int tens = 0, int fives = 0, int ones = 0)
|
||||
{
|
||||
cashTill[3] += twenties;
|
||||
cashTill[2] += tens;
|
||||
cashTill[1] += fives;
|
||||
cashTill[0] += ones;
|
||||
|
||||
int amountPaid = twenties * 20 + tens * 10 + fives * 5 + ones;
|
||||
int changeNeeded = amountPaid - cost;
|
||||
|
||||
if (changeNeeded < 0)
|
||||
throw new InvalidOperationException("Not enough money provided");
|
||||
|
||||
Console.WriteLine("Cashier Returns:");
|
||||
|
||||
while ((changeNeeded > 19) && (cashTill[3] > 0))
|
||||
{
|
||||
cashTill[3]--;
|
||||
changeNeeded -= 20;
|
||||
Console.WriteLine("\t A twenty");
|
||||
}
|
||||
|
||||
while ((changeNeeded > 9) && (cashTill[2] > 0))
|
||||
{
|
||||
cashTill[2]--;
|
||||
changeNeeded -= 10;
|
||||
Console.WriteLine("\t A ten");
|
||||
}
|
||||
|
||||
while ((changeNeeded > 4) && (cashTill[1] > 0))
|
||||
{
|
||||
cashTill[1]--;
|
||||
changeNeeded -= 5;
|
||||
Console.WriteLine("\t A five");
|
||||
}
|
||||
|
||||
while ((changeNeeded > 0) && (cashTill[0] > 0))
|
||||
{
|
||||
cashTill[0]--;
|
||||
changeNeeded -= 1;
|
||||
Console.WriteLine("\t A one");
|
||||
}
|
||||
|
||||
if (changeNeeded > 0)
|
||||
throw new InvalidOperationException("Can't make change. Do you have anything smaller?");
|
||||
|
||||
}
|
||||
|
||||
static void LogTillStatus(int[] cashTill)
|
||||
{
|
||||
Console.WriteLine("The till currently has:");
|
||||
Console.WriteLine($"{cashTill[3] * 20} in twenties");
|
||||
Console.WriteLine($"{cashTill[2] * 10} in tens");
|
||||
Console.WriteLine($"{cashTill[1] * 5} in fives");
|
||||
Console.WriteLine($"{cashTill[0]} in ones");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
static string TillAmountSummary(int[] cashTill)
|
||||
{
|
||||
return $"The till has {cashTill[3] * 20 + cashTill[2] * 10 + cashTill[1] * 5 + cashTill[0]} dollars";
|
||||
|
||||
}
|
||||
21
2-csharp/lesson-6-projects/guided-project/LICENSE
Normal file
21
2-csharp/lesson-6-projects/guided-project/LICENSE
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2023 Microsoft Learning
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
2
2-csharp/lesson-6-projects/guided-project/README.md
Normal file
2
2-csharp/lesson-6-projects/guided-project/README.md
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# Guided-project-debugging-CSharp
|
||||
Starter and Solution code for the Guided project: "Debug and handle exceptions in a C# console application using Visual Studio Code" from the Microsoft Learn collection "Getting started with C#"
|
||||
183
2-csharp/lesson-6-projects/guided-project/Starter/Program.cs
Normal file
183
2-csharp/lesson-6-projects/guided-project/Starter/Program.cs
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
/*
|
||||
This application manages transactions at a store check-out line. The
|
||||
check-out line has a cash register, and the register has a cash till
|
||||
that is prepared with a number of bills each morning. The till includes
|
||||
bills of four denominations: $1, $5, $10, and $20. The till is used
|
||||
to provide the customer with change during the transaction. The item
|
||||
cost is a randomly generated number between 2 and 49. The customer
|
||||
offers payment based on an algorithm that determines a number of bills
|
||||
in each denomination.
|
||||
|
||||
Each day, the cash till is loaded at the start of the day. As transactions
|
||||
occur, the cash till is managed in a method named MakeChange (customer
|
||||
payments go in and the change returned to the customer comes out). A
|
||||
separate "safety check" calculation that's used to verify the amount of
|
||||
money in the till is performed in the "main program". This safety check
|
||||
is used to ensure that logic in the MakeChange method is working as
|
||||
expected.
|
||||
*/
|
||||
|
||||
string? readResult = null;
|
||||
bool useTestData = true;
|
||||
|
||||
Console.Clear();
|
||||
|
||||
int[] cashTill = new int[] { 0, 0, 0, 0 };
|
||||
int registerCheckTillTotal = 0;
|
||||
|
||||
// registerDailyStartingCash: $1 x 50, $5 x 20, $10 x 10, $20 x 5 => ($350 total)
|
||||
int[,] registerDailyStartingCash = new int[,] { { 1, 50 }, { 5, 20 }, { 10, 10 }, { 20, 5 } };
|
||||
|
||||
int[] testData = new int[] { 6, 10, 17, 20, 31, 36, 40, 41 };
|
||||
int testCounter = 0;
|
||||
|
||||
LoadTillEachMorning(registerDailyStartingCash, cashTill);
|
||||
|
||||
registerCheckTillTotal = registerDailyStartingCash[0, 0] * registerDailyStartingCash[0, 1] + registerDailyStartingCash[1, 0] * registerDailyStartingCash[1, 1] + registerDailyStartingCash[2, 0] * registerDailyStartingCash[2, 1] + registerDailyStartingCash[3, 0] * registerDailyStartingCash[3, 1];
|
||||
|
||||
// display the number of bills of each denomination currently in the till
|
||||
LogTillStatus(cashTill);
|
||||
|
||||
// display a message showing the amount of cash in the till
|
||||
Console.WriteLine(TillAmountSummary(cashTill));
|
||||
|
||||
// display the expected registerDailyStartingCash total
|
||||
Console.WriteLine($"Expected till value: {registerCheckTillTotal}\n\r");
|
||||
|
||||
var valueGenerator = new Random((int)DateTime.Now.Ticks);
|
||||
|
||||
int transactions = 10;
|
||||
|
||||
if (useTestData)
|
||||
{
|
||||
transactions = testData.Length;
|
||||
}
|
||||
|
||||
while (transactions > 0)
|
||||
{
|
||||
transactions -= 1;
|
||||
int itemCost = valueGenerator.Next(2, 20);
|
||||
|
||||
if (useTestData)
|
||||
{
|
||||
itemCost = testData[testCounter];
|
||||
testCounter += 1;
|
||||
}
|
||||
|
||||
int paymentOnes = itemCost % 2; // value is 1 when itemCost is odd, value is 0 when itemCost is even
|
||||
int paymentFives = (itemCost % 10 > 7) ? 1 : 0; // value is 1 when itemCost ends with 8 or 9, otherwise value is 0
|
||||
int paymentTens = (itemCost % 20 > 13) ? 1 : 0; // value is 1 when 13 < itemCost < 20 OR 33 < itemCost < 40, otherwise value is 0
|
||||
int paymentTwenties = (itemCost < 20) ? 1 : 2; // value is 1 when itemCost < 20, otherwise value is 2
|
||||
|
||||
// display messages describing the current transaction
|
||||
Console.WriteLine($"Customer is making a ${itemCost} purchase");
|
||||
Console.WriteLine($"\t Using {paymentTwenties} twenty dollar bills");
|
||||
Console.WriteLine($"\t Using {paymentTens} ten dollar bills");
|
||||
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")
|
||||
{
|
||||
Console.WriteLine($"Transaction successfully completed.");
|
||||
registerCheckTillTotal += itemCost;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Transaction unsuccessful: {transactionMessage}");
|
||||
}
|
||||
|
||||
Console.WriteLine(TillAmountSummary(cashTill));
|
||||
Console.WriteLine($"Expected till value: {registerCheckTillTotal}\n\r");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
Console.WriteLine("Press the Enter key to exit");
|
||||
do
|
||||
{
|
||||
readResult = Console.ReadLine();
|
||||
|
||||
} while (readResult == null);
|
||||
|
||||
|
||||
static void LoadTillEachMorning(int[,] registerDailyStartingCash, int[] cashTill)
|
||||
{
|
||||
cashTill[0] = registerDailyStartingCash[0, 1];
|
||||
cashTill[1] = registerDailyStartingCash[1, 1];
|
||||
cashTill[2] = registerDailyStartingCash[2, 1];
|
||||
cashTill[3] = registerDailyStartingCash[3, 1];
|
||||
}
|
||||
|
||||
|
||||
static string 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;
|
||||
cashTill[0] += ones;
|
||||
|
||||
int amountPaid = twenties * 20 + tens * 10 + fives * 5 + ones;
|
||||
int changeNeeded = amountPaid - cost;
|
||||
|
||||
if (changeNeeded < 0)
|
||||
transactionMessage = "Not enough money provided.";
|
||||
|
||||
Console.WriteLine("Cashier Returns:");
|
||||
|
||||
while ((changeNeeded > 19) && (cashTill[3] > 0))
|
||||
{
|
||||
cashTill[3]--;
|
||||
changeNeeded -= 20;
|
||||
Console.WriteLine("\t A twenty");
|
||||
}
|
||||
|
||||
while ((changeNeeded > 9) && (cashTill[2] > 0))
|
||||
{
|
||||
cashTill[2]--;
|
||||
changeNeeded -= 10;
|
||||
Console.WriteLine("\t A ten");
|
||||
}
|
||||
|
||||
while ((changeNeeded > 4) && (cashTill[1] > 0))
|
||||
{
|
||||
cashTill[2]--;
|
||||
changeNeeded -= 5;
|
||||
Console.WriteLine("\t A five");
|
||||
}
|
||||
|
||||
while ((changeNeeded > 0) && (cashTill[0] > 0))
|
||||
{
|
||||
cashTill[0]--;
|
||||
changeNeeded--;
|
||||
Console.WriteLine("\t A one");
|
||||
}
|
||||
|
||||
if (changeNeeded > 0)
|
||||
transactionMessage = "Can't make change. Do you have anything smaller?";
|
||||
|
||||
if (transactionMessage == "")
|
||||
transactionMessage = "transaction succeeded";
|
||||
|
||||
return transactionMessage;
|
||||
}
|
||||
|
||||
static void LogTillStatus(int[] cashTill)
|
||||
{
|
||||
Console.WriteLine("The till currently has:");
|
||||
Console.WriteLine($"{cashTill[3] * 20} in twenties");
|
||||
Console.WriteLine($"{cashTill[2] * 10} in tens");
|
||||
Console.WriteLine($"{cashTill[1] * 5} in fives");
|
||||
Console.WriteLine($"{cashTill[0]} in ones");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
static string TillAmountSummary(int[] cashTill)
|
||||
{
|
||||
return $"The till has {cashTill[3] * 20 + cashTill[2] * 10 + cashTill[1] * 5 + cashTill[0]} dollars";
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue