Ejemplo n.º 1
0
        public ActionResult CalculationForm(CalculationViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //Передаем ViewModel в презентер и получаем измененную ViewModel
                    model.OperationResult = presenter.Calculation(model);
                }
                catch (Exception e)
                {
                    //выводим сообщение об ошибке
                    return RedirectToAction("ShowError","Error", e.Message);
                }
            }

            return View(model);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Вычисление
        /// </summary>
        /// <param name="model">ViewModel для presenter</param>
        /// <returns>результат операции (строка)</returns>
        public string Calculation(CalculationViewModel model)
        {
            //проверили и типизировали данные из модели
            var argument1 = Converter.IntFromString(model.Argument1,
                "Недопустимое значение первого аргумента.");
            var argument2 = Converter.IntFromString(model.Argument2,
                "Недопустимое значение второго аргумента.");
            var operationType = Converter.OperationTypeFromString(model.SelectedOperationType,
                "Не выбран тип операции.");
            //вызвали службу и заполнили модель результатом операции
            decimal operationResult;
            switch (operationType)
            {
                //Сложение
                case OperationTypes.Addition:
                    operationResult = calculationService.Addition(argument1, argument2);
                    break;
                //Вычитание
                case OperationTypes.Subtraction:
                    operationResult = calculationService.Subtraction(argument1, argument2);
                    break;
                //Умножение
                case OperationTypes.Multiplication:
                    operationResult = calculationService.Multiplication(argument1, argument2);
                    break;
                //Деление
                case OperationTypes.Division:
                    operationResult = calculationService.Division(argument1, argument2);
                    break;
                default:
                    operationResult = 0;
                    throw new Exception("Не удалось получить результат");
            }

            //Сохранение
            storageService.Save(argument1,
                    argument2,
                    operationResult,
                    operationType);
            //возвращаем результат операции
            return operationResult.ToString();
        }
Ejemplo n.º 3
0
        private void btn_Calculation_Click(object sender, EventArgs e)
        {
            //создаем модель и заполняем её аргументами
            var model = new CalculationViewModel
            {
                Argument1 = this.viewUI.GetArgument1(),
                Argument2 = this.viewUI.GetArgument2(),
                SelectedOperationType = this.viewUI.GetOperationType()
            };

            string operationResult = string.Empty;
            try
            {
                //получаем результат операции от презентера
                operationResult = this.presenter.Calculation(model);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }

            //отображаем результат операции
            this.viewUI.SetOperationResult(operationResult);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Отображает основную форму для вычислений
 /// </summary>
 /// <returns></returns>
 public ActionResult CalculationForm()
 {
     var model = new CalculationViewModel();
     return View(model);
 }