示例#1
0
        private void showShoppingCart(ShoppingCart shoppingCart, ModelStore store)
        {
            if (shoppingCart.Items.Count == 0)
            {
                writer.WriteLine(ind(1) + "Your shopping cart is EMPTY.");
                return;
            }



            int price = 0;

            writer.WriteLine(ind(1) + "Your shopping cart:");
            writer.WriteLine(ind(1) + "<table>");
            writer.WriteLine(ind(2) + "<tr>");
            writer.WriteLine(ind(3) + "<th>Title</th>");
            writer.WriteLine(ind(3) + "<th>Count</th>");
            writer.WriteLine(ind(3) + "<th>Price</th>");
            writer.WriteLine(ind(3) + "<th>Actions</th>");
            writer.WriteLine(ind(2) + "</tr>");

            for (int i = 0; i < shoppingCart.Items.Count; i++)
            {
                int  bookId     = shoppingCart.Items[i].BookId;
                int  count      = shoppingCart.Items[i].Count;
                Book book       = store.GetBook(bookId);
                int  totalPrice = count * book.Price;

                writer.WriteLine(ind(2) + "<tr>");
                writer.WriteLine(ind(3) + "<td><a href=\"/Books/Detail/{0}\">{1}</a></td>", book.Id, book.Title);
                writer.WriteLine(ind(3) + "<td>{0}</td>", count);
                if (count == 1)
                {
                    writer.WriteLine(ind(3) + "<td>{0} EUR</td>", book.Price);
                }
                else
                {
                    writer.WriteLine(ind(3) + "<td>{0} * {1} = {2} EUR</td>", count, book.Price, totalPrice);
                }
                writer.WriteLine(ind(3) + "<td>&lt;<a href=\"/ShoppingCart/Remove/{0}\">Remove</a>&gt;</td>", book.Id);
                writer.WriteLine(ind(2) + "</tr>");

                price += totalPrice;
            }



            writer.WriteLine(ind(1) + "</table>");
            writer.WriteLine(ind(1) + "Total price of all items: {0} EUR", price);
        }
示例#2
0
        void createSubsection(string subsectionType, int bookId, int custId, ModelStore store)
        {
            switch (subsectionType)
            {
            case "Books":
                showBooks(store.GetBooks());
                break;

            case "Detail":
                showDetail(store.GetBook(bookId));
                break;

            case "Add":
            case "Remove":
            case "ShoppingCart":
                showShoppingCart(store.GetCustomer(custId).ShoppingCart, store);
                break;
            }
        }
示例#3
0
        public void parse(string request)
        {
            string[] get = request.Split(' ');
            if (!validateGet(get))
            {
                htmlBuilder.createDump();
                return;
            }
            string[] url = get[2].Split('/');
            if (!validateUrl(url))
            {
                htmlBuilder.createDump();
                return;
            }
            string subsectionType = "";
            string custId         = get[1];
            int    bookIdInt      = 0;
            int    custIdInt      = 0;

            subsectionType = getSubsectionType(url, out string bookId);
            if (!validateSubsectionType(subsectionType, url))
            {
                htmlBuilder.createDump();
                return;
            }

            if (!(int.TryParse(bookId, out bookIdInt) && int.TryParse(custId, out custIdInt)))
            {
                htmlBuilder.createDump();
                return;
            }
            Customer customer = store.GetCustomer(custIdInt);
            Book     book     = store.GetBook(bookIdInt);

            if (customer == null || (book == null && bookIdInt != -5769765))
            {
                htmlBuilder.createDump();
                return;
            }

            switch (subsectionType)
            {
            case "Add":
                if (!addItemToCart(store, bookIdInt, store.GetCustomer(custIdInt).ShoppingCart))
                {
                    htmlBuilder.createDump();
                    return;
                }
                break;

            case "Remove":
                if (!removeItemFromCart(store, bookIdInt, store.GetCustomer(custIdInt).ShoppingCart))
                {
                    htmlBuilder.createDump();
                    return;
                }
                break;

            case "":
                htmlBuilder.createDump();
                return;
            }

            htmlBuilder.createWebPage(subsectionType, bookIdInt, int.Parse(custId), store);
        }