Ejemplo n.º 1
0
        public IActionResult Index(String Numbers = "", String Calculate = OtherAction)
        {
            int[] NumbersToSum;
            int   Sum;
            bool  Prime;

            ViewData["Message1"]  = "";
            ViewData["Message2"]  = "";
            ViewData["Message3"]  = "";
            ViewData["InputNums"] = "";
            ViewData["Result"]    = "";
            ViewData["ResultOut"] = "";

            switch (Calculate)
            {
            case AddAction:
                if (Numbers != null)
                {
                    try
                    {
                        NumbersToSum          = StringToNumbers(Numbers);
                        Sum                   = calculator.Add(NumbersToSum);
                        ViewData["Message1"]  = "Lukujen ";
                        ViewData["InputNums"] = Numbers;
                        ViewData["Message2"]  = " summa on ";
                        ViewData["Result"]    = Sum;
                        ViewData["ResultOut"] = Sum;
                        ViewData["Message3"]  = ".";
                    }
                    catch (FormatException fe)
                    {
                        ViewData["Message1"]  = ParseErrorStart;
                        ViewData["InputNums"] = Numbers;
                        ViewData["Message2"]  = ParseErrorEnd;
                        ViewData["Result"]    = ParseErrorResult;
                    }
                }
                else
                {
                    ViewData["Message1"] = EmptyMsg;
                    ViewData["Result"]   = EmptyResult;
                }
                break;

            case PrimeAction:
                if (Numbers != null)
                {
                    try
                    {
                        NumbersToSum = StringToNumbers(Numbers);
                        Sum          = calculator.Add(NumbersToSum);
                        Prime        = calculator.IsPrime(Sum);

                        if (Prime)
                        {
                            ViewData["Message1"]  = "Luku ";
                            ViewData["InputNums"] = Sum;
                            ViewData["Message2"]  = " on alkuluku.";
                            ViewData["Result"]    = Prime;
                        }
                        else
                        {
                            ViewData["Message1"]  = "Luku ";
                            ViewData["InputNums"] = Sum;
                            ViewData["Message2"]  = " ei ole alkuluku.";
                            ViewData["Result"]    = Prime;
                        }
                    }
                    catch (FormatException fe)
                    {
                        ViewData["Message1"]  = ParseErrorStart;
                        ViewData["InputNums"] = Numbers;
                        ViewData["Message2"]  = ParseErrorEnd;
                        ViewData["Result"]    = ParseErrorResult;
                    }
                }
                else
                {
                    ViewData["Message1"] = EmptyMsg;
                    ViewData["Result"]   = EmptyResult;
                }
                break;

            default:
                ViewData["Message1"] = "Kirjoita tutkittava luku tai luvut kenttään ja valitse haluamasi toiminto.";
                break;
            }
            return(View());
        }