예제 #1
0
        public void TestCircleValue()
        {
            FiguresFactory factory = new FiguresFactory();
            IFigure        figure  = factory.GetFigure("circle");

            var jparams = JToken.Parse(@"{ 'radius': 10 }");

            figure.SetParamsFromJson(jparams);

            decimal expected = 314.15926535897900m;
            decimal area     = figure.CalculateArea();

            Assert.Equal(expected, area, 25);
        }
예제 #2
0
        public void TestTriangleValue()
        {
            FiguresFactory factory = new FiguresFactory();
            IFigure        figure  = factory.GetFigure("triangle");

            var jparams = JToken.Parse(@"{ 'a': 10, 'b': 10, 'c': 10 }");

            figure.SetParamsFromJson(jparams);

            decimal expected = 43.30127018922193233818615854m;
            decimal area     = figure.CalculateArea();

            Assert.Equal(expected, area, 25);
        }
예제 #3
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello! It's batch calculator.");

            if (args.Length != 1)
            {
                System.Console.WriteLine("Usage: Program.exe <input json file>");
                return;
            }

            decimal totalArea = 0m;
            FiguresFactory figuresFactory = new FiguresFactory();

            using (StreamReader file = File.OpenText(args[0]))
            {
                JsonTextReader reader = new JsonTextReader(file);
                var jtoken = JToken.Load(reader);

                if (jtoken.Type != JTokenType.Array)
                {
                    System.Console.WriteLine("Error! File must contain json array");
                    return;
                }

                var jarray = (JArray)jtoken;

                foreach (JToken jtokenFigure in jarray)
                {
                    if (jtokenFigure.Type != JTokenType.Object)
                    {
                        System.Console.WriteLine("Error! Each element of array must contain object");
                    }

                    var jfigure = (JObject)jtokenFigure;

                    string figureName = jfigure.Value<string>("figure");

                    if (figureName == null)
                    {
                        System.Console.WriteLine("Error! Figure object must contain figure key");
                        return;
                    }

                    IFigure figure = figuresFactory.GetFigure(figureName);

                    if (figure == null)
                    {
                        System.Console.WriteLine("Error! Unknown figure");
                        return;
                    }

                    var jtokenParams = jfigure["params"];

                    figure.SetParamsFromJson(jtokenParams);

                    System.Console.WriteLine(figure.ToString());

                    decimal area = figure.CalculateArea();

                    System.Console.WriteLine("Area = " + area);
                    System.Console.WriteLine();

                    totalArea += area;
                }
            }

            System.Console.WriteLine("TotalArea = " + totalArea);

        }
예제 #4
0
        public IActionResult CalculateArea([FromBody] JToken jsonData)
        {
            if (jsonData.Type != JTokenType.Object)
            {
                return(BadRequest(new
                {
                    error = "Figure must be object"
                }));
            }

            var jfigure = (JObject)jsonData;

            string figureName = jfigure.Value <string>("figure");

            if (figureName == null)
            {
                return(BadRequest(new
                {
                    error = "Figure object must contain figure key"
                }));
            }

            FiguresFactory figuresFactory = new FiguresFactory();
            IFigure        figure         = figuresFactory.GetFigure(figureName);

            if (figure == null)
            {
                return(BadRequest(new
                {
                    error = "Unknown figure type"
                }));
            }

            var jtokenParams = jfigure["params"];

            try
            {
                figure.SetParamsFromJson(jtokenParams);
            }
            catch
            {
                return(BadRequest(new
                {
                    error = "Invalid figure arguments"
                }));
            }

            decimal area = 0m;

            try
            {
                area = figure.CalculateArea();
            }
            catch (System.ArgumentException)
            {
                return(BadRequest(new
                {
                    error = "Invalid figure arguments"
                }));
            }
            catch (System.OverflowException)
            {
                return(BadRequest(new
                {
                    error = "Overflow caused during calculation. Arguments is too big"
                }));
            }

            return(Ok(new { area = area }));
        }