Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 4 homework #809

Open
wants to merge 1 commit into
base: Chapter1/Homework/4
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion Src/BootCamp.Chapter/BootCamp.Chapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp6.0</TargetFramework>
</PropertyGroup>

</Project>
15 changes: 7 additions & 8 deletions Src/BootCamp.Chapter/Checks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
}
87 changes: 87 additions & 0 deletions Src/BootCamp.Chapter/Lesson4.cs
Original file line number Diff line number Diff line change
@@ -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 "-";
}
}
}
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();
}
}
}
2 changes: 1 addition & 1 deletion Tests/BootCamp.Chapter.Tests/BootCamp.Chapter.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp6.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
16 changes: 16 additions & 0 deletions Tests/BootCamp.Chapter.Tests/Utils/FakeConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

}