// factory method that returns the language translator for the corresponding input language
        public static NumberTranslator GetNumberTranslator(string lang)
        {
            NumberTranslator numTranslator = null;

            if (lang.Equals("english", StringComparison.InvariantCultureIgnoreCase))
            {
                numTranslator = new EnglishTranslator();
            }
            // need to write more if else when new languages added.

            return(numTranslator);
        }
예제 #2
0
        // Utility function to print the numbers in the range [end - start] which are multiple of given num translated in given language
        static void PrintMultiplesOfNinRange(int startNum, int endNum, int divisibleBy, string language)
        {
            // get the number translator corresponding to the input language from the factory.
            NumberTranslator numTranslator = NumberTranslatorFactory.GetNumberTranslator(language);

            // iterate over the range in reverse.
            for (int iLoopCounter = endNum; iLoopCounter >= startNum; iLoopCounter--)
            {
                // if the number is a multiple of divisibleBy, then print it.
                if ((iLoopCounter % divisibleBy) == 0)
                {
                    // get the string that reprsents number worded in the given language.
                    string numStr = numTranslator.Translate(iLoopCounter);
                    Console.WriteLine(numStr);
                }
            }
        }