diff --git a/Src/BootCamp.Chapter/Checks.cs b/Src/BootCamp.Chapter/Checks.cs index 49864932a..bce30fb48 100644 --- a/Src/BootCamp.Chapter/Checks.cs +++ b/Src/BootCamp.Chapter/Checks.cs @@ -17,25 +17,25 @@ public static class Checks public static int PromptInt(string message) { // To do: call your implementation. - return 0; + return Lesson4.GetInt(message); } public static string PromptString(string message) { // To do: call your implementation. - return ""; + return Lesson4.GetString(message); } public static float PromptFloat(string message) { // To do: call your implementation. - return 0; + return Lesson4.GetFloat(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..098a5aad0 --- /dev/null +++ b/Src/BootCamp.Chapter/Lesson4.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Text; + +namespace BootCamp.Chapter +{ + public class Lesson4 + { + public static void Demo() + { + var userOne = GetUserInfo(); + Console.WriteLine($"{userOne}"); + + var userTwo = GetUserInfo(); + Console.WriteLine($"{userTwo}"); + } + + public static string GetInput(string message) + { + Console.Write($"{message}"); + var input = Console.ReadLine(); + return input; + } + + public static string GetString(string message) + { + var input = GetInput(message); + if (string.IsNullOrEmpty(input)) + { + Console.WriteLine("Name cannot be empty"); + return "-"; + } + return input; + } + public static int GetInt(string message) + { + var input = GetInput(message); + var isValidNumber = int.TryParse(input, out var result); + return isValidNumber ? result : PrintAndReturnNumberError($"\"{input}\" is not a valid number."); + + } + public static float GetFloat(string message) + { + var input = GetInput(message); + var isValidNumber = float.TryParse(input, NumberStyles.Float, CultureInfo.InvariantCulture, out var result); + + return isValidNumber ? result : PrintAndReturnNumberError($"\"{input}\" is not a valid number."); + } + + public static int PrintAndReturnNumberError(string message) + { + Console.WriteLine($"{message}"); + return -1; + } + + public static float CalculateBMI(float weight, float height) + { + if (weight <= 0 || height <= 0) + { + Console.WriteLine("Failed calculating BMI. Reason: "); + if (height <= 0) + Console.WriteLine($"Height cannot be equal or less than zero, but was {height}."); + if (weight <= 0) + Console.WriteLine($"Weight cannot be equal or less than zero, but was {weight}."); + return -1; + } + + return weight / (float)Math.Pow(height, 2); + } + + public static string GetUserInfo() + { + var name = GetInput("name: "); + var surName = GetInput("surname: "); + var age = GetInt("age: "); + var weight = GetFloat("weight: "); + var height = GetFloat("height: "); + var bmi = CalculateBMI(weight, height); + + return PrintUserInfo(name, surName, age, weight, height); + } + + public static string PrintUserInfo(string name, string sureName, int age, float weight, double height) + { + return $"{name} {sureName} is {age} years old, his weight is {weight} kg and his height is {height} cm."; + } + } +} diff --git a/Src/BootCamp.Chapter/Program.cs b/Src/BootCamp.Chapter/Program.cs index 94e5531bc..9a916053f 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(); } } }