public string ProcessResult(FigureAreaResult resultOfCalculation) { string result = ""; if (resultOfCalculation.Successful) { result = string.Format("Pole powierzchni wybranej figury {0} wynosi: {1}", resultOfCalculation.Figure.Name, resultOfCalculation.Result); } else { try { throw resultOfCalculation.Exception; } catch (FormatException) { result = "Podano nieprawidłowe dane"; } catch (NullReferenceException) { result = "Należy podać wymiary"; } catch (IndexOutOfRangeException) { result = "Należy podać wszystkie wymagane wymiary oddzielone ;"; } } return(result); }
public FigureAreaResult CalculateArea(FormData formData) { FigureAreaResult resultOfCalculation = new FigureAreaResult(); resultOfCalculation.Successful = true; string sizes = formData.Sizes; try { if (formData.Sizes.Contains(",")) { sizes = formData.Sizes.Replace(',', '.'); } string[] sizesFromUser = sizes.Split(new Char[] { ';' }); switch (formData.ChosenFigure) { case FiguresEN.Square: Square square = new Square() { Name = FiguresPL.Kwadrat.ToString(), SizeA = Convert.ToDouble(sizesFromUser[0]) }; SquareAreaCalculator squareAreaCalculator = new SquareAreaCalculator(); resultOfCalculation.Figure = square; resultOfCalculation.Result = squareAreaCalculator.AreaCalculator(square); break; case FiguresEN.Rectangle: Rectangle rectangle = new Rectangle() { Name = FiguresPL.Prostokąt.ToString(), SizeA = Convert.ToDouble(sizesFromUser[0]), SizeB = Convert.ToDouble(sizesFromUser[1]) }; RectangleAreaCalculator rectangleAreaCalculator = new RectangleAreaCalculator(); resultOfCalculation.Figure = rectangle; resultOfCalculation.Result = rectangleAreaCalculator.AreaCalculator(rectangle); break; case FiguresEN.Parallelogram: Parallelogram parallelogram = new Parallelogram() { Name = FiguresPL.Równoległobok.ToString(), SizeA = Convert.ToDouble(sizesFromUser[0]), SizeH = Convert.ToDouble(sizesFromUser[1]) }; ParallelogramAreaCalculator parallelogramAreaCalculator = new ParallelogramAreaCalculator(); resultOfCalculation.Figure = parallelogram; resultOfCalculation.Result = parallelogramAreaCalculator.AreaCalculator(parallelogram); break; case FiguresEN.Rhombus: Rhombus rhombus = new Rhombus() { Name = FiguresPL.Romb.ToString(), SizeA = Convert.ToDouble(sizesFromUser[0]), SizeH = Convert.ToDouble(sizesFromUser[1]) }; RhombusAreaCalculator rhombusAreaCalculator = new RhombusAreaCalculator(); resultOfCalculation.Figure = rhombus; resultOfCalculation.Result = rhombusAreaCalculator.AreaCalculator(rhombus); break; case FiguresEN.Trapeze: Trapeze trapeze = new Trapeze() { Name = FiguresPL.Trapez.ToString(), SizeA = Convert.ToDouble(sizesFromUser[0]), SizeB = Convert.ToDouble(sizesFromUser[1]), SizeH = Convert.ToDouble(sizesFromUser[2]) }; TrapezeAreaCalculator trapezeAreaCalculator = new TrapezeAreaCalculator(); resultOfCalculation.Figure = trapeze; resultOfCalculation.Result = trapezeAreaCalculator.AreaCalculator(trapeze); break; case FiguresEN.Triangle: Triangle triangle = new Triangle() { Name = FiguresPL.Trójkąt.ToString(), SizeA = Convert.ToDouble(sizesFromUser[0]), SizeH = Convert.ToDouble(sizesFromUser[1]) }; TriangleAreaCalculator triangleAreaCalculator = new TriangleAreaCalculator(); resultOfCalculation.Figure = triangle; resultOfCalculation.Result = triangleAreaCalculator.AreaCalculator(triangle); break; case FiguresEN.Circle: Circle circle = new Circle() { Name = FiguresPL.Koło.ToString(), Radius = Convert.ToDouble(sizesFromUser[0]) }; CircleAreaCalculator circleAreaCalculator = new CircleAreaCalculator(); resultOfCalculation.Figure = circle; resultOfCalculation.Result = circleAreaCalculator.AreaCalculator(circle); break; } } catch (FormatException) { resultOfCalculation.Exception = new FormatException(); resultOfCalculation.Successful = false; } catch (IndexOutOfRangeException) { resultOfCalculation.Exception = new IndexOutOfRangeException(); resultOfCalculation.Successful = false; } catch (NullReferenceException) { resultOfCalculation.Exception = new NullReferenceException(); resultOfCalculation.Successful = false; } return(resultOfCalculation); }