Exemplo n.º 1
0
        public void ReadString(string pCommand)
        {
            string[] arrCommands = pCommand.Split(' ');
            switch (arrCommands[0].ToLower())
            {
            case "create":
                if (arrCommands.Length > 1)
                {
                    switch (arrCommands[1].ToLower())
                    {
                    case "square":
                        Square square = new Square();
                        square.Side = double.Parse(arrCommands[2]);
                        this.Add(square);
                        this.CalculateSurfaceAreas();
                        Console.WriteLine("{0} created!", square.GetType().Name);
                        break;

                    case "circle":
                        Circle circle = new Circle();
                        circle.Radius = double.Parse(arrCommands[2]);
                        this.Add(circle);
                        this.CalculateSurfaceAreas();
                        Console.WriteLine("{0} created!", circle.GetType().Name);
                        break;

                    case "triangle":
                        Triangle triangle = new Triangle();
                        triangle.Height = double.Parse(arrCommands[2]);
                        triangle.Width  = double.Parse(arrCommands[3]);
                        this.Add(triangle);
                        this.CalculateSurfaceAreas();
                        Console.WriteLine("{0} created!", triangle.GetType().Name);
                        break;

                    case "rectangle":
                        Rectangle rectangle = new Rectangle();
                        rectangle.Height = double.Parse(arrCommands[2]);
                        rectangle.Width  = double.Parse(arrCommands[3]);
                        this.Add(rectangle);
                        this.CalculateSurfaceAreas();
                        Console.WriteLine("{0} created!", rectangle.GetType().Name);
                        break;

                    default:
                        goto ShowCommands;
                    }
                }
                else
                {
                    ShowCommands();
                }
                this.ReadString(Console.ReadLine());
                break;

            case "calculate":
                this.CalculateSurfaceAreas();
                this.ReadString(Console.ReadLine());
                break;

            case "print":
                if (arrSurfaceAreas != null)
                {
                    for (int i = 0; i < arrSurfaceAreas.Length; i++)
                    {
                        Console.WriteLine("[{0}] {1} surface area is {2}", i, arrObjects[i].GetType().Name, arrSurfaceAreas[i]);
                    }
                }
                else
                {
                    Console.WriteLine("There are no surface areas to print");
                }
                this.ReadString(Console.ReadLine());
                break;

            case "reset":
                this.arrSurfaceAreas = null;
                this.arrObjects      = null;
                Console.WriteLine("Reset state!!");
                this.ReadString(Console.ReadLine());
                break;

            case "exit":
                break;

            default:
ShowCommands:
                this.Logger.Log("Unknown command!!!");
                this.Logger.Log("commands:");
                this.Logger.Log("- create square {double} (create a new square)");
                this.Logger.Log("- create circle {double} (create a new circle)");
                this.Logger.Log("- create rectangle {height} {width} (create a new rectangle)");
                this.Logger.Log("- create triangle {height} {width} (create a new triangle)");
                this.Logger.Log("- print (print the calculated surface areas)");
                this.Logger.Log("- calculate (calulate the surface areas of the created shapes)");
                this.Logger.Log("- reset (reset)");
                this.Logger.Log("- exit (exit the loop)");
                this.ReadString(Console.ReadLine());
                break;
            }
        }
Exemplo n.º 2
0
 public bool ShouldPaint(Triangle triangle)
 {
     return(Math.Abs(triangle.GetArea()) < 0.001);
 }