private bool ValidateProductIndexEntry(string optionNum, out int result) { if (!Int32.TryParse(optionNum, out result)) { Console.Write("\nError: You must enter a number. "); return(false); } if (result < 1 || result > FarmProducts.Options().Count) { Console.Write("\nError: You must enter a valid product number. "); return(false); } return(true); }
public void AddProductsToBox(SingleBox box) { bool addAnother = false; do { Console.WriteLine("\nOur farm products:\n"); List <string> options = FarmProducts.Options(); int optionIndex = 1; foreach (string option in options) { Console.WriteLine(optionIndex + " " + option); optionIndex++; } Console.Write("\nWhat would you like to add to your box? (Please type in the number or \"ABORT\" to exit.) "); string optionNum = Console.ReadLine(); if (optionNum.Equals("abort", StringComparison.OrdinalIgnoreCase)) { return; } if (ValidateProductIndexEntry(optionNum, out int result)) { do { Console.Write("\nHow many would you like? (Or, type in \"ABORT\" to exit.) "); string numOfChosenItem = Console.ReadLine(); if (numOfChosenItem.Equals("abort", StringComparison.OrdinalIgnoreCase)) { return; } if (ValidateNumberOfItemsRequested(numOfChosenItem, out int numOfItem)) { string newProductValue = options[result - 1]; box.Add_Product(newProductValue, numOfItem); Console.WriteLine($"\nYour item {newProductValue} has been successfully added."); Console.Write("\nWould you like to add another item to your box? "); string addAnotherItemYesOrNo = Console.ReadLine(); if (addAnotherItemYesOrNo.Equals("yes", StringComparison.OrdinalIgnoreCase)) { addAnother = true; } else if (addAnotherItemYesOrNo.Equals("no", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("\nYou have the following items in your box:"); foreach (CSABoxProduct product in box.ProductList) { Console.WriteLine($"{product.ProductName}, {product.ProductQuantity}"); } addAnother = false; } break; } } while (true); } else { addAnother = true; } } while (addAnother); }