コード例 #1
0
        static void Order(ref Navigator nav)
        {
            Console.WriteLine("Let's build your order! Type in the format 1 10 for 10 of product 1.");
            Console.WriteLine("Type 'end' to stop, 'buy' to submit the order, or select from the following: ");

            while (true)
            {
                try
                {
                    nav.CurrentStore.PrintAvailableProducts();
                    string[] input = Console.ReadLine().Split(' ');
                    Console.WriteLine("");

                    if (input.Length >= 1)
                    {
                        if (input[0].ToLower() == "end")
                        {
                            nav.ClearCart();
                            break;
                        }
                        else if (input[0].ToLower() == "buy")
                        {
                            nav.SubmitCart();

                            Console.WriteLine("Order submitted!");
                            break;
                        }
                        else if (input.Length > 1)
                        {
                            int productId = int.Parse(input[0]);
                            int quantity  = int.Parse(input[1]);

                            nav.AddProductToCart(productId, quantity);
                            nav.PrintCart();
                        }
                        else
                        {
                            throw new Exception("Invalid input.");
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error: {e.Message}. Please enter a valid input.");
                    Console.WriteLine("");
                }
            }
        }