From 65a86f7d6da8b9ee9491690a1ecc5e978048e813 Mon Sep 17 00:00:00 2001 From: Piotr Czajkowski Date: Sun, 2 Jan 2022 13:04:39 +0100 Subject: [PATCH 1/7] Added Header --- 22_Change/csharp/Change.csproj | 8 ++++++++ 22_Change/csharp/Change.sln | 25 +++++++++++++++++++++++++ 22_Change/csharp/Program.cs | 25 +++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 22_Change/csharp/Change.csproj create mode 100644 22_Change/csharp/Change.sln create mode 100644 22_Change/csharp/Program.cs diff --git a/22_Change/csharp/Change.csproj b/22_Change/csharp/Change.csproj new file mode 100644 index 00000000..1d2d39a9 --- /dev/null +++ b/22_Change/csharp/Change.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/22_Change/csharp/Change.sln b/22_Change/csharp/Change.sln new file mode 100644 index 00000000..c0f2e5e8 --- /dev/null +++ b/22_Change/csharp/Change.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.810.16 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Change", "Change.csproj", "{AE094667-8496-4ECF-8B42-B1648EE26073}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AE094667-8496-4ECF-8B42-B1648EE26073}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AE094667-8496-4ECF-8B42-B1648EE26073}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AE094667-8496-4ECF-8B42-B1648EE26073}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AE094667-8496-4ECF-8B42-B1648EE26073}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {65684CBD-CD74-46AF-8E9E-0F69DCF72697} + EndGlobalSection +EndGlobal diff --git a/22_Change/csharp/Program.cs b/22_Change/csharp/Program.cs new file mode 100644 index 00000000..c8adbeac --- /dev/null +++ b/22_Change/csharp/Program.cs @@ -0,0 +1,25 @@ +using System; + +namespace Change +{ + class Program + { + static void Header() + { + Console.WriteLine("Change".PadLeft(33)); + Console.WriteLine("Creative Computing Morristown, New Jersey".PadLeft(15)); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine("I, your friendly microcomputer, will determine\n" + + "the correct change for items costing up to $100."); + Console.WriteLine(); + Console.WriteLine(); + } + + static void Main(string[] args) + { + Header(); + } + } +} From 6c0b0da270d7da4b8e9532f6837c65b74e60dd94 Mon Sep 17 00:00:00 2001 From: Piotr Czajkowski Date: Sun, 2 Jan 2022 13:29:27 +0100 Subject: [PATCH 2/7] Added GetInput --- 22_Change/csharp/Program.cs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/22_Change/csharp/Program.cs b/22_Change/csharp/Program.cs index c8adbeac..2c28ea99 100644 --- a/22_Change/csharp/Program.cs +++ b/22_Change/csharp/Program.cs @@ -17,9 +17,43 @@ namespace Change Console.WriteLine(); } + static (bool, double) GetInput() + { + Console.WriteLine("Cost of item? "); + var priceString = Console.ReadLine(); + if (!double.TryParse(priceString, out double price)) + { + Console.WriteLine($"{priceString} isn't a number!"); + return (false, 0); + } + + Console.WriteLine("Amount of payment? "); + var paymentString = Console.ReadLine(); + if (!double.TryParse(paymentString, out double payment)) + { + Console.WriteLine($"{paymentString} isn't a number!"); + return (false, 0); + } + + return (true, payment - price); + } + static void Main(string[] args) { Header(); + + while (true) + { + (bool result, double change) = GetInput(); + if (!result) + continue; + + if (change == 0) + { + Console.WriteLine("Correct amount, thank you!"); + continue; + } + } } } } From d530794947e6341b72d155a80d014351f19231c1 Mon Sep 17 00:00:00 2001 From: Piotr Czajkowski Date: Sun, 2 Jan 2022 14:08:49 +0100 Subject: [PATCH 3/7] Added PrintChange --- 22_Change/csharp/Program.cs | 65 ++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/22_Change/csharp/Program.cs b/22_Change/csharp/Program.cs index 2c28ea99..b45e5114 100644 --- a/22_Change/csharp/Program.cs +++ b/22_Change/csharp/Program.cs @@ -17,14 +17,14 @@ namespace Change Console.WriteLine(); } - static (bool, double) GetInput() + static (bool status, double price, double payment) GetInput() { Console.WriteLine("Cost of item? "); var priceString = Console.ReadLine(); if (!double.TryParse(priceString, out double price)) { Console.WriteLine($"{priceString} isn't a number!"); - return (false, 0); + return (false, 0, 0); } Console.WriteLine("Amount of payment? "); @@ -32,10 +32,53 @@ namespace Change if (!double.TryParse(paymentString, out double payment)) { Console.WriteLine($"{paymentString} isn't a number!"); - return (false, 0); + return (false, 0, 0); } - return (true, payment - price); + return (true, price, payment); + } + + static void PrintChange(double change) + { + var tens = (int)(change / 10); + if (tens > 0) + Console.WriteLine($"{tens} ten dollar bill(s)"); + + var temp = change - (tens * 10); + var fives = (int)(temp / 5); + if (fives > 0) + Console.WriteLine($"{fives} five dollar bill(s)"); + + temp -= fives * 5; + var ones = (int)temp; + if (ones > 0) + Console.WriteLine($"{ones} one dollar bill(s)"); + + temp -= ones; + var cents = temp * 100; + var half = (int)(cents / 50); + if (half > 0) + Console.WriteLine($"{half} one half dollar(s)"); + + temp = cents - (half * 50); + var quarters = (int)(temp / 25); + if (quarters > 0) + Console.WriteLine($"{quarters} quarter(s)"); + + temp -= quarters * 25; + var dimes = (int)(temp / 10); + if (dimes > 0) + Console.WriteLine($"{dimes} dime(s)"); + + temp -= dimes * 10; + var nickels = (int)(temp / 5); + if (nickels > 0) + Console.WriteLine($"{nickels} nickel(s)"); + + temp -= nickels * 5; + var pennys = (int)(temp + 0.5); + if (pennys > 0) + Console.WriteLine($"{pennys} penny(s)"); } static void Main(string[] args) @@ -44,15 +87,27 @@ namespace Change while (true) { - (bool result, double change) = GetInput(); + (bool result, double price, double payment) = GetInput(); if (!result) continue; + var change = payment - price; if (change == 0) { Console.WriteLine("Correct amount, thank you!"); continue; } + + if (change < 0) + { + Console.WriteLine($"Sorry, you have short-changed me ${price - payment}!"); + continue; + } + + Console.WriteLine($"Your change ${change:N2}"); + PrintChange(change); + Console.WriteLine("Thank you, come again!"); + Console.WriteLine(); } } } From 835adc571c68cf9b828ea7e81bdc4749d7d844a2 Mon Sep 17 00:00:00 2001 From: Piotr Czajkowski Date: Sun, 2 Jan 2022 14:22:37 +0100 Subject: [PATCH 4/7] Format --- 22_Change/csharp/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/22_Change/csharp/Program.cs b/22_Change/csharp/Program.cs index b45e5114..3b609d9d 100644 --- a/22_Change/csharp/Program.cs +++ b/22_Change/csharp/Program.cs @@ -100,7 +100,7 @@ namespace Change if (change < 0) { - Console.WriteLine($"Sorry, you have short-changed me ${price - payment}!"); + Console.WriteLine($"Sorry, you have short-changed me ${price - payment:N2}!"); continue; } From 534fa866bdbb927c09861d87e2a9748ccc1c5335 Mon Sep 17 00:00:00 2001 From: Piotr Czajkowski Date: Sun, 2 Jan 2022 14:29:11 +0100 Subject: [PATCH 5/7] Added some descriptions --- 22_Change/csharp/Program.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/22_Change/csharp/Program.cs b/22_Change/csharp/Program.cs index 3b609d9d..b57e45e3 100644 --- a/22_Change/csharp/Program.cs +++ b/22_Change/csharp/Program.cs @@ -4,6 +4,9 @@ namespace Change { class Program { + /// + /// Prints header. + /// static void Header() { Console.WriteLine("Change".PadLeft(33)); @@ -17,6 +20,14 @@ namespace Change Console.WriteLine(); } + /// + /// Gets user input for price and payment. + /// + /// + /// False if any input can't be parsed to double. Price and payment returned would be 0. + /// True if it was possible to parse inputs into doubles. Price and payment returned + /// would be as provided by the user. + /// static (bool status, double price, double payment) GetInput() { Console.WriteLine("Cost of item? "); @@ -38,6 +49,10 @@ namespace Change return (true, price, payment); } + /// + /// Prints bills and coins for given change. + /// + /// static void PrintChange(double change) { var tens = (int)(change / 10); From 64682e704cf5f4bc9fa26eacf46795b3bd125b28 Mon Sep 17 00:00:00 2001 From: Piotr Czajkowski Date: Sun, 2 Jan 2022 14:36:33 +0100 Subject: [PATCH 6/7] Spelling --- 22_Change/csharp/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/22_Change/csharp/Program.cs b/22_Change/csharp/Program.cs index b57e45e3..b7496b0f 100644 --- a/22_Change/csharp/Program.cs +++ b/22_Change/csharp/Program.cs @@ -91,9 +91,9 @@ namespace Change Console.WriteLine($"{nickels} nickel(s)"); temp -= nickels * 5; - var pennys = (int)(temp + 0.5); - if (pennys > 0) - Console.WriteLine($"{pennys} penny(s)"); + var pennies = (int)(temp + 0.5); + if (pennies > 0) + Console.WriteLine($"{pennies} penny(s)"); } static void Main(string[] args) From b27ee11d0076687d4462312076516e6e8093d3ef Mon Sep 17 00:00:00 2001 From: Piotr Czajkowski Date: Sun, 2 Jan 2022 14:50:51 +0100 Subject: [PATCH 7/7] Input closer to original --- 22_Change/csharp/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/22_Change/csharp/Program.cs b/22_Change/csharp/Program.cs index b7496b0f..12804731 100644 --- a/22_Change/csharp/Program.cs +++ b/22_Change/csharp/Program.cs @@ -30,7 +30,7 @@ namespace Change /// static (bool status, double price, double payment) GetInput() { - Console.WriteLine("Cost of item? "); + Console.Write("Cost of item? "); var priceString = Console.ReadLine(); if (!double.TryParse(priceString, out double price)) { @@ -38,7 +38,7 @@ namespace Change return (false, 0, 0); } - Console.WriteLine("Amount of payment? "); + Console.Write("Amount of payment? "); var paymentString = Console.ReadLine(); if (!double.TryParse(paymentString, out double payment)) {