using SampleApplication.Entities; using System.Net.Http.Headers; using System.Reflection.Metadata.Ecma335; namespace SampleApplication { public class ShapeProcessor { public List shapes = new List(); public void AddShapes(IEnumerable shapes) { // TODO: // - Ignore duplicate Ids // - Validate dimensions // - Store shapes in memory foreach(var shape in shapes) { if (!ValidateDimensions(shape)) { throw new ArgumentException("Invalid shape dimensions"); } this.shapes.Add(shape); } } public (double Area, double Perimeter) GetResult(int id) { // TODO: // - Throw KeyNotFoundException if not found // - Compute area & perimeter if(!shapes.Any(s => s.Id == id)) { throw new KeyNotFoundException("Shape not found"); } var shape = shapes.First(s => s.Id == id); return this.CalculateAreaPerimeter(shape); } public (int TotalShapes, double TotalArea, int MaxPerimeterId) GetStatistics() { // TODO: var totalShapes = shapes.Count; double totalArea = shapes.Sum(s => this.CalculateAreaPerimeter(s).area); var maxPerimeterShape = shapes.OrderByDescending(s => this.CalculateAreaPerimeter(s).perimeter).FirstOrDefault(); return (totalShapes, totalArea, maxPerimeterShape != null ? maxPerimeterShape.Id : 0); } private bool ValidateDimensions(ShapeInput shape) { return shape.Type switch { ShapeType.Circle => shape.Dimensions.Length == 1 && shape.Dimensions[0] > 0, ShapeType.Rectangle => shape.Dimensions.Length == 2 && shape.Dimensions.All(d => d > 0), ShapeType.Triangle => shape.Dimensions.Length == 2, _ => false, }; } private (double area, double perimeter) CalculateAreaPerimeter(ShapeInput shape) { var perimeter = shape.Type switch { ShapeType.Circle => 2 * Math.PI * shape.Dimensions[0], ShapeType.Rectangle => 2 * (shape.Dimensions[0] + shape.Dimensions[1]), ShapeType.Triangle => this.CalculateTrianglePerimeter(shape), _ => throw new NotSupportedException("Shape type not supported"), }; var area = shape.Type switch { ShapeType.Circle => Math.PI * shape.Dimensions[0] * shape.Dimensions[0], ShapeType.Rectangle => shape.Dimensions[0] * shape.Dimensions[1], ShapeType.Triangle => 0.5 * shape.Dimensions[0] * shape.Dimensions[1], _ => throw new NotSupportedException("Shape type not supported"), }; return (area, perimeter); } private double CalculateTrianglePerimeter(ShapeInput shape) { double @base = shape.Dimensions[0]; double height = shape.Dimensions[1]; double hypotenuse = Math.Sqrt(@base * @base + height * height); return @base + height + hypotenuse; } } }