Example #1
0
        private static List <object> getPropertiesFromUser(List <VehicleProperties> i_VehicleRequiredProperties, int i_VehicleType)
        {
            List <object> userEnteredProperties = new List <object>();
            string        message;
            string        userInputAsString;

            foreach (VehicleProperties currentProperty in i_VehicleRequiredProperties)
            {
                if (currentProperty.Type == typeof(bool))
                {
                    message = string.Format(
                        "Please choose {1}:{0}Enter 1 for 'yes' or 2 for 'no'.",
                        Environment.NewLine,
                        currentProperty.Description);
                    Console.WriteLine(message);
                    userInputAsString = Console.ReadLine();
                    while (userInputAsString != "1" && userInputAsString != "2")
                    {
                        Console.WriteLine(message);
                        userInputAsString = Console.ReadLine();
                    }

                    if (userInputAsString.Equals("1"))
                    {
                        userEnteredProperties.Add(true);
                    }
                    else if (userInputAsString.Equals("2"))
                    {
                        userEnteredProperties.Add(false);
                    }
                }
                else if (currentProperty.Type == typeof(int))
                {
                    int  value    = 0;
                    bool isNumber = false;
                    message = string.Format("Please enter {0} (an integer):", currentProperty.Description);
                    while (!isNumber)
                    {
                        Console.WriteLine(message);
                        userInputAsString = Console.ReadLine();
                        isNumber          = int.TryParse(userInputAsString, out value);
                        if (isNumber)
                        {
                            userEnteredProperties.Add(value);
                        }
                    }
                }
                else if (currentProperty.Description == "Energy Level")
                {
                    float energyLevelValue = 0;
                    message = string.Format("Please enter {0} (a number):", currentProperty.Description);
                    Console.WriteLine(message);
                    energyLevelValue = Validation.GetEnergyLevel(i_VehicleType, s_Garage);
                    userEnteredProperties.Add(energyLevelValue);
                }
                else if (currentProperty.Description == "Current Air Pressure")
                {
                    float CurrentPressureValue = 0;
                    message = string.Format("Please enter {0} (a number):", currentProperty.Description);
                    Console.WriteLine(message);
                    CurrentPressureValue = Validation.GetCurrentAirPressure(i_VehicleType, s_Garage);
                    userEnteredProperties.Add(CurrentPressureValue);
                }
                else if (currentProperty.Type == typeof(float))
                {
                    float value    = 0;
                    bool  isNumber = false;
                    message = string.Format("Please enter {0} (a number):", currentProperty.Description);
                    while (!isNumber)
                    {
                        Console.WriteLine(message);
                        userInputAsString = Console.ReadLine();
                        isNumber          = float.TryParse(userInputAsString, out value);
                        if (isNumber)
                        {
                            userEnteredProperties.Add(value);
                        }
                    }
                }
                else if (currentProperty.Type == typeof(string))
                {
                    message = string.Format("Please enter {0}: ", currentProperty.Description);
                    Console.WriteLine(message);
                    userInputAsString = Console.ReadLine();
                    userEnteredProperties.Add(userInputAsString);
                }
                else if (currentProperty.Type.IsEnum)
                {
                    Console.WriteLine(
                        string.Format(
                            "Please enter {0}.",
                            currentProperty.Description));
                    userEnteredProperties.Add(getUserChoiceFromEnumValues(currentProperty.Type));
                }
            }

            return(userEnteredProperties);
        }