From 341d21550148a3cb3dbeb391652a9e2780f4e5fb Mon Sep 17 00:00:00 2001 From: LostCSharp7 Date: Sun, 1 Oct 2023 05:19:35 +0530 Subject: [PATCH] Chapter 4 homework --- Src/BootCamp.Chapter/BootCamp.Chapter.csproj | 2 +- Src/BootCamp.Chapter/Checks.cs | 15 ++-- Src/BootCamp.Chapter/Lesson4.cs | 87 +++++++++++++++++++ Src/BootCamp.Chapter/Program.cs | 1 + .../BootCamp.Chapter.Tests.csproj | 2 +- .../Utils/FakeConsole.cs | 16 ++++ 6 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 Src/BootCamp.Chapter/Lesson4.cs diff --git a/Src/BootCamp.Chapter/BootCamp.Chapter.csproj b/Src/BootCamp.Chapter/BootCamp.Chapter.csproj index 958d2f1da..fa2362844 100644 --- a/Src/BootCamp.Chapter/BootCamp.Chapter.csproj +++ b/Src/BootCamp.Chapter/BootCamp.Chapter.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp6.0 diff --git a/Src/BootCamp.Chapter/Checks.cs b/Src/BootCamp.Chapter/Checks.cs index 49864932a..0f6d1e241 100644 --- a/Src/BootCamp.Chapter/Checks.cs +++ b/Src/BootCamp.Chapter/Checks.cs @@ -16,26 +16,25 @@ public static class Checks { public static int PromptInt(string message) { - // To do: call your implementation. - return 0; + return Lesson4.PromptInt(message); + } public static string PromptString(string message) { - // To do: call your implementation. - return ""; + return Lesson4.PromptString(message); + } public static float PromptFloat(string message) { - // To do: call your implementation. - return 0; + return Lesson4.PromptFloat(message); } public static float CalculateBmi(float weight, float height) { - // To do: call your implementation. - return 0; + return Lesson4.CalculateBMI(weight, height); + } } } diff --git a/Src/BootCamp.Chapter/Lesson4.cs b/Src/BootCamp.Chapter/Lesson4.cs new file mode 100644 index 000000000..686419ad5 --- /dev/null +++ b/Src/BootCamp.Chapter/Lesson4.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; + +namespace BootCamp.Chapter +{ + internal class Lesson4 + { + public static void demo() + { + //Gather the input from the user. + string name = PromptString("Enter your name: "); + string surName = PromptString("Enter your surname: "); + int age = PromptInt("Enter your age: "); + float height = PromptFloat("Enter your height(m): "); + float weight = PromptFloat("Enter your waight(Kg): "); + + //Find BMI + double bodyMassIndex = CalculateBMI(weight, height); + if(bodyMassIndex <= 0f ) + { + return; + } + //Print messages + Console.WriteLine(name + " " + surName + " is " + age + " years old, his weight is " + weight + " kg and his height is " + height + "m."); + Console.WriteLine("Body Mass Index measured for " + name + " " + surName + " is " + bodyMassIndex); + } + + public static float CalculateBMI(float weight, float height) + { + if(weight <= 0f || height <= 0f) + { + Console.WriteLine("Failed calculating BMI. Reason:"); + if (height <= 0f) Console.WriteLine("Height cannot be equal or less than zero, but was " + height); + if (weight <= 0f) Console.WriteLine("Weight cannot be equal or less than zero, but was " + weight); + return -1f; + } + float bodyMassIndex = (weight / (height * height)); + return bodyMassIndex; + } + + public static int PromptInt(string Message) + { + int age; + Console.WriteLine(Message); + string Input = Console.ReadLine(); + bool isAgeValid = int.TryParse(Input, out age); + if (!isAgeValid) + { + Console.Write("\"" + Input + "\" is not a valid number." + Environment.NewLine); + _ = Environment.NewLine; + return -1; + } + return age; + + } + + public static float PromptFloat(string Message) + { + float metrics; + Console.WriteLine(Message); + string Input = Console.ReadLine(); + bool IsValidHeight_Weigth = float.TryParse(Input, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out metrics); + if (!IsValidHeight_Weigth) + { + Console.Write( "\"" + Input + "\" is not a valid number." + Environment.NewLine); + + return -1f; + } + return metrics; + } + + public static string PromptString(string Message) + { + string Name; + Console.WriteLine(Message); + Name = Console.ReadLine(); + if(!String.IsNullOrEmpty(Name)) return Name; + Console.WriteLine("Name cannot be empty" + Environment.NewLine); + return "-"; + } + } +} diff --git a/Src/BootCamp.Chapter/Program.cs b/Src/BootCamp.Chapter/Program.cs index 94e5531bc..09fa8183d 100644 --- a/Src/BootCamp.Chapter/Program.cs +++ b/Src/BootCamp.Chapter/Program.cs @@ -9,6 +9,7 @@ class Program { static void Main(string[] args) { + Lesson4.demo(); } } } diff --git a/Tests/BootCamp.Chapter.Tests/BootCamp.Chapter.Tests.csproj b/Tests/BootCamp.Chapter.Tests/BootCamp.Chapter.Tests.csproj index 3a8778c77..ffca4abc4 100644 --- a/Tests/BootCamp.Chapter.Tests/BootCamp.Chapter.Tests.csproj +++ b/Tests/BootCamp.Chapter.Tests/BootCamp.Chapter.Tests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.0 + netcoreapp6.0 false diff --git a/Tests/BootCamp.Chapter.Tests/Utils/FakeConsole.cs b/Tests/BootCamp.Chapter.Tests/Utils/FakeConsole.cs index 8a058c034..180e6adf7 100644 --- a/Tests/BootCamp.Chapter.Tests/Utils/FakeConsole.cs +++ b/Tests/BootCamp.Chapter.Tests/Utils/FakeConsole.cs @@ -30,4 +30,20 @@ public static void Cleanup(string testKey) File.Delete($"{testKey}.{TestFileExtension}"); } } + + + public static class ConsoleStub + { + public static StringWriter StubConsole(string readLineReturn) + { + var output = new StringWriter(); + Console.SetOut(output); + + var input = new StringReader(readLineReturn); + Console.SetIn(input); + + return output; + } + } + }