Esempio n. 1
0
        /// <summary>
        /// helper method to get a valid positive integer from the user
        /// </summary>
        /// <param name="maxAttempts">maximum number of attempts</param>
        /// <param name="userPrompt">user prompt</param>
        /// <param name="validInput">indicates valid user input</param>
        /// <returns></returns>
        public static string GetYesNoFromUser(int maxAttempts, string userPrompt, out bool maxAttemptsExceeded)
        {
            bool validInput = false;

            maxAttemptsExceeded = false;
            string userResponse = "";
            int    attempts     = 1;

            while (!validInput && !maxAttemptsExceeded)
            {
                ConsoleUtil.DisplayPromptMessage($"{userPrompt} [Yes / No] ");
                userResponse = Console.ReadLine();
                ConsoleUtil.DisplayMessage("");

                //
                // input is valid
                //
                if (userResponse.ToUpper() == "YES" || userResponse.ToUpper() == "NO")
                {
                    validInput = true;
                }
                //
                // input is invalid, but more attempts available
                //
                else
                {
                    ConsoleUtil.DisplayMessage($"You entered: {userResponse}");
                    ConsoleUtil.DisplayMessage($"\"{userResponse}\" is not a valid response.");

                    //
                    // more attempts available
                    //
                    if (attempts < maxAttempts)
                    {
                        ConsoleUtil.DisplayMessage($"Please enter either \"Yes\" or \"No\".");
                        ConsoleUtil.DisplayMessage("Press any key to try again.");
                    }
                    //
                    // all attempts used
                    //
                    else
                    {
                        ConsoleUtil.DisplayMessage("It appears you have exceeded the maximum number of attempts allowed.");
                        ConsoleUtil.DisplayMessage("Press any key to continue.");
                        maxAttemptsExceeded = true;
                    }

                    Console.ReadKey();
                    Console.Clear();
                }

                attempts++;
            }

            return(userResponse);
        }
        /// <summary>
        /// get the next city to travel to from the user
        /// </summary>
        /// <returns>string City</returns>
        public string DisplayGetNextCity()
        {
            string nextCity = "";

            ConsoleUtil.HeaderText = "Travel";
            ConsoleUtil.DisplayReset();

            ConsoleUtil.DisplayPromptMessage("Enter the name of the next city:");
            nextCity = Console.ReadLine();

            return(nextCity);
        }
        /// <summary>
        /// displays the current inventory
        /// </summary>
        public void DisplayInventory(Salesperson salesperson, Product units)
        {
            ConsoleUtil.HeaderText = "Current Inventory";
            ConsoleUtil.DisplayReset();

            ConsoleUtil.DisplayMessage("");
            ConsoleUtil.DisplayMessage("Products:");
            ConsoleUtil.DisplayMessage("");
            ConsoleUtil.DisplayPromptMessage($"\t- {salesperson.CurrentStock.Type}, # of units: {units.NumberOfUnits.ToString()}");
            ConsoleUtil.DisplayMessage("");

            DisplayContinuePrompt();
        }
        /// <summary>
        /// allows the user to update their account information
        /// </summary>
        public Salesperson DisplayUpdateAcountInfo(Salesperson salesperson, out bool maxAttemptsExceeded)
        {
            string userResponse;
            bool   editingAccount = true;

            maxAttemptsExceeded = false;

            ConsoleUtil.HeaderText = "Update Account";
            ConsoleUtil.DisplayReset();

            ConsoleUtil.DisplayMessage("");
            ConsoleUtil.DisplayMessage(" NOTE: You cannot change the number units here. Use the Buy/Sell options \nfrom the main menu to do that.");

            DisplayContinuePrompt();

            while (editingAccount)
            {
                ConsoleUtil.HeaderText = "Update Account";
                ConsoleUtil.DisplayReset();

                //
                // displays current account info wihtout cities and number of units
                //
                #region Current Account Info

                ConsoleUtil.DisplayMessage("Current account info:");
                ConsoleUtil.DisplayMessage("");

                ConsoleUtil.DisplayMessage("First Name: " + salesperson.FirstName);
                ConsoleUtil.DisplayMessage("Last Name: " + salesperson.LastName);
                ConsoleUtil.DisplayMessage("Account ID: " + salesperson.AccountID);
                ConsoleUtil.DisplayMessage("Starting City: " + salesperson.StartingCity);
                ConsoleUtil.DisplayMessage("Product Type: " + salesperson.CurrentStock.Type);

                #endregion

                ConsoleUtil.DisplayMessage("");
                ConsoleUtil.DisplayMessage("Please select a corresonding number to change that part of your account.");
                ConsoleUtil.DisplayMessage("");

                ConsoleUtil.DisplayPromptMessage("1)First Name | 2)Last Name | 3)Account ID | 4)Starting City | 5)Product");

                ConsoleUtil.DisplayMessage("");
                ConsoleUtil.DisplayMessage("");
                ConsoleUtil.DisplayMessage("");
                ConsoleUtil.DisplayPromptMessage("Enter your choice here: ");
                userResponse = Console.ReadLine();
                ConsoleUtil.DisplayMessage("");

                //
                // get a valid answer form user
                //
                while (userResponse != "1" && userResponse != "2" && userResponse != "3" && userResponse != "4" && userResponse != "5")
                {
                    ConsoleUtil.HeaderText = "Invalid Input";
                    ConsoleUtil.DisplayReset();

                    ConsoleUtil.DisplayPromptMessage("1)First Name | 2)Last Name | 3)Account ID | 4)Starting City | 5)Product");
                    ConsoleUtil.DisplayMessage("");
                    ConsoleUtil.DisplayPromptMessage("Please enter a correct corresponding number: ");
                    userResponse = Console.ReadLine();

                    if (userResponse == "1" && userResponse == "2" && userResponse == "3" && userResponse == "4" && userResponse == "5")
                    {
                        break;
                    }
                }

                //
                // edit account info
                //
                switch (userResponse)
                {
                case "1":
                    ConsoleUtil.HeaderText = "Update First Name";
                    ConsoleUtil.DisplayReset();

                    ConsoleUtil.DisplayPromptMessage("Enter your new first name: ");
                    salesperson.FirstName = Console.ReadLine();
                    ConsoleUtil.DisplayMessage("");
                    break;

                case "2":
                    ConsoleUtil.HeaderText = "Update Last Name";
                    ConsoleUtil.DisplayReset();

                    ConsoleUtil.DisplayPromptMessage("Enter your new last name: ");
                    salesperson.LastName = Console.ReadLine();
                    ConsoleUtil.DisplayMessage("");
                    break;

                case "3":
                    ConsoleUtil.HeaderText = "Update Account ID";
                    ConsoleUtil.DisplayReset();

                    ConsoleUtil.DisplayPromptMessage("Enter your new Account ID: ");
                    salesperson.AccountID = Console.ReadLine();
                    ConsoleUtil.DisplayMessage("");
                    break;

                case "4":
                    ConsoleUtil.HeaderText = "Update Starting City";
                    ConsoleUtil.DisplayReset();

                    ConsoleUtil.DisplayPromptMessage("Enter your new starting city: ");
                    salesperson.StartingCity = Console.ReadLine();
                    ConsoleUtil.DisplayMessage("");
                    break;

                case "5":
                    ConsoleUtil.HeaderText = "Update Product Type";
                    ConsoleUtil.DisplayReset();

                    Product.ProductType productType;

                    ConsoleUtil.DisplayMessage("");
                    ConsoleUtil.DisplayPromptMessage("Enter you new product selection: ");

                    //
                    // get new product type from user
                    //
                    if (Enum.TryParse <Product.ProductType>(UppercaseFirst(Console.ReadLine()), out productType))
                    {
                        salesperson.CurrentStock.Type = productType;
                    }
                    else
                    {
                        ConsoleUtil.DisplayReset();
                        ConsoleUtil.DisplayMessage("Seems like you entered an invalid product type.");
                        ConsoleUtil.DisplayMessage("By default, your product type has been set to None.");
                        salesperson.CurrentStock.Type = Product.ProductType.None;
                        DisplayContinuePrompt();
                    }
                    break;

                default:
                    break;
                }
                ConsoleUtil.HeaderText = "Update Account";
                ConsoleUtil.DisplayReset();
                ConsoleUtil.DisplayMessage("Your account has been updated!");
                ConsoleUtil.DisplayMessage("");
                userResponse = ConsoleValidator.GetYesNoFromUser(MAXIMUM_ATTEMPTS, "\tWould you like to make more changes or continue on?", out maxAttemptsExceeded);

                if (maxAttemptsExceeded)
                {
                    ConsoleUtil.DisplayMessage("Max attempts exceeded! You will now be returned to the main menu.");
                    return(salesperson);
                }
                else if (userResponse.ToUpper() == "YES")
                {
                    editingAccount = true;
                }
                else
                {
                    editingAccount = false;
                    return(salesperson);
                }
            }
            return(salesperson);
        }
        /// <summary>
        /// setup the new salesperson object with the initial data
        /// Note: To maintain the pattern of only the Controller changing the data this method should
        ///       return a Salesperson object with the initial data to the controller. For simplicity in
        ///       this demo, the ConsoleView object is allowed to access the Salesperson object's properties.
        /// </summary>
        public Salesperson DisplaySetupAccount()
        {
            Salesperson salesperson = new Salesperson();

            Product.ProductType productType;
            int numberOfUnits;

            ConsoleUtil.HeaderText = "Account Setup";
            ConsoleUtil.DisplayReset();

            ConsoleUtil.DisplayMessage("Setup your account now.");
            ConsoleUtil.DisplayMessage("");

            ConsoleUtil.DisplayPromptMessage("Enter your first name: ");
            salesperson.FirstName = Console.ReadLine();
            ConsoleUtil.DisplayMessage("");

            ConsoleUtil.DisplayPromptMessage("Enter your last name: ");
            salesperson.LastName = Console.ReadLine();
            ConsoleUtil.DisplayMessage("");

            ConsoleUtil.DisplayPromptMessage("Enter your account ID: ");
            salesperson.AccountID = Console.ReadLine();
            ConsoleUtil.DisplayMessage("");

            ConsoleUtil.DisplayPromptMessage("Enter your Starting City: ");
            salesperson.StartingCity = Console.ReadLine();
            ConsoleUtil.DisplayMessage("");

            ConsoleUtil.HeaderText = "Account Setup";
            ConsoleUtil.DisplayReset();

            ConsoleUtil.DisplayMessage("Please select which type of product you want to work with from below.");
            ConsoleUtil.DisplayMessage("");

            ConsoleUtil.DisplayMessage("Product Types:");
            ConsoleUtil.DisplayMessage("");

            Console.Write(
                "\t- Gilded" + Environment.NewLine +
                "\t- Spiked" + Environment.NewLine +
                "\t- Colorful" + Environment.NewLine +
                "\t- Vintage" + Environment.NewLine +
                "\t- Striped" + Environment.NewLine +
                "\t- Used" + Environment.NewLine);

            ConsoleUtil.DisplayMessage("");
            ConsoleUtil.DisplayPromptMessage("Enter product selection: ");

            //
            // get product type from user
            //
            if (Enum.TryParse <Product.ProductType>(UppercaseFirst(Console.ReadLine()), out productType))
            {
                salesperson.CurrentStock.Type = productType;
            }
            else
            {
                ConsoleUtil.DisplayReset();
                ConsoleUtil.DisplayMessage("Seems like you entered an invalid product type.");
                ConsoleUtil.DisplayMessage("By default, your product type has been set to None.");
                salesperson.CurrentStock.Type = Product.ProductType.None;
                DisplayContinuePrompt();
            }

            //
            // get number of products in inventory
            //
            ConsoleUtil.DisplayReset();
            ConsoleUtil.DisplayMessage($"You have selected {productType} as your product type.");

            if (ConsoleValidator.TryGetIntegerFromUser(MINIMUM_BUYSELL_AMOUNT, MAXIMUM_BUYSELL_AMOUNT, MAXIMUM_ATTEMPTS, $"{productType} products to add to your inventory", out numberOfUnits))
            {
                ConsoleUtil.DisplayReset();
                salesperson.CurrentStock.AddProducts(numberOfUnits);
                ConsoleUtil.DisplayMessage($"Thank you! {numberOfUnits} {productType} products are now in your inventory!");
                DisplayContinuePrompt();
            }
            else
            {
                ConsoleUtil.DisplayReset();
                ConsoleUtil.DisplayMessage("Maximum attempts exceeded!");
                ConsoleUtil.DisplayMessage($"By default, the number of {productType} products in your inventory are now set to zero.");
                salesperson.CurrentStock.AddProducts(0);
                DisplayContinuePrompt();
            }

            ConsoleUtil.DisplayReset();

            ConsoleUtil.DisplayMessage("Your account is now setup!");

            DisplayContinuePrompt();

            return(salesperson);
        }
