static void Run()
        {
            #region Runs the program expecting only integral inputs and calls ConvertToRomanNumeral() upon receiving
            while (true)
            {
                int result;
                Console.WriteLine("Numeral Registry");
                Console.WriteLine("M - 1000");
                Console.WriteLine("D - 500");
                Console.WriteLine("C - 100");
                Console.WriteLine("L - 50");
                Console.WriteLine("X - 10");
                Console.WriteLine("V - 5");
                Console.WriteLine("I - 1\n");

                Console.WriteLine("Please enter an integer value OR roman numeral to be converted...");
                string userInput = Console.ReadLine();
                if (converter.CheckIfInputIsValid(userInput) == true)    // Added a bool to a method to validate input
                {
                    Console.WriteLine(userInput + " = " + converter.ConvertToRomanNumeral(int.Parse(userInput)));
                }
                else
                {
                    Console.WriteLine(userInput + " Invalid Input");
                }
                Console.Write("\nPress any key...");
                Console.ReadKey();
                Console.Clear();
            }
            #endregion
        }
Ejemplo n.º 2
0
 public static void AllowedNumbers(int input, string stringInput)
 {
     if (input > 0 && input <= 4999)
     {
         result = String.Format("{0} = {1}", stringInput, converter.ConvertToRomanNumeral(input));
         Console.WriteLine(result);
     }
     else
     {
         throw new InvalidNumberException("InvalidNumberException thrown");
     }
 }
Ejemplo n.º 3
0
        static void Run()
        {
            #region Runs the program expecting only integral inputs and calls ConvertToRomanNumeral() upon receiving
            while (true)
            {
                int result;
                Console.WriteLine("Numeral Registry");
                Console.WriteLine("Acceptable Values     |     Non-Acceptable Values");
                Console.WriteLine("M - 1000              |     (D, L, I, X, V) + M");
                Console.WriteLine("D - 500               |     (L, X, V, I) + D");
                Console.WriteLine("C - 100               |     (L, V, I) + C");
                Console.WriteLine("L - 50                |     (V, I) + L");
                Console.WriteLine("X - 10                |     ");
                Console.WriteLine("V - 5                 |     ");
                Console.WriteLine("I - 1                 |     (I) + I + I + I + I\n");

                Console.WriteLine("Please enter an integer value OR roman numeral to be converted...");
                string   userInput = Console.ReadLine();
                string[] eiwa      = { "DM", "LM", "IM", "XM", "VM", "LD", "XD", "VD", "ID", "LC", "VC", "IC", "VL", "IL", "IIII" };

                try
                {
                    if (userInput.All(c => c >= '0' && c <= '9') && userInput != "")
                    {
                        Console.WriteLine(userInput + " = " + converter.ConvertToRomanNumeral(int.Parse(userInput)));
                    }
                    else if (userInput.All(c => c == 'M' || c == 'D' || c == 'C' || c == 'L' || c == 'X' || c == 'V' || c == 'I') &&
                             !eiwa.Any(c => userInput.Contains(c)))
                    {
                        Console.WriteLine(userInput + " = " + converter.ConvertFromRomanNumeral(userInput.ToCharArray().Select(c => c.ToString()).ToArray()));
                    }

                    else if (userInput.All(c => c == 'D' || c == 'M'))
                    {
                        throw (new DmInputException());
                    }

                    else if (userInput.All(c => c == 'L' || c == 'M'))
                    {
                        throw (new LmInputException());
                    }

                    else if (userInput.All(c => c == 'I' || c == 'M'))
                    {
                        throw (new ImInputException());
                    }

                    else if (userInput.All(c => c == 'X' || c == 'M'))
                    {
                        throw (new XmInputException());
                    }

                    else if (userInput.All(c => c == 'V' || c == 'M'))
                    {
                        throw (new VmInputException());
                    }

                    else if (userInput.All(c => c == 'L' || c == 'D'))
                    {
                        throw (new LdInputException());
                    }

                    else if (userInput.All(c => c == 'X' || c == 'D'))
                    {
                        throw (new XdInputException());
                    }

                    else if (userInput.All(c => c == 'V' || c == 'D'))
                    {
                        throw (new VdInputException());
                    }

                    else if (userInput.All(c => c == 'I' || c == 'D'))
                    {
                        throw (new IdInputException());
                    }

                    else if (userInput.All(c => c == 'L' || c == 'C'))
                    {
                        throw (new LcInputException());
                    }

                    else if (userInput.All(c => c == 'V' || c == 'C'))
                    {
                        throw (new VcInputException());
                    }

                    else if (userInput.All(c => c == 'I' || c == 'C'))
                    {
                        throw (new IcInputException());
                    }

                    else if (userInput.All(c => c == 'V' || c == 'L'))
                    {
                        throw (new VlInputException());
                    }

                    else if (userInput.All(c => c == 'I' || c == 'L'))
                    {
                        throw (new IlInputException());
                    }

                    else if (userInput.All(c => (c == 'I' || c == 'I' || c == 'I' || c == 'I')))
                    {
                        throw (new IiiiInputException());
                    }

                    Console.Write("\nPress any key...");
                    Console.ReadKey();
                    Console.Clear();
                }

                catch (OverflowException ofEx)
                {
                    throw new Exception("Don't type so many integers you moron!", ofEx.InnerException);
                }
            }
            #endregion
        }
Ejemplo n.º 4
0
        public static void Run()
        {
            // Instance variables
            string stringInput;
            bool   validInput;
            int    integerInput;
            string result;

            while (true)
            {
                #region Menu
                Console.WriteLine("Numeral Registry");
                Console.WriteLine("M  - 1000");
                Console.WriteLine("D  - 500");
                Console.WriteLine("C  - 100");
                Console.WriteLine("L  - 50");
                Console.WriteLine("X  - 10");
                Console.WriteLine("V  - 5");
                Console.WriteLine("I  - 1\n");
                Console.WriteLine("Please enter an integer value OR roman numeral to be converted...");
                #endregion

                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("\nInput: ");
                Console.ResetColor();

                try
                {
                    stringInput = Console.ReadLine().ToUpper();
                }
                catch (Exception e)
                {
                    if (e is OutOfMemoryException || e is ArgumentOutOfRangeException || e is System.IO.IOException)
                    {
                        throw new UserDefinedException("Error encountered: " + e.Message);
                    }
                    stringInput = String.Empty;
                }

                validInput = int.TryParse(stringInput, out integerInput);

                try
                {
                    if (String.IsNullOrEmpty(stringInput))
                    {
                        throw new UserDefinedException();
                    }
                }
                catch (Exception e)
                {
                    if (e is UserDefinedException)
                    {
                        Console.WriteLine("Error encountered: {0}. No empty strings allowed.",
                                          e.Message);
                    }
                }

                try
                {
                    ValidateSequence(stringInput);
                }
                catch (Exception e)
                {
                    stringInput = string.Empty;
                    if (e is UserDefinedException)
                    {
                        Console.WriteLine("Error encountered: {0} Roman Numeral sequence was incorrect", e.Message);
                    }
                }

                if (!(String.IsNullOrEmpty(stringInput)))
                {
                    if (validInput)
                    {
                        result = String.Format("{0} = {1}", stringInput, converter.ConvertToRomanNumeral(integerInput));
                    }
                    else
                    {
                        try
                        {
                            if (stringInput.All(c => Enum.IsDefined(typeof(RomanNumeralsType), c.ToString())))
                            {
                                result = String.Format("{0} = {1}", stringInput, converter.ConvertToInteger(stringInput).ToString());
                            }
                            else
                            {
                                throw new UserDefinedException();
                            }
                        }
                        catch (Exception e)
                        {
                            if (e is UserDefinedException)
                            {
                                Console.WriteLine("Error encountered: " + e.Message + " Write either all numerals or digits.");
                            }
                        }
                    }
                }

                Console.Write("\nPress any key...");
                Console.ReadKey();
                Console.Clear();
            }
        }