Ejemplo n.º 1
0
        static void Main()
        {
            string option = "";
            int valueInt;

            Program p = new Program();
            Calculator c = new Calculator();

            // TODO: Using the delegate you declared above for displaying
            //      a menu, create an anonymous method.  The code for the
            //      anonymous method is the code you copied in the
            //      DisplayMenu method at the previous TODO.
            DisplayMenuDelegate displayMenuDelegate = delegate()
            {
                Console.WriteLine("\nCalculator for Add ");
                Console.WriteLine("\n\t1. Add");
                Console.WriteLine("\tX. Exit Calculator");
                Console.Write("\nEnter option: ");
            };

            while (option.ToUpper() != "X")
            {
                // TODO:  Call the anonymous method stored in the delegate.
                displayMenuDelegate();

                // Get the option from the user.
                option = Console.ReadLine();
                Console.WriteLine();

                switch (option.ToUpper())
                {
                    case "1":
                        // Get numeric value.
                        valueInt = p.GetNumericValue();

                        // Use delegate method to call calculate method.
                        CalculateMethod cm = new CalculateMethod(c.Add);
                        cm(valueInt);

                        break;

                    case "X":
                        break;

                    default:
                        Console.WriteLine("Menu option {0} is not valid.",
                            option);
                        break;
                }
            }

            Console.WriteLine("\nTotal values: {0}",
                c.TotalValues);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        static void Main()
        {
            string option = "";
            int valueInt;

            Program p = new Program();
            Calculator c = new Calculator();

            while (option.ToUpper() != "X")
            {
                p.DisplayMenu();

                // Get the option from the user.
                option = Console.ReadLine();
                Console.WriteLine();

                switch (option.ToUpper())
                {
                    case "1":
                        // Get numeric value.
                        valueInt = p.GetNumericValue();

                        // Use delegate method to call calculate method.
                        // TODO: Load the Add method into the delegate you declared above.
                        ArithmeticMethod am = new ArithmeticMethod(c.Add);

                        // TODO: Call the delegate passing in the numeric value stored in the
                        //      the variable valueInt.
                        am(valueInt);

                        break;

                    case "X":
                        break;

                    default:
                        Console.WriteLine("Menu option {0} is not valid.",
                            option);
                        break;
                }
            }

            Console.WriteLine("\nTotal values: {0}",
                c.TotalValues);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        static void Main()
        {
            string option = "";

            // TODO:  Take note that the CalculateMethod delegate is being
            //      declared here.  It will be loaded in the TODOs below.  Its
            //      reference name is "cm".
            CalculateMethod cm;

            Program p = new Program();
            Calculator c = new Calculator();

            // Declare an anonymous method.
            DisplayMenu menu = delegate
            {
                Console.WriteLine("\nCalculator for Add and Subtract");
                Console.WriteLine("\n\t1. Add");
                Console.WriteLine("\t2. Subtract");
                Console.WriteLine("\tX. Exit Calculator");
                Console.Write("\nEnter option: ");
            };

            // TODO: Declare the CalculationComplete delegate and load it with
            //      the CalculationCompleteCallback method. Give the reference
            //      name of the delegate "cc".

            while (option.ToUpper() != "X")
            {
                // Call the anonymous method.
                menu();

                // Get the option from the user.
                option = Console.ReadLine();
                Console.WriteLine();

                switch (option.ToUpper())
                {
                    case "1":

                        // Load delegate method to call Add method.
                        // TODO: Load the already declared CalculateMethod delegate (name is cm)
                        //      with the Add method in the Calculator class.

                        // TODO:  Take note that the CallCalculator method in the Calculator class is being
                        //      called here to pass in the two loaded delegates for the calculate
                        //      method and the callback method.
                        c.CallCalculator(cm, cc);

                        break;

                    case "2":

                        // Load delegate method to call Subtract method.
                        // TODO: Load the already declared CalculateMethod delegate (name is cm)
                        //      with the Subtract method in the Calculator class.

                        // TODO:  Take note that the CallCalculator method in the Calculator class is being
                        //      called here to pass in the two loaded delegates for the calculate
                        //      method and the callback method.
                        c.CallCalculator(cm, cc);

                        break;

                    case "X":
                        break;

                    default:
                        Console.WriteLine("Menu option {0} is not valid.",
                            option);
                        break;
                }
            }

            Console.WriteLine("\nTotal values: {0}",
                c.TotalValues);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
Ejemplo n.º 4
0
        static void Main()
        {
            string option = "";

            CalculateMethod cm = null;     // Delegate for calculation methods.

            Program p = new Program();
            Calculator c = new Calculator();

            // Declare an anonymous method.
            DisplayMenu menu = delegate
            {
                Console.WriteLine("\nCalculator for Add and Subtract");
                Console.WriteLine("\n\t1. Add");
                Console.WriteLine("\t2. Subtract");
                Console.WriteLine("\tX. Exit Calculator");
                Console.Write("\nEnter option: ");
            };

            // Declare the CalculationComplete delegate and load it with
            //      the CalculationCompleteCallback method.
            CalculationComplete cc = new CalculationComplete(p.CalculationCompleteCallback);

            while (option.ToUpper() != "X")
            {
                // Call the anonymous method.
                menu();

                // Get the option from the user.
                option = Console.ReadLine();
                Console.WriteLine();

                switch (option.ToUpper())
                {
                    case "1":

                        // Load multicast delegate to call Add method.
                        // TODO: Include the Add method in the multicast
                        //      CalculateMethod delegate.

                        // Loads a numeric value into a collection to be used
                        //       by the method when it is executed.
                        c.values.Add(c.GetNumericValue());

                        break;

                    case "2":

                        // Load delegate to call Subtract method.
                        // TODO: Include the Subtract method in the multicast
                        //      CalculateMethod delegate.

                        // Loads a numeric value into a collection to be used
                        //       by the method when it is executed.
                        c.values.Add(c.GetNumericValue());

                        break;

                    case "X":
                        break;

                    default:
                        Console.WriteLine("Menu option {0} is not valid.",
                            option);
                        break;
                }
            }

            // Call the method in the Calculator class
            //      to pass in the two loaded delegates for the calculate
            //      methods and the callback method.  Do this ONLY if
            //      the multicast CalculateMethod delegate has any calculate methods
            //      loaded into it.
            if (cm == null)
            {
                Console.WriteLine("\nNo calculations were requested.");
            }
            else
            {
                c.CallCalculator(cm, cc);
            }

            Console.WriteLine("\nTotal values: {0}",
                c.TotalValues);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }