using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main()
{
int attempts = 3;
while (attempts > 0)
{
PrintMenu();
Console.Write("Select the number of figure: ");
switch (Console.ReadLine())
{
case "1":
double? triangleBase = ReadDouble("Input the base value: ");
double? height = ReadDouble("Input the height value:");
if (!IsInputCorrect(triangleBase, height))
{
Console.WriteLine("Incorrect input. Press any key and try again...");
Console.ReadKey();
break;
}
Console.WriteLine($"Area of figure is {0.5 * height * triangleBase}");
break;
case "2":
double? length = ReadDouble("Input the length value: ");
double? width = ReadDouble("Input the width value:");
if (!IsInputCorrect(length, width))
{
Console.WriteLine("Incorrect input. Press any key and try again...");
Console.ReadKey();
break;
}
Console.WriteLine($"Area of figure is {length * width}");
break;
case "3":
double? radius = ReadDouble("Input the radius value: ");
if (!IsInputCorrect(radius))
{
Console.WriteLine("Incorrect input. Press any key and try again...");
Console.ReadKey();
break;
}
Console.WriteLine($"Area of figure is {radius * radius * Math.PI}");
break;
default:
if (--attempts > 0)
{
Console.WriteLine("Incorrect input. Press any key and try again...");
Console.ReadKey();
}
break;
}
if (attempts == 0)
break;
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
Console.Clear();
}
Console.WriteLine("Press any key...");
Console.ReadKey();
}
private static bool IsInputCorrect(params double?[] values)
{
return values.All(v => v.HasValue);
}
private static double? ReadDouble(string message, int attempts = 3)
{
while (true)
{
Console.Write(message);
if (double.TryParse(Console.ReadLine(), out double result))
{
return result;
}
else if(--attempts > 0)
{
Console.WriteLine($"Incorrect input. You have {attempts} to correct input");
}
else
{
return null;
}
}
}
private static void PrintMenu()
{
Console.WriteLine("Calculate area of figure, choose the figure");
Console.WriteLine("1. Triangle");
Console.WriteLine("2. Rectangle");
Console.WriteLine("3. Circle");
}
}
}
Comments
Leave a comment