Answer to Question #132447 in C# for Dennis

Question #132447
Write a complete C# application program to calculate the area of a triangle, rectangle or circle. The program should have a decision making statement(s) to decide:
 If the figure is a triangle then ask for base and height values to input.
 If the figure is a rectangle the ask for length and breath values to input
 If the figure is a circle then ask for the radius values to input
The program should display the output on the screen and should enable the user to re-enter choice for at most three times incase a wrong selection is entered
1
Expert's answer
2020-09-10T15:54:37-0400
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");
    }

  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS