Esempio n. 1
0
        /// <summary> Shows HTML source for Shopping Cart of one customer. (In table with price calculation) </summary>
        /// <param name="customer">Customer object.</param>
        /// <param name="model">Model object. (For getting book details) </param>
        public void ShowShoppingCart(Customer customer, ModelStore model)
        {
            var cartCount = customer.ShoppingCart.Items.Count;

            PrintHeadAndMenu(customer.FirstName, cartCount);

            if (cartCount == 0)  // Special output case when cart is empty
            {
                stdOut.WriteLine("    Your shopping cart is EMPTY.");
            }
            else
            {
                stdOut.WriteLine("    Your shopping cart:");
                stdOut.WriteLine("    <table>");
                stdOut.WriteLine("        <tr>");
                stdOut.WriteLine("            <th>Title</th>");
                stdOut.WriteLine("            <th>Count</th>");
                stdOut.WriteLine("            <th>Price</th>");
                stdOut.WriteLine("            <th>Actions</th>");
                stdOut.WriteLine("        </tr>");
                decimal totalPrice = 0;
                foreach (var item in customer.ShoppingCart.Items)
                {
                    var book           = model.GetBook(item.BookId);
                    var bookTotalPrice = item.Count * book.Price;
                    totalPrice += bookTotalPrice;

                    stdOut.WriteLine("        <tr>");
                    stdOut.WriteLine("            <td><a href=\"/Books/Detail/" + item.BookId + "\">" + book.Title + "</a></td>");
                    stdOut.WriteLine("            <td>" + item.Count + "</td>");
                    if (item.Count == 1)
                    {
                        stdOut.WriteLine("            <td>" + bookTotalPrice + " EUR</td>");
                    }
                    else
                    {
                        stdOut.WriteLine("            <td>" + item.Count + " * " + book.Price + " = " + bookTotalPrice + " EUR</td>");
                    }
                    stdOut.WriteLine("            <td>&lt;<a href=\"/ShoppingCart/Remove/" + item.BookId + "\">Remove</a>&gt;</td>");
                    stdOut.WriteLine("        </tr>");
                }
                stdOut.WriteLine("    </table>");
                stdOut.WriteLine("    Total price of all items: " + totalPrice + " EUR");
            }


            stdOut.WriteLine("</body>");
            stdOut.WriteLine("</html>");

            PrintCmdEnd();
        }
Esempio n. 2
0
        /// <summary> Parses one command and makes action or shows view. </summary>
        /// <param name="line">One command as string.</param>
        /// <exception cref="System.Exception">Throws when command is not correct by definition.</exception>
        internal void ProcessCommand(string line)
        {
            string[] tokens = line.Split(' ');
            if (tokens.Length != 3)
            {
                throw new Exception("Command length wrong.");
            }

            if (tokens[0] != "GET")
            {
                throw new Exception("Not a GET request.");
            }

            if (tokens[2].Substring(0, domain.Length) != domain)
            {
                throw new Exception("Wrong domain.");
            }

            Customer customer;

            if ((customer = model.GetCustomer(int.Parse(tokens[1]))) == null)
            {
                throw new Exception("Customer doesn't exists.");
            }


            string[] cmds = tokens[2].
                            Substring(domain.Length).
                            Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            if (cmds.Length == 0)
            {
                throw new Exception("HTTP address is too short.");
            }

            switch (cmds[0])
            {
            case "Books":
                if (cmds.Length == 1)
                {
                    view.ShowBooks(model.GetBooks(), customer);
                }
                else if (cmds.Length == 3 && cmds[1] == "Detail")
                {
                    Book detail = model.GetBook(int.Parse(cmds[2]));
                    if (detail == null)
                    {
                        throw new Exception("Book Detail not found.");
                    }
                    view.ShowBookDetail(detail, customer);
                }
                else
                {
                    throw new Exception("Books HTTP address is wrong.");
                }
                break;

            case "ShoppingCart":
                if (cmds.Length == 1)
                {
                    view.ShowShoppingCart(customer, model);
                }
                else if (cmds.Length == 3)
                {
                    switch (cmds[1])
                    {
                    case "Add":
                        AddBook(customer, model.GetBook(int.Parse(cmds[2])));
                        break;

                    case "Remove":
                        RemoveBook(customer, model.GetBook(int.Parse(cmds[2])));
                        break;

                    default:
                        throw new Exception("ShoppingCart HTTP address is wrong.");
                    }
                }
                else
                {
                    throw new Exception("ShoppingCart HTTP address is wrong.");
                }
                break;

            default:
                throw new Exception("HTTP address is wrong.");
            }
        }
Esempio n. 3
0
        /// <summary> Shows HTML source for Shopping Cart of one customer. (In table with price calculation) </summary>
        /// <param name="customer">Customer object.</param>
        /// <param name="model">Model object. (For getting book details) </param>
        public void ShowShoppingCart(Customer customer, ModelStore model)
        {
            var cartCount = customer.ShoppingCart.Items.Count;
            PrintHeadAndMenu(customer.FirstName, cartCount);

            if(cartCount == 0)  // Special output case when cart is empty
                stdOut.WriteLine("    Your shopping cart is EMPTY.");
            else {
                stdOut.WriteLine("    Your shopping cart:");
                stdOut.WriteLine("    <table>");
                stdOut.WriteLine("        <tr>");
                stdOut.WriteLine("            <th>Title</th>");
                stdOut.WriteLine("            <th>Count</th>");
                stdOut.WriteLine("            <th>Price</th>");
                stdOut.WriteLine("            <th>Actions</th>");
                stdOut.WriteLine("        </tr>");
                decimal totalPrice = 0;
                foreach(var item in customer.ShoppingCart.Items) {
                    var book = model.GetBook(item.BookId);
                    var bookTotalPrice = item.Count * book.Price;
                    totalPrice += bookTotalPrice;

                    stdOut.WriteLine("        <tr>");
                    stdOut.WriteLine("            <td><a href=\"/Books/Detail/" + item.BookId + "\">" + book.Title + "</a></td>");
                    stdOut.WriteLine("            <td>" + item.Count + "</td>");
                    if(item.Count == 1)
                        stdOut.WriteLine("            <td>" + bookTotalPrice + " EUR</td>");
                    else
                        stdOut.WriteLine("            <td>" + item.Count + " * " + book.Price + " = " + bookTotalPrice + " EUR</td>");
                    stdOut.WriteLine("            <td>&lt;<a href=\"/ShoppingCart/Remove/" + item.BookId + "\">Remove</a>&gt;</td>");
                    stdOut.WriteLine("        </tr>");
                }
                stdOut.WriteLine("    </table>");
                stdOut.WriteLine("    Total price of all items: " + totalPrice + " EUR");
            }

            stdOut.WriteLine("</body>");
            stdOut.WriteLine("</html>");

            PrintCmdEnd();
        }