コード例 #1
0
        public void TestCompoundShapeMath()
        {
            // arrange
            // manually calc sum of perimeters - line + circle + line
            double expectedPerimeterSum = 1 + 18.8495559215387594;
            // manually calc sum of areas - line + circle + line
            double expectedAreaSum = 0 + 28.274333882308139146;

            _compoundShape.Add(_line);
            _compoundShape.Add(_circle);

            // act
            double actualPerimeterSum = _compoundShape.GetPerimeter();
            double actualAreaSum      = _compoundShape.GetArea();

            // assert
            Assert.AreEqual(expectedPerimeterSum, actualPerimeterSum);
            Assert.AreEqual(expectedAreaSum, actualAreaSum);
        }
コード例 #2
0
        public void TestCompoundShapeMock()
        {
            // allocate
            double expectedAreaSum = 8;

            var mockShape1 = new Mock <IShapeComposition>();
            var mockShape2 = new Mock <IShapeComposition>();

            mockShape1.Setup(shape => shape.GetArea()).Returns(mockValue1);
            mockShape2.Setup(shape => shape.GetArea()).Returns(mockValue2);

            // act
            _compoundShape.Add(mockShape1.Object);
            _compoundShape.Add(mockShape2.Object);

            double actualAreaSum = _compoundShape.GetArea();

            // assert
            Assert.AreEqual(expectedAreaSum, actualAreaSum);
        }
コード例 #3
0
        public void TestCompoundShapeAdd()
        {
            // arrange
            CompoundShape compoundShape = new CompoundShape(7, 7);

            // act
            compoundShape.Add(new Line(3, 4, 5, 6));

            // assert
            Assert.AreEqual(expectedOneCount, compoundShape.Count);
        }
コード例 #4
0
        static void Main(string[] args)
        {
            Dot       dot       = new Dot(1, 10, 55);
            Circle    circle    = new Circle(2, 23, 15, 10);
            Rectangle rectangle = new Rectangle(3, 10, 17, 20, 30);

            CompoundShape compoundShape = new CompoundShape(4);

            compoundShape.Add(dot);
            compoundShape.Add(circle);
            compoundShape.Add(rectangle);

            CompoundShape c = new CompoundShape(5);

            c.Add(dot);
            compoundShape.Add(c);

            Export(circle, compoundShape);

            Console.ReadKey();
        }
コード例 #5
0
        static void Main(string[] args)
        {
            // naming conventions
            // https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/general-naming-conventions?redirectedfrom=MSDN
            // https://github.com/ktaranov/naming-convention/blob/master/C%23%20Coding%20Standards%20and%20Naming%20Conventions.md

            Console.WriteLine("Hello World!");

            // different types of string concatenation & interpolation
            string someStringValue      = "some value";
            string anotherStringValue   = "another value";
            string differentStringValue = "different value";

            string doNotConcatStringLikeThis = "Use this to format " + someStringValue + " with " + anotherStringValue + " and " + differentStringValue;
            string concatString      = string.Concat(someStringValue, anotherStringValue, differentStringValue);
            string formatString      = string.Format("Use this to format {0} with {1} and {2}", someStringValue, anotherStringValue, differentStringValue);
            string interpolateString = $"Use this to format {someStringValue} with {anotherStringValue} and {differentStringValue}";

            Console.WriteLine("-----");

            // class members
            Console.WriteLine(ClassWithMembers.StaticString);
            ClassWithMembers classWithMembers = new ClassWithMembers();

            classWithMembers.ChangeSomeValues(10);
            classWithMembers.WriteSomething();

            Console.WriteLine("-----");

            // Inheritance & Basic Polymorphism
            BaseClass    bc   = new BaseClass();
            DerivedClass dc   = new DerivedClass();
            BaseClass    bcdc = new DerivedClass();

            bc.Method1();
            bc.Method2();

            dc.Method1();
            dc.Method2();

            // runtime polymorphism
            // calls method1 of derived class because of virtual/override
            bcdc.Method1();
            // calls method2 of base class, because of new keyword (method hiding)
            bcdc.Method2();
            // calls overloaded method2 of derived class because of casting
            ((DerivedClass)bcdc).Method2("sdf");

            Console.WriteLine("-----");

            // Polymorphism with abstract base class
            AbstractShape abstractLine = new DerivedLine(0, 1, 1, 1);

            abstractLine.ShowOrigin();
            double abstractLinePerimeter = abstractLine.GetPerimeter();

            AbstractShape abstractCircle = new DerivedCircle(5, 5, 3);

            abstractCircle.ShowOrigin();
            double abstractCircleArea = abstractCircle.GetArea();

            DerivedCompoundShape derivedCompound = new DerivedCompoundShape(7, 7);

            derivedCompound.Add(abstractLine);
            derivedCompound.Add(abstractCircle);
            derivedCompound.Add(new DerivedLine(3, 4, 5, 6));
            derivedCompound.ShowOrigin();
            derivedCompound.PrintShapeType();

            // casting works in IDE, but will throw an error at runtime if "line" is smth else than a Line obje
            //AbstractShape shape = new DerivedCircle(0, 0, 1);
            //DerivedLine line3 = (DerivedLine)shape;

            Console.WriteLine("-----");

            // Polymorphism with interfaces
            IBetterShapeComposition betterLine = new BetterLine(0, 1, 1, 1);

            betterLine.ShowOrigin();
            double betterLinePerimeter = betterLine.GetPerimeter();

            IBetterShapeComposition betterCircle = new BetterCircle(5, 5, 3);
            double betterCircleArea = betterCircle.GetArea();

            betterCircle.ShowOrigin();

            BetterCompoundShape betterCompound = new BetterCompoundShape(7, 7);

            betterCompound.Add(betterLine);
            betterCompound.Add(betterCircle);
            betterCompound.Add(new BetterLine(3, 4, 5, 6));
            betterCompound.ShowOrigin();
            betterCompound.PrintShapeType();

            Console.WriteLine("-----");

            // Very good solution for this usecase - Polymorphism with interfaces and a base class
            Line line = new Line(0, 1, 1, 1);

            line.ShowOrigin();
            double linePerimeter = line.GetPerimeter();

            Circle circle     = new Circle(5, 5, 3);
            double circleArea = betterCircle.GetArea();

            betterCircle.ShowOrigin();

            CompoundShape compoundShape = new CompoundShape(7, 7);

            compoundShape.Add(line);
            compoundShape.Add(circle);
            compoundShape.Add(new Line(3, 4, 5, 6));
            compoundShape.ShowOrigin();
            compoundShape.PrintShapeType();

            Console.WriteLine("-----");

            IShapeStack shapeStack = new ShapeStack(5);

            shapeStack.Push(line);
            Console.WriteLine($"Is empty? {shapeStack.IsEmpty()}");

            if (!shapeStack.IsEmpty())
            {
                shapeStack.DescribeStack();
            }

            Console.WriteLine("-----");

            IShapeComposition poppedShape = shapeStack.Pop();

            Console.WriteLine("Following shape was popped from stack:");
            poppedShape.PrintShapeType();
            Console.WriteLine($"Is stack empty? {shapeStack.IsEmpty()}");

            Console.WriteLine("-----");
        }