An exercise in TDD.

public class Program
    {
        static void Main(string[] args)
        {
            int sn = 123456782;
            int[] Digits;
            int AddedResult = 0;
            string s = sn.ToString();
            string sa = s.Substring(s.Length - 1, 1);

            int checkDigit = Convert.ToInt32(sn.ToString().Substring(s.Length - 1, 1)); //get the last digit.

            if (IsValidLength(sn))
            {

                sn = RemoveLastDigit(sn);
                Digits = ExtractEvenDigits(sn);
                Digits = DoubleDigits(Digits);
                AddedResult = AddedEvenDigits(Digits);
                AddedResult += AddOddDigits(sn);
                if (IsValidSN(AddedResult, checkDigit))
                {
                    Console.WriteLine("The number is valid");
                }
                else
                {
                    Console.WriteLine("The Number is not valid");
                }
            }
            else
            {
                Console.WriteLine("NotValidLength");
            }

            Char c = '2';
            Console.WriteLine("Character is {0}: ", Convert.ToInt32(c)); // returns 50
            string st = c.ToString();
            Console.WriteLine("string is {0}: ", Convert.ToInt32(st)); //returns 2;

            Console.Read();
            
        }

        public static bool IsValidSN(int AddedResult, int checkDigit)
        {
            return ((AddedResult % 10 == 0 && checkDigit == 0) || IsValidDifference(AddedResult, checkDigit));
            
        }

        public static bool IsValidDifference(int AddedResult, int checkDigit)
        {
            int nextHighestTens = AddedResult;
            while (nextHighestTens % 10 != 0)
            {
                nextHighestTens++;
            }
            return ((nextHighestTens - AddedResult) == checkDigit);
        }

        public static int AddOddDigits(int sn)
        {
            string s = sn.ToString();
            int i = 1;
            int addedResult = 0;
            foreach (char c in s)
            {
                if (i % 2 != 0)
                {
                    addedResult += Convert.ToInt32(c.ToString());
                }
                i++;
            }

            return addedResult;
        }

        public static int AddedEvenDigits(int[] Digits)
        {
            int addedEvenDigits = 0;
            string s = "";
            for (int i = 0; i < Digits.Length; i++) //extract each digit. For example 12 is extracted as 1 and 2
            {
                s += Digits[i].ToString();
            }
            for (int i = 0; i < s.Length; i++) //now add all extracted digits
            {
                addedEvenDigits += Convert.ToInt32(s[i].ToString());
            }
            return addedEvenDigits;
        }

        public static int[] DoubleDigits(int[] Digits)
        {
            int[] doubledDigits = new int[Digits.Count()];
            for (int i = 0; i < Digits.Length; i++)
            {
                doubledDigits[i] = Digits[i] * 2;
            }
            return doubledDigits;
        }

        public static int[] ExtractEvenDigits(int sn)
        {
            int[] EvenDigits = new int[4];
            string s = sn.ToString(); //12345678

            int j = 0;
            for (int i = 1; i < s.Length; i += 2)
            {
                EvenDigits[j] = Convert.ToInt32(s[i].ToString());
                j++;
            }
            
            return EvenDigits;
        }

        public static int RemoveLastDigit(int sn)
        {
            string s = sn.ToString();
            return Convert.ToInt32(s.Substring(0, s.Count() - 1));
        }
        public static bool IsValidLength(int sn)
        {
            return (sn > 9999999 && sn < 1000000000);
        }
    }

The Tests

    [TestFixture]
    public class SINTests
    {
        private int SinNumber = 123456782;

        [Test]
        public void TestValidNumber()
        {
            Assert.IsTrue(Program.IsValidLength(SinNumber));
        }
        
        [Test]
        public void TestRemoveLastDigit()
        {
            Assert.AreEqual(12345678, Program.RemoveLastDigit(SinNumber));
        }

        [Test]
        public void TestExtractEvenDigit()
        {
            int sn = 12345678;
            int[] array = new int[] { 2,4,6,8 };
            Assert.AreEqual(array, Program.ExtractEvenDigits(sn));
        }

        [Test]
        public void TestAddOddDigits()
        {
            int sn = 12345678;
            int result = 1 + 3 + 5 + 7;
            Assert.AreEqual(result, Program.AddOddDigits(sn));
        }
        [Test]
        public void TestDoubleEvenDigits()
        {
            int sn = 12345678;
            int[] original = new int[] { 2, 4, 6, 8 };
            int[] array = new int[] { 4, 8, 12, 16 };
            Assert.AreEqual(array, Program.DoubleDigits(original));
        }
        [Test]
        public void TestOddDigits()
        {
            int sn = 12345678;
            Assert.AreEqual(16, Program.AddOddDigits(sn));
        }

    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s