예제 #1
0
        static void Main(string[] args)
        {
            Book[] books = availableBooks(); //get an array of books:
            ShoppingCart shoppingCart = new ShoppingCart();

            var complete = false;

            do
            {
                Console.Write("\nEnter your book choice (0 - 4). Any other number completes the order: ");
                var bookChoice = Convert.ToInt32(Console.ReadLine());

                switch (bookChoice)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        // Enter number of copies
                        Console.Write("Enter number of copies: ");
                        var numCopies = Convert.ToInt32(Console.ReadLine());

                        // Add book to order and order to shopping cart
                        Book book = new Book(books[bookChoice].GetTitle(), books[bookChoice].GetPrice());
                        BookOrder order = new BookOrder(book, numCopies);

                        shoppingCart.AddBookOrder(order);

                        // Display confirmation
                        Console.WriteLine("\n" + numCopies + " copy of '" + book.GetTitle() +
                                          " added to your shopping cart.");

                        // Tells the loop to ask for more books
                        complete = false;

                        break;

                    default:

                        // List book titles and number of copies in cart
                        System.Console.WriteLine("\nYou have placed the following books into your shopping cart:\n");

                        // Store total value of book order
                        double orderTotal = 0.0;

                        // Iterate through book orders, display the cart contents and calculate the total
                        foreach (BookOrder bookOrder in shoppingCart.GetBookOrders())
                        {
                            Console.WriteLine("  " + bookOrder.GetNumCopies() + " copy (copies) of '" + bookOrder.GetBook().GetTitle() + "'");
                            orderTotal += (bookOrder.GetBook().GetPrice()) * bookOrder.GetNumCopies();
                        }

                        // List total cost of all orders in cart
                        Console.WriteLine("\nThe total cost is $" + orderTotal);

                        // Save to file and exit
                        System.Console.WriteLine("\nYou shopping cart has been saved. Press return key to exit the application");
                        saveShoppingCart(shoppingCart);

                        // Marks the order as complete
                        complete = true;
                        break;
                }
            } while (!complete);

            System.Console.ReadLine();
        }
예제 #2
0
 public void AddBookOrder(BookOrder order)
 {
     bookOrders.Add(order);
 }
예제 #3
0
        public void MainLoop()
        {
            do
            {
                // Get the selected book
                var bookInput = _console.ReadFromOptions(
                    "The Following Books are available",
                    "What book would you like to add?",
                    "That is not a valid book",
                    _bookOptions
                    );

                // Break program execution if the user desires to quit
                // This is the only exit from the loop
                if (bookInput == _bookOptions.ContinueOption)
                    break;

                // Get the amount of copies
                int bookAmount;
                do
                {
                    bookAmount = _console.ReadValidInt(
                        "How many would you like?",
                        "That was not a valid response."
                        );
                } while (bookAmount < 1);

                // Add the order to the shoppingcard
                var currentOrder = new BookOrder(_books[_bookOptions.Index(bookInput)], bookAmount);
                _shoppingCart.AddBookOrder(currentOrder);

                // Display a message to the user letting them know an item was added
                _console.WriteASuccessLine(
                    String.Format("{0}  was added to your Shopping cart.", currentOrder));
            } while (true);

            // Clear the output buffer
            _console.Print();
        }