Exemple #1
0
        static void Main()                                                                    // 100/100 - Пример за DependencyInversion с ctor Injection!!!
        {
            PrimitiveCalculator calculator = new PrimitiveCalculator(new AdditionStrategy()); // default strategy!

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                string[] tokens  = input.Split();
                string   command = tokens[0];

                if (command == "mode")
                {
                    char @operator = tokens[1][0];

                    ICalculationStrategy strategy = null;

                    switch (@operator)
                    {
                    case '+':
                        strategy = new AdditionStrategy();
                        break;

                    case '-':
                        strategy = new SubtractionStrategy();
                        break;

                    case '*':
                        strategy = new MultyplicationStrategy();
                        break;

                    case '/':
                        strategy = new DivisionStrategy();
                        break;
                    }

                    if (strategy == null)
                    {
                        throw new ArgumentException("Inavalid mode!");
                    }

                    calculator.ChangeStrategy(strategy);
                }
                else
                {
                    int firstOperand  = int.Parse(tokens[0]);
                    int secondOperand = int.Parse(tokens[1]);

                    int result = calculator.PerformCalculation(firstOperand, secondOperand);

                    Console.WriteLine(result);
                }
            }
        }
    static void Main(string[] args)
    {
        PrimitiveCalculator calculator = new PrimitiveCalculator(new AdditionStrategy());

        string input;

        while ((input = Console.ReadLine()) != "End")
        {
            string[] tokens   = input.Split();
            string   firstArg = tokens[0];

            if (firstArg == "mode")
            {
                char operand = tokens[1][0];
                ICalculationStrategy strategy = null;
                switch (operand)
                {
                case '+':
                    strategy = new AdditionStrategy();
                    break;

                case '-':
                    strategy = new SubtractionStrategy();
                    break;

                case '*':
                    strategy = new MultyplicationStrategy();
                    break;

                case '/':
                    strategy = new DivideStrategy();
                    break;
                }
                if (strategy == null)
                {
                    throw new InvalidOperationException("Invalid Mode!");
                }
                calculator.ChangeStrategy(strategy);
            }
            else
            {
                int first  = int.Parse(firstArg);
                int second = int.Parse(tokens[1]);

                int result = calculator.PerformCalculation(first, second);

                Console.WriteLine(result);
            }
        }
    }
Exemple #3
0
        public static void Main()
        {
            var input      = Console.ReadLine();
            var calculator = new PrimitiveCalculator();

            while (input != "End")
            {
                string[] inputArgs = input.Split();
                if (inputArgs[0] == "mode")
                {
                    switch (inputArgs[1][0])
                    {
                    case '/':
                        var devision = new DevisionStrategy();
                        calculator.ChangeStrategy(devision);
                        break;

                    case '*':
                        var multyplication = new MultyplicationStrategy();
                        calculator.ChangeStrategy(multyplication);
                        break;

                    case '-':
                        var substraction = new SubstractionStrategy();
                        calculator.ChangeStrategy(substraction);
                        break;

                    case '+':
                        var addition = new AdditionStrategy();
                        calculator.ChangeStrategy(addition);
                        break;
                    }
                }
                else
                {
                    int firstOperand  = int.Parse(inputArgs[0]);
                    int secondOperand = int.Parse(inputArgs[1]);

                    int result = calculator.PerformCalculation(firstOperand, secondOperand);

                    Console.WriteLine(result);
                }

                input = Console.ReadLine();
            }
        }