示例#1
0
        /// <summary>
        /// Reads an integer value from the user. Takes steps to ensure the length and positive
        /// nature of the integer are correct according to the parameters as they are set.
        /// </summary>
        /// <param name="digitsMin">
        /// Minimum # of digits the integer can be.
        /// </param>
        /// <param name="digitsMax">
        /// Maximum # of digits the integer can be.
        /// </param>
        /// <param name="isPositive">
        /// Should the number be positive?
        /// </param>
        /// <param name="context">
        /// Context of the input, so the user knows what they should type.
        /// </param>
        /// <returns>
        /// The integer in the specified parameters range.
        /// </returns>
        public static int ReadInteger(int digitsMin, int digitsMax, bool isPositive, string context = "ID")
        {
            string num = "";

            // We can't allow for less than 1 digit.
            if (digitsMin < 1)
            {
                digitsMin = 1;
            }

            while (num == "")
            {
                Console.Write("Enter " + context + ": ");
                // Get the input from the user.
                num = InputController.ReadLine(true, isPositive);

                // Ensure the input was good.
                if (num == "" || num.Length < digitsMin || num.Length > digitsMax)
                {
                    Console.WriteLine("\n\tBad " + context + "!" + " Please try again.\n");
                    // Number was bad, reset the input.
                    num = "";
                }
            }
            return(Convert.ToInt32(num));
        }
示例#2
0
        /// <summary>
        /// Gets input string from the user of a string that matches the parameters set.
        /// </summary>
        /// <param name="lengthMin">
        /// Minimum length of the string.
        /// </param>
        /// <param name="lengthMax">
        /// Maximum length of the string.
        /// </param>
        /// <param name="context">
        /// Context of the input, so the user knows what to type.
        /// </param>
        /// <returns>
        /// The input within the specified range of the parameters.
        /// </returns>
        public static string ReadText(int lengthMin, int lengthMax, string context = "Text")
        {
            // Strings cant be negative in length.
            if (lengthMin < 0)
            {
                lengthMin = 1;
            }

            // The minimum length should not be greater than the maximum.
            if (lengthMin > lengthMax)
            {
                lengthMin = lengthMax;
            }

            string text = "";

            while (text == "")
            {
                Console.Write("Enter " + context + ": ");

                // Get the user input.
                text = InputController.ReadLine(false, false);

                // Ensure the input is in the range of the parameters.
                if ((text == "" && lengthMin > 0) ||
                    text.Length < lengthMin || text.Length > lengthMax)
                {
                    Console.WriteLine("\n\tBad " + context + "!" + " Please try again.\n");
                    // On bad input, reset the input.
                    text = "";
                }
                else
                {
                    break;
                }
            }

            return(text);
        }