Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Src/BootCamp.Chapter/Checks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
89 changes: 89 additions & 0 deletions Src/BootCamp.Chapter/Lesson4.cs
Original file line number Diff line number Diff line change
@@ -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.";
}
}
}
1 change: 1 addition & 0 deletions Src/BootCamp.Chapter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Program
{
static void Main(string[] args)
{
Lesson4.Demo();
}
}
}