Exemplo n.º 1
0
        /// <summary>
        /// The UI of the program.  It will start by asking the user which option (userSelection) they would like
        /// to choose (Option 1 = Convert time and Option 2 = Exiting program).  Then it will ask the user
        /// what the value (value) they would like to convert from (convertFrom) and convert to (convertTo).
        /// After that is done, it will go to TimeConversion.cs to do the calculations and modify the user input
        /// (See TimeConversion.cs for more details).  This will continue until the user selects Option 2 to exit
        /// the program.
        /// </summary>
        /// <param name="args">Console Application</param>
        public static void Main(string[] args)
        {
            int userSelection = 0;

            double value;
            string convertFrom;
            string convertTo;

            do
            {
                Console.WriteLine("Please make a selection");
                Console.WriteLine("1. Convert time");
                Console.WriteLine("2. Exit");

                try
                {
                    userSelection = int.Parse(Console.ReadLine());

                    if (userSelection == 1)
                    {
                        Console.WriteLine("Enter a value:");
                        value = ValueInput();

                        Console.WriteLine("What would you like to convert from? (seconds, minutes, hours, days)");
                        convertFrom = Console.ReadLine();

                        Console.WriteLine("What would you like to convert to? (seconds, minutes, hours, days)");
                        convertTo = Console.ReadLine();

                        Console.WriteLine("Convert " + convertFrom + " to " + convertTo + ": " +
                                          TimeConversion.Convert(value, convertFrom, convertTo));
                    }
                    else if (userSelection == 2)
                    {
                        Console.WriteLine("Exiting program...");
                    }
                    else
                    {
                        Console.WriteLine("Please select option 1 or option 2 only.");
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid input. Please try again.");
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            } while (userSelection != 2);
        }
        public static void Main(string[] args)
        {
            bool exited = false;

            while (!exited)
            {
                int menuSelected = PrintMenu();
                while (menuSelected == -1)
                {
                    menuSelected = PrintMenu();
                }

                switch (menuSelected)
                {
                case 1:
                    double   time            = GetDateTime();
                    string[] dateTimeStrings = GetConvertStrings();
                    try
                    {
                        double result = TimeConversion.Convert(time, dateTimeStrings[0], dateTimeStrings[1]);
                        Console.WriteLine($"The time of {time} {dateTimeStrings[0]} converted to {dateTimeStrings[1]} is {result} {dateTimeStrings[1]}");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"An error occured: {ex.Message}");
                    }

                    break;

                case 2:
                    Console.WriteLine("Thank you, exiting now...");
                    exited = true;
                    break;
                }
                if (!exited)
                {
                    Console.WriteLine("press any key to continue to menu");
                    Console.ReadKey();
                }
            }
        }