Created
April 14, 2024 07:05
-
-
Save timReynolds/aca2189f8a0fae57f0938ec641da4632 to your computer and use it in GitHub Desktop.
Area Cal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
interface IShapeCalculator | |
{ | |
void CalculateAndDisplayArea(); | |
} | |
class CircleCalculator : IShapeCalculator | |
{ | |
public void CalculateAndDisplayArea() | |
{ | |
Console.WriteLine("Enter the radius:"); | |
double radius = double.Parse(Console.ReadLine()); | |
double area = Math.PI * radius * radius; | |
Console.WriteLine($"The area of the circle is: {area}"); | |
} | |
} | |
class RectangleCalculator : IShapeCalculator | |
{ | |
public void CalculateAndDisplayArea() | |
{ | |
Console.WriteLine("Enter the length:"); | |
double length = double.Parse(Console.ReadLine()); | |
Console.WriteLine("Enter the width:"); | |
double width = double.Parse(Console.ReadLine()); | |
double area = length * width; | |
Console.WriteLine($"The area of the rectangle is: {area}"); | |
} | |
} | |
class TriangleCalculator : IShapeCalculator | |
{ | |
public void CalculateAndDisplayArea() | |
{ | |
Console.WriteLine("Enter the base:"); | |
double baseLength = double.Parse(Console.ReadLine()); | |
Console.WriteLine("Enter the height:"); | |
double height = double.Parse(Console.ReadLine()); | |
double area = 0.5 * baseLength * height; | |
Console.WriteLine($"The area of the triangle is: {area}"); | |
} | |
} | |
class Program | |
{ | |
static readonly Dictionary<string, IShapeCalculator> calculators = new Dictionary<string, IShapeCalculator> | |
{ | |
{ "circle", new CircleCalculator() }, | |
{ "rectangle", new RectangleCalculator() }, | |
{ "triangle", new TriangleCalculator() } | |
}; | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Enter the shape type (circle, rectangle, triangle):"); | |
string shapeType = Console.ReadLine(); | |
if (calculators.TryGetValue(shapeType, out var calculator)) | |
{ | |
calculator.CalculateAndDisplayArea(); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid shape type entered."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment