Example #1
0
        public static void Main()
        {
            ShapeFactory shapeCreator = new ShapeFactory();

            for (int i = 0; i < 5; i++)
            {
                if (shapeCreator.getShape((ShapeType)i) != null)
                {
                    shapeCreator.getShape((ShapeType)i).draw();
                }
                else
                {
                    Console.WriteLine("Requested shape not created!");
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            int          width        = 2;
            int          height       = 2;
            ShapeFactory shapeFactory = new ShapeFactory();

            Console.WriteLine("Welcome to the Shape Factory");
            Console.WriteLine("Building circle...");
            IShape shape;

            try
            {
                shape = shapeFactory.GetShape("Circle");
                Console.WriteLine($"Area of Circle: {shape.GetArea(height, width)}\nCircumference of Circle: {shape.GetPerimeter(height, width)}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("Building Square...");
            try
            {
                shape = shapeFactory.GetShape("Square");
                Console.WriteLine($"Area of Square: {shape.GetArea(height, width)}\nPerimeter of Square: {shape.GetPerimeter(height, width)}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("Building Hexagon...");
            try
            {
                shape = shapeFactory.GetShape("Hexagon");
                Console.WriteLine($"Area of Hexagon: {shape.GetArea(height, width)}\nPerimeter of Hexagon: {shape.GetPerimeter(height, width)}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            List <IShape> shapes = new List <IShape>();

            IShapeFactory factory = new ShapeFactory();

            foreach (var shapeName in factory.AvailableShapes)
            {
                var shape = factory.Create(shapeName);
                shapes.Add(shape);
            }

            foreach (var shape in shapes)
            {
                shape.Draw();
            }

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            //This is the object that will make the shapes.
            ShapeFactory fact = new ShapeFactory();
            //This is a shape, at first the shape will be line.
            IGeometricShape shape = fact.getShape((ShapeFactory.ShapeType) 0);

            shape.draw();

            for (int i = 1; i < Enum.GetValues(typeof(ShapeFactory.ShapeType)).Length; i++)
            {
                shape = fact.getShape((ShapeFactory.ShapeType)i);
                //if shape name does not exist.
                if (shape == null)
                {
                    Console.WriteLine("SHAPE " + (ShapeFactory.ShapeType)i + " NOT FOUND.");
                }
                else
                {
                    shape.draw();
                }
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            double AreaSum = 0;
            Random random  = new Random();
            int    i       = 0;

            while (i < 10)
            {
                int   temp  = random.Next(0, 3);
                int   a     = random.Next(1, 100);
                int   b     = random.Next(1, 100);
                int   c     = random.Next(1, 100);
                Shape shape = ShapeFactory.Create(temp, a, b, c);   //工厂创建对象
                if (shape.IsLegel() == false)
                {
                    continue;    //当图形不合法时,重新生成图形
                }
                Console.WriteLine("第" + (i + 1) + "个图形的面积是:" + shape.Area());
                AreaSum = AreaSum + shape.Area();
                i       = i + 1;
            }
            Console.WriteLine("10个图形的面积之和是:" + AreaSum);
        }
Example #6
0
        static void Main(string[] args)
        {
            for (int i = 1; i < 5; i++)
            {
                ShapeType type;
                Console.WriteLine("What shape would you like?\n" +
                                  "1. Line\n" +
                                  "2. Circle\n" +
                                  "3. Rectangle\n" +
                                  "4. Triangle\n");
                type = (ShapeType)i;

                ShapeFactory factory = new ShapeFactory();
                if (factory.getShape(type) == null)
                {
                    Console.WriteLine("Shape not found.\n");
                }
                else
                {
                    factory.getShape(type).draw();
                }
            }
        }