Esempio n. 6
0
        /// <summary>
        /// helper method to get a valid integer from the user within a range
        /// </summary>
        /// <param name="minValue">inclusive minimum value</param>
        /// <param name="maxValue">inclusive maximum value</param>
        /// <param name="maxAttempts">maximum number of attempts</param>
        /// <param name="pluralName">plural name of item</param>
        /// <param name="validInput">indicates valid user input</param>
        /// <returns></returns>
        public static bool TryGetIntegerFromUser(int minValue, int maxValue, int maxAttempts, string pluralName, out int userInteger)
        {
            bool   validInput          = false;
            bool   maxAttemptsExceeded = false;
            string userResponse;
            string feedbackMessage = "";
            int    attempts        = 1;

            userInteger = 0;

            while (!validInput && !maxAttemptsExceeded)
            {
                //
                // more attempts available
                //
                if (attempts <= maxAttempts)
                {
                    ConsoleUtil.DisplayPromptMessage($"Enter a number, between {minValue} and {maxValue}, of {pluralName}: ");
                    userResponse = Console.ReadLine();
                    ConsoleUtil.DisplayMessage("");

                    //
                    // input is an Integer
                    //
                    if (int.TryParse(userResponse, out userInteger))
                    {
                        //
                        // input is in range
                        //
                        if (userInteger >= minValue && userInteger <= maxValue)
                        {
                            validInput = true;
                        }
                        //
                        // input is not in range
                        //
                        else
                        {
                            feedbackMessage = $"The number {userInteger} is not in the specified range.";
                        }
                    }
                    //
                    // input is not an Integer
                    //
                    else
                    {
                        feedbackMessage = $"{userResponse} is not an integer.";
                    }

                    if (!validInput && attempts <= maxAttempts)
                    {
                        ConsoleUtil.DisplayMessage($"You entered: {userResponse}");
                        ConsoleUtil.DisplayMessage(feedbackMessage);

                        if (attempts < maxAttempts)
                        {
                            ConsoleUtil.DisplayMessage($"Please enter an integer between {minValue} and {maxValue}.");
                            ConsoleUtil.DisplayMessage("Press any key to try again.");
                            Console.ReadKey();
                        }
                        else
                        {
                            ConsoleUtil.DisplayMessage("It appears you have exceeded the maximum number of attempts allowed.");
                            ConsoleUtil.DisplayMessage("Press any key to continue.");
                            Console.ReadKey();
                        }

                        Console.Clear();
                    }
                    else
                    {
                        ConsoleUtil.DisplayMessage("");
                    }

                    attempts++;
                }
                else
                {
                    maxAttemptsExceeded = true;
                }
            }

            return(validInput);
        }