public void SumNumbers()
        {
            IntValidator validator = new IntValidator();

            Console.WriteLine("\nEnter the number of numbers you want to sum\n ");

            bool canParse;

            var userSize = Console.ReadLine();

            canParse = validator.Validate(userSize);

            int sum = 0;

            if (canParse)
            {
                int   size     = Int32.Parse(userSize);
                int[] arr      = new int[size];
                bool  loopFalg = true;

                for (int i = 0; i < size; i++)
                {
                    bool isInt = false;
                    loopFalg = true;

                    while (loopFalg)
                    {
                        Console.WriteLine("\nPlease enter your number\n");
                        var userNumber = Console.ReadLine();
                        isInt = validator.Validate(userNumber);
                        if (isInt)
                        {
                            sum     += Int32.Parse(userNumber);
                            loopFalg = false;
                        }
                        else
                        {
                            Console.WriteLine("\nYou entered invalid number!!! please try again\n");
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("You entered invalid number!!!");
                return;
            }

            Console.WriteLine($"\nThe sum is: {sum}\n");
        }
Exemple #2
0
        public void Validate_WithInteger_ReturnTrue()
        {
            IntValidator validator = new IntValidator();

            bool result = validator.Validate("1");

            Assert.AreEqual(true, result);
        }
Exemple #3
0
        public void Validate_WithLetters_ReturnFalse()
        {
            IntValidator validator = new IntValidator();

            bool result = validator.Validate("ABC");

            Assert.AreEqual(false, result);
        }
Exemple #4
0
        public void Validate_WithOverFlowNegative_ReturnFalse()
        {
            IntValidator validator = new IntValidator();

            bool result = validator.Validate("-9999999999999999999999");

            Assert.AreEqual(false, result);
        }
Exemple #5
0
        public void Validate_Empty_ReturnFalse()
        {
            IntValidator validator = new IntValidator();

            bool result = validator.Validate("");

            Assert.AreEqual(false, result);
        }
Exemple #6
0
        public void Validate_WithNegativeNumber_ReturnFalse()
        {
            IntValidator validator = new IntValidator();

            bool result = validator.Validate("-1");

            Assert.AreEqual(false, result);
        }
Exemple #7
0
        public void Validate_WithFloat_ReturnFalse()
        {
            IntValidator validator = new IntValidator();

            bool result = validator.Validate("12.5");

            Assert.AreEqual(false, result);
        }
Exemple #8
0
        private int getValues()
        {
            bool canParse;

            while (true)
            {
                Console.WriteLine("\nPlease enter the number you want to print\n");
                var          userNumber = Console.ReadLine();
                IntValidator validator  = new IntValidator();
                canParse = validator.Validate(userNumber);

                if (canParse)
                {
                    return(Int32.Parse(userNumber));
                }
                else
                {
                    Console.WriteLine("\nYou entered invalid number! please try again\n");
                }
            }
        }