Beispiel #1
0
        public string RunCorrectApproach()
        {
            var explanation = new StringBuilder();

            explanation.AppendLine($"--- CORRECT APPROACH ---");

            var rectangle = new CorrectApproach.Rectangle {
                Height = 10, Width = 3
            };

            explanation.AppendLine($"We have created a rectangle of 10x3: Height[{rectangle.Height}] Width[{rectangle.Width}] Area[{rectangle.GetArea()}]");

            var square = new CorrectApproach.Square {
                SideLength = 10
            };

            explanation.AppendLine($"And a square of 10x10: Size of sides[{ square.SideLength}] Area[{ square.GetArea()}]");

            var approach      = new CorrectApproach();
            var rectangleTest = approach.ValidateAreaCalculation(rectangle, 30);
            var squareTest    = approach.ValidateAreaCalculation(square, 100);

            explanation.AppendLine($"Using both through their interface, we test that GIVEN sides of 10x3 rectangle WHEN we get the area THEN we get 30");
            explanation.AppendLine($"\tRectangle object test: {(rectangleTest ? "passed" : "NOT passed")}");
            explanation.AppendLine($"...and that GIVEN a 10x10 square WHEN we get the area THEN we get 100");
            explanation.AppendLine($"\tSquare object test: {(squareTest ? "passed" : "NOT passed")}");
            explanation.AppendLine();

            return(explanation.ToString());
        }
        public string RunCorrectApproach()
        {
            var explanation = new StringBuilder();

            explanation.AppendLine("--- CORRECT APPROACH ---");
            explanation.AppendLine("Getting all items using a specific interfaces will allow to use objects with the needed properties");

            var wrongApproach = new CorrectApproach();
            List <CorrectApproach.SellableItem> items = wrongApproach.GetAllProducts();

            var vegetableProducts        = items.Where(x => x.MeasurementUnit == "Kg").ToList();
            var ageragePriceOfVegetables = vegetableProducts.Average((v) => v.Price);

            explanation.AppendLine("In this approach there are:");
            explanation.AppendLine($"\t{vegetableProducts.Count} vegetables products with an average price of {ageragePriceOfVegetables}€/Kg");

            var tvProducts           = items.Where(x => x.Name.ToLower().Contains("tv")).Select(x => (CorrectApproach.PixelMeasureable)x).ToList();
            var differentResolutions = tvProducts.Select(tv => tv.Resolution).Distinct().Count();

            explanation.AppendLine($"\t{tvProducts.Count} TVs of {differentResolutions} different resolutions");

            var schollItems = items
                              .Where(x => "Clothing4Youth,WealthyLife,LearningSolutions".ToLower().Contains(x.ProviderName.ToLower())).Select(x => (CorrectApproach.Sizable)x).ToList();
            var shoesItems = schollItems.Where(i => i.MeasurementUnit == "pair").Count();

            explanation.AppendLine($"\t{schollItems.Count} school items, and {shoesItems} of them are shoes");

            string vegetableProperties = vegetableProducts.First().GetType().GetInterface("SellableItem").GetProperties().Select(p => p.Name).Aggregate((name, nextName) => $"{name}, {nextName}");

            explanation.AppendLine("Vegetable visible properties: " + vegetableProperties);

            return(explanation.ToString());
        }