예제 #1
0
 /// <summary>
 /// Constructor. Creates 3D shapes.
 /// </summary>
 /// <param name="shapeType">Type of shape to be created</param>
 /// <param name="baseShape">The 2D shape to base the 3D shape on</param>
 /// <param name="height">The height of the shape</param>
 protected Shape3D(ShapeType shapeType, Shape2D baseShape, double height)
     : base(shapeType)
 {
     _baseShape = baseShape;
     Height     = height;
 }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="howMany">This generates how many shapes are going to be created in the process</param>
        /// <param name="is3D">False gives us 2D, True gives us 3D</param>
        static void createAllTheShapes(int howMany, bool is3D)
        {
            /// <If2D>Cuboid, Cylinder and Sphere by random choice</if2D>
            if (is3D == true)
            {
                List <Shape3D> shapes3D = new List <Shape3D>();
                Shape3D        shape3D;
                for (int i = 0; i < howMany; i++)
                {
                    switch (randomer.Next(0, 3))
                    {
                    case 0:
                        shape3D = new Cuboid(randomer.Next(5, 100), randomer.Next(5, 100), randomer.Next(5, 100));
                        shapes3D.Add(shape3D);
                        break;

                    case 1:
                        shape3D = new Cylinder(randomer.Next(5, 100), randomer.Next(5, 100), randomer.Next(5, 100));
                        shapes3D.Add(shape3D);
                        break;

                    case 2:
                        shape3D = new Sphere(randomer.Next(5, 100));
                        shapes3D.Add(shape3D);
                        break;
                    }
                }
                /// <summary>
                /// For Array = Sort by shapetype in order
                /// For List = Sort by shapetype and volume
                /// </summary>
                /// For the Array
                ///<alternative!>GetType</alternative>
                /// shapes3D.OrderBy( x => x.GetType().Name).ThenByDescending(x => x.Volume).ToArray();
                ///<alternative!>CompareTo</alternative>
                // Array.Sort(shapes, (x, y) => x.shapeType.CompareTo(y.shapeType));
                ///<alternative!>another alternative</alternative>
                // shapes3D.OrderBy(o=>o.shapeType).ThenBy (o => o.Volume).ToList();

                List <Shape3D> Shape3DSorted = new List <Shape3D>(shapes3D.OrderBy(sz => sz.shapeType.ToString())
                                                                  .ThenBy(sx => sx.Volume)
                                                                  .ToArray());

                // Note To Self // Store Linq In Variable

                foreach (var shape in Shape3DSorted)
                {
                    Console.WriteLine(shape);
                }
            }
            /// <If2D> Ellipse and Rectangle by randomers choice </if2D>
            if (is3D == false)
            {
                Shape2D[] sortedShapes;
                Shape2D[] shape2D = new Shape2D[howMany];

                for (int i = 0; i < howMany; i++)
                {
                    switch (randomer.Next(0, 2))
                    {
                    case 0:
                        shape2D[i] = new Rectangle(randomer.Next(5, 100), randomer.Next(5, 100));
                        break;

                    case 1:
                        shape2D[i] = new Ellipse(randomer.Next(5, 100), randomer.Next(5, 100));
                        break;
                    }
                }
                /// <summary>
                /// Sort by name and then by area
                /// </summary>

                sortedShapes = shape2D.OrderBy(x => x.shapeType).ThenBy(y => y.Area).ToArray();

                /// <Extra> In Case Cast Is Required </Extra>
                // shape2D.Cast<Shape2D>().OrderBy(ls => ls.GetType().Name);

                foreach (var shape in sortedShapes)
                {
                    Console.WriteLine(shape);
                }
            }
        }