-
Notifications
You must be signed in to change notification settings - Fork 558
Expand file tree
/
Copy pathLesson4.cs
More file actions
89 lines (77 loc) · 2.86 KB
/
Lesson4.cs
File metadata and controls
89 lines (77 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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.";
}
}
}