Example #1
0
            public static void MainMenu()
            {
                WriteLine("Welcome to the Math assignment in C Sharp!");
                WriteLine("Enter 1 to build a rectangle, 2 for a square, 3 for a triangle, 4 for an octagon or 5 for a times table.");
                WriteLine("Or enter 0 to exit.");
                var answer = ReadLine();

                if (answer == "0")
                {
                    WriteLine("Goodbye!");
                    Environment.Exit(0);
                }
                else if (answer == "1")
                {
                    WriteLine("Enter the length of your rectangle: ");
                    double length = 0;
                    double width  = 0;
                    try
                    {
                        length = Convert.ToDouble(ReadLine());
                    }
                    catch (SystemException)
                    {
                        WriteLine("Error! Invalid value. Going back to main menu...");
                        Menu.MainMenu();
                    }
                    WriteLine("Enter the width of your rectangle: ");
                    try
                    {
                        width = Convert.ToDouble(ReadLine());
                    }
                    catch (SystemException)
                    {
                        WriteLine("Error! Invalid value. Going back to main menu...");
                        Menu.MainMenu();
                    }

                    Rectangle rectangle = new Rectangle(length, width);

                    WriteLine("The area of your rectangle is " + rectangle.GetArea());
                    rectangle.AcceptDetails();
                    rectangle.Display();
                    rectangle.DisplayInfo();
                    Menu.MainMenu();
                }
                else if (answer == "2")
                {
                    WriteLine("Enter the length and width of your square: ");
                    double length = 0;
                    try
                    {
                        length = Convert.ToDouble(ReadLine());
                    }
                    catch (SystemException)
                    {
                        WriteLine("Error. Invalid value specified. Returning to main menu...");
                        Menu.MainMenu();
                    }

                    var width = length;

                    Square square = new Square(length, width);

                    WriteLine("The area of your rectangle is " + square.GetArea());
                    square.AcceptDetails();
                    square.Display();
                    Menu.MainMenu();
                }
                else if (answer == "3")
                {
                    WriteLine("Enter the length of your triangle: ");
                    double length = 0;
                    double width  = 0;
                    try
                    {
                        length = Convert.ToDouble(ReadLine());
                    }
                    catch (SystemException)
                    {
                        WriteLine("Error! Invalid value. Going back to main menu...");
                        Menu.MainMenu();
                    }
                    WriteLine("Enter the width of your triangle: ");
                    try
                    {
                        width = Convert.ToDouble(ReadLine());
                    }
                    catch (SystemException)
                    {
                        WriteLine("Error! Invalid value. Going back to main menu...");
                        Menu.MainMenu();
                    }

                    Triangle triangle = new Triangle(length, width);

                    WriteLine("The area of your triangle is " + triangle.GetArea());
                    triangle.AcceptDetails();
                    triangle.Display();
                    Menu.MainMenu();
                }
                else if (answer == "4")
                {
                    WriteLine("Enter the side length of your octagon: ");
                    double sidelength = 0;
                    try
                    {
                        sidelength = Convert.ToDouble(ReadLine());
                    }
                    catch (SystemException)
                    {
                        WriteLine("Error! Invalid value. Going back to main menu...");
                        Menu.MainMenu();
                    }

                    Octagon octagon = new Octagon(sidelength);

                    octagon.AcceptDetails();
                    octagon.Display();
                    Menu.MainMenu();
                }
                else if (answer == "5")
                {
                    WriteLine("Please enter your value for a times table.");
                    double value = 0;
                    try
                    {
                        value = Convert.ToDouble(ReadLine());
                    }
                    catch (SystemException)
                    {
                    }
                    TimesTable table = new TimesTable(value);
                    table.Display();
                    Menu.MainMenu();
                }
                else
                {
                    WriteLine("Invalid response! Try again.");
                    Menu.MainMenu();
                }
            }