Hands on Test Driven Development Katas

In these days, Test Driven Development (TDD) is one of the most growing things in the technical world. Most of us are following Agile methodology where we would like to test our code within code.In this article, we will discuss all about TDD Katas and how we can get hands-on with

C# (10.0 MB)
 
 
 
 
 
5 Star
(1)
222 times
Add to favorites
12/24/2015
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
using System;

namespace TDD_Katas_project.OddEvenKata
{
    public class OddEven
    {
        #region Public Methods
        public static string PrintOddEven(int startNumber, int lastNumber)
        {
            return GetOddEvenWithinRange(startNumber, lastNumber);
        }
        public static string PrintSingleOddEven(int number)
        {
            return CheckSingleNumberOddEvenPrimeResult(number);
        }
        #endregion

        #region MyRePrivate Methods
        private static string CheckSingleNumberOddEvenPrimeResult(int number)
        {
            var result = string.Empty;
            result = CheckSingleNumberOddEvenPrimeResult(result, number);
            return result;
        }

        private static string GetOddEvenWithinRange(int startNumber, int lastNumber)
        {
            var result = string.Empty;
            for (var number = startNumber < 0 ? 1 : startNumber; number <= lastNumber; number++)
                result = CheckSingleNumberOddEvenPrimeResult(result, number);

            return result;

        }

        private static string CheckSingleNumberOddEvenPrimeResult(string result, int number)
        {
            var newNumber = string.Empty;

            var oddNumber = IsOddNumber(number) ? "Odd" : Convert.ToString(number);
            var primenumber = IsPrimeNumber(number) ? Convert.ToString(number) : oddNumber;

            if (!string.IsNullOrEmpty(newNumber))
                newNumber += IsEvenNumber(number) ? "Even" : primenumber;
            else
                newNumber = IsEvenNumber(number) ? "Even" : primenumber;

            result += " " + newNumber;
            return result.Trim();
        }

        private static bool IsEvenNumber(int number)
        {
            return number >= 2 && number % 2 == 0;
        }
        private static bool IsOddNumber(int number)
        {
            return (number % 2 != 0);
        }
        private static bool IsPrimeNumber(int number)
        {
            if (number < 2) return false;
            if (IsEvenNumber(number)) return false;
            var divisble = 3;
            while (divisble * divisble <= number)
            {
                if (number % divisble == 0) return false;
                divisble += 2;
            }
            return true;

        }
        #endregion
    }
}