Click here to Skip to main content
15,886,137 members
Articles / General Programming / Regular Expressions

Parsing Latitude and Longitude Information

Rate me:
Please Sign up or sign in to vote.
4.73/5 (24 votes)
21 Feb 2012CPOL9 min read 93.1K   2K   74  
Parses user input and extracts latitude and longitude information, taking into account the user's language and regional settings
using System;
using System.Reflection;
using Geospatial;
using NUnit.Framework;

namespace UnitTests
{
    public static class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine("Type 'exit' to finish or 'tests' to run the unit tests.\n");
                Console.Write("Coordinate: ");

                string input = Console.ReadLine();
                switch (input.ToUpperInvariant())
                {
                    case "EXIT":
                        return;
                    case "TESTS":
                        RunTests();
                        break;
                    default:
                        Location location;
                        if (!Location.TryParse(input, out location))
                        {
                            Console.WriteLine("Invalid input.");
                        }
                        else
                        {
                            Console.WriteLine("{0:DMS1}\n", location);
                        }
                        break;
                }

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

        private static void RunTests()
        {
            int totalPassed = 0;
            int totalFailed = 0;
            foreach (var type in Assembly.GetAssembly(typeof(Program)).GetTypes())
            {
                if (type.GetCustomAttributes(typeof(TestFixtureAttribute), false).Length != 0)
                {
                    Console.WriteLine(type.Name);

                    int passed = 0;
                    int failed = 0;
                    RunTests(type, ref passed, ref failed);

                    totalPassed += passed;
                    totalFailed += failed;
                }
            }

            Console.WriteLine("\nTotal number of tests ran:{0} (Failed:{1})\n", totalPassed + totalFailed, totalFailed);
        }

        private static void RunTests(Type type, ref int passed, ref int failed)
        {
            object instance = Activator.CreateInstance(type);
            foreach (var method in type.GetMethods())
            {
                if (method.GetCustomAttributes(typeof(TestAttribute), false).Length != 0)
                {
                    Console.Write("\t{0}...", method.Name);
                    try
                    {
                        method.Invoke(instance, null);
                        Console.WriteLine("passed.");
                        passed++;
                    }
                    catch (TargetInvocationException ex)
                    {
                        Console.WriteLine("FAILED ({0})", ex.InnerException.Message);
                        failed++;
                    }
                }
            }
            Console.WriteLine("Passed:{0} Failed:{1}\n", passed, failed);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions