コード例 #1
0
        public static void BringUpCart(List <Book> cart)
        {
            bool validInput = false;

            Console.WriteLine(@"    \________ ");
            Console.WriteLine(@" ~   \######/ ");
            Console.WriteLine(@"  ~   |####/");
            Console.WriteLine(@" ~    |____.");
            Console.WriteLine(@"______o____o___");

            Console.WriteLine($"\r\nHere is your what's in your cart:");
            Menu.DisplayBookList(cart);


            if (cart.Count == 0)
            {
                Console.Clear();
                Console.WriteLine("Your cart is Empty! Press enter to continue.");
                Console.ReadLine();
            }
            else
            {
                do
                {
                    Console.WriteLine("Would you like to: \r\n 1. Checkout \r\n 2. Empty Cart \r\n 3. Go Back");
                    string cartChoice = Console.ReadLine().ToLower();

                    if (cartChoice == "1" || cartChoice == "one")
                    {
                        Checkout.CheckoutBook(cart);
                        validInput = true;
                        Console.Clear();
                        Console.WriteLine($"The following books have been checked out.");
                        Menu.DisplayBookList(cart);
                        Console.WriteLine("Press enter to continue.");
                        Console.ReadLine();
                        Console.Clear();
                        cart.Clear();
                    }
                    else if (cartChoice == "2" || cartChoice == "two")
                    {
                        Console.Clear();
                        Console.WriteLine("The cart has been cleared. Press enter to continue.");
                        Console.ReadLine();
                        cart.Clear();
                        validInput = true;
                    }
                    else if (cartChoice == "3" || cartChoice == "three")
                    {
                        validInput = true;
                        Console.Clear();
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("That is not a valid entry. Please press enter to try again.");
                        Console.ReadLine();
                        Console.Clear();

                        Console.WriteLine(@"    \________ ");
                        Console.WriteLine(@" ~   \######/ ");
                        Console.WriteLine(@"  ~   |####/");
                        Console.WriteLine(@" ~    |____.");
                        Console.WriteLine(@"______o____o___");

                        Console.WriteLine($"\r\nHere is your what's in your cart:");
                        Menu.DisplayBookList(cart);

                        validInput = false;
                    }
                } while (!validInput);
            }
        }
コード例 #2
0
        public static void SelectFromList(List <Book> bookList, List <Book> cart)
        {
            if (bookList.Any())
            {
                var    isValidInput = false;
                var    selectAgain  = false;
                string userChoice;

                Console.WriteLine("Would you like to select a book (Y/N)?");
                userChoice = Console.ReadLine();
                do
                {
                    do
                    {
                        if (userChoice.Equals("Y", StringComparison.OrdinalIgnoreCase))
                        {
                            isValidInput = true;
                            Console.WriteLine("Select a book by number from the list above.");
                            var  selectedBook = SelectBook(bookList);
                            bool validInput   = false;

                            if (selectedBook.IsCheckedOut)
                            {
                                Console.WriteLine("Would you like to return this book (Y/N)?");
                                do
                                {
                                    var userInput = Console.ReadLine();
                                    if (userInput.Equals("Y", StringComparison.OrdinalIgnoreCase))
                                    {
                                        validInput = true;
                                        Checkout.ReturnBook(selectedBook);
                                        Console.WriteLine($"You've returned {selectedBook.Title}. Press enter to continue.");
                                        Console.ReadLine();
                                    }
                                    else if (userInput.Equals("N", StringComparison.OrdinalIgnoreCase))
                                    {
                                        validInput = true;
                                    }
                                    else
                                    {
                                        Console.BackgroundColor = ConsoleColor.Red;
                                        Console.ForegroundColor = ConsoleColor.Black;
                                        Console.WriteLine("That is not a valid entry. Please try again:");
                                        Console.BackgroundColor = ConsoleColor.Black;
                                        Console.ForegroundColor = ConsoleColor.Green;
                                    }
                                } while (!validInput);
                            }
                            else
                            {
                                Console.WriteLine("Add book to cart (Y/N)?");
                                do
                                {
                                    var userInput = Console.ReadLine();
                                    if (userInput.Equals("Y", StringComparison.OrdinalIgnoreCase))
                                    {
                                        validInput = true;
                                        if (!cart.Contains(selectedBook))
                                        {
                                            cart.Add(selectedBook);
                                            Console.WriteLine($"You've added {selectedBook.Title} to the cart. Press enter to continue.");
                                            Console.ReadLine();
                                        }
                                        else
                                        {
                                            Console.WriteLine("That book is already in the cart. Press enter to continue.");
                                            Console.ReadLine();
                                        }
                                    }
                                    else if (userInput.Equals("N", StringComparison.OrdinalIgnoreCase))
                                    {
                                        validInput = true;
                                    }
                                    else
                                    {
                                        Console.BackgroundColor = ConsoleColor.Red;
                                        Console.ForegroundColor = ConsoleColor.Black;
                                        Console.WriteLine("That is not a valid entry. Please try again:");
                                        Console.BackgroundColor = ConsoleColor.Black;
                                        Console.ForegroundColor = ConsoleColor.Green;
                                    }
                                } while (!validInput);
                            }
                        }
                        else if (userChoice.Equals("N", StringComparison.OrdinalIgnoreCase))
                        {
                            return;
                        }
                        else
                        {
                            Console.BackgroundColor = ConsoleColor.Red;
                            Console.ForegroundColor = ConsoleColor.Black;
                            Console.WriteLine("That is not a valid entry. Please try again:");
                            Console.BackgroundColor = ConsoleColor.Black;
                            Console.ForegroundColor = ConsoleColor.Green;
                        }
                    } while (!isValidInput);

                    Console.WriteLine("Would you like to select another book from the list (Y/N)?");
                    bool isValid;
                    do
                    {
                        userChoice = Console.ReadLine();
                        if (userChoice.Equals("Y", StringComparison.OrdinalIgnoreCase))
                        {
                            isValid     = true;
                            selectAgain = true;
                        }
                        else if (userChoice.Equals("N", StringComparison.OrdinalIgnoreCase))
                        {
                            isValid     = true;
                            selectAgain = false;
                        }
                        else
                        {
                            Console.BackgroundColor = ConsoleColor.Red;
                            Console.ForegroundColor = ConsoleColor.Black;
                            Console.WriteLine("That is not a valid entry. Please try again:");
                            Console.BackgroundColor = ConsoleColor.Black;
                            Console.ForegroundColor = ConsoleColor.Green;
                            isValid = false;
                        }
                    } while (!isValid);
                } while (selectAgain);
            }
        }