public static IOperation Create(decimal firstNumber, decimal secondNumber, string operatorValue)
        {
            IOperation operation = null;

            switch (operatorValue)
            {
            case OperatorSymbols.Addition:
                operation = new SumOperation(firstNumber, secondNumber);
                break;

            case OperatorSymbols.Substraction:
                operation = new SubstractOperation(firstNumber, secondNumber);
                break;

            case OperatorSymbols.Multiplication:
                operation = new MultiplyOperation(firstNumber, secondNumber);
                break;

            case OperatorSymbols.Division:
                operation = new DivideOperation(firstNumber, secondNumber);
                break;

            default:
                throw new InvalidOperationException("Invalid operator");
            }
            return(operation);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            bool            exitLoop = true;
            bool            isInputCorrect;
            IMathOperations mathOperations;

            do
            {
                Console.WriteLine("Choose operation:");
                Console.WriteLine("1. Add.");
                Console.WriteLine("2. Subtract.");
                Console.WriteLine("3. Multiply.");
                Console.WriteLine("4. Divide.");
                Console.WriteLine("0. Quit.\n");
                isInputCorrect = int.TryParse(Console.ReadLine(), out int caseChoice);
                if (isInputCorrect)
                {
                    mathOperations = new NullOperation();

                    switch (caseChoice)
                    {
                    case 1:
                        mathOperations = new SumOperation();
                        break;

                    case 2:
                        mathOperations = new SubtractionOperation();
                        break;

                    case 3:
                        mathOperations = new MultiplyOperation();
                        break;

                    case 4:
                        mathOperations = new DivideOperation();
                        break;

                    case 0:
                        exitLoop = false;
                        break;

                    default:
                        PrintNoOptionSelected();
                        break;
                    }
                    mathOperations.MathOperation();
                }
                else
                {
                    PrintNoOptionSelected();
                }
            } while (exitLoop);
        }