コード例 #1
0
ファイル: Program.cs プロジェクト: lunakv/Skola
        public void ShowShoppingCart(Customer cust)
        {
            PrintCommonHeader(cust);

            if (cust.ShoppingCart.Items.Count == 0)
            {
                ShowEmptyCart();
                return;
            }

            _output.WriteLine("	Your shopping cart:");
            _output.WriteLine("	<table>");
            _output.WriteLine("		<tr>");
            _output.WriteLine("			<th>Title</th>");
            _output.WriteLine("			<th>Count</th>");
            _output.WriteLine("			<th>Price</th>");
            _output.WriteLine("			<th>Actions</th>");
            _output.WriteLine("		</tr>");

            int totalCost = 0;

            for (int i = 0; i < cust.ShoppingCart.Items.Count; i++)
            {
                ShoppingCartItem currentItem = cust.ShoppingCart.Items[i];
                Book             currentBook = _model.GetBook(currentItem.BookId);
                int itemPrice = currentItem.Count * currentBook.Price;
                totalCost += itemPrice;

                _output.WriteLine("		<tr>");
                _output.WriteLine("			<td><a href=\"/Books/Detail/{0}\">{1}</a></td>", currentBook.Id, currentBook.Title);
                _output.WriteLine("			<td>{0}</td>", currentItem.Count);
                if (currentItem.Count == 1)
                {
                    _output.WriteLine("			<td>{0} EUR</td>", currentBook.Price);
                }
                else
                {
                    _output.WriteLine("			<td>{0} * {1} = {2} EUR</td>", currentItem.Count, currentBook.Price, itemPrice);
                }
                _output.WriteLine("			<td>&lt;<a href=\"/ShoppingCart/Remove/{0}\">Remove</a>&gt;</td>", currentBook.Id);
                _output.WriteLine("		</tr>");
            }

            _output.WriteLine("	</table>");
            _output.WriteLine("	Total price of all items: {0} EUR", totalCost);
            _output.WriteLine("</body>");
            _output.WriteLine("</html>");
            _output.WriteLine("====");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: lunakv/Skola
        public void ProcessRequest(string request)
        {
            if (_parser.GetRequestType(request) != "GET")
            {
                _view.ShowInvalidRequest();
                return;
            }

            int      custID = _parser.GetCustID(request);
            Customer cust   = _model.GetCustomer(custID);

            if (cust == null)
            {
                _view.ShowInvalidRequest();
                return;
            }

            string command = _parser.GetCommand(request);

            if (command == null)
            {
                _view.ShowInvalidRequest();
                return;
            }

            if (command == "Books")
            {
                _view.ShowAllBooks(cust);
            }
            else if (command == "ShoppingCart")
            {
                _view.ShowShoppingCart(cust);
            }
            else
            {
                int  bookID = _parser.GetBookID(command);
                Book book   = _model.GetBook(bookID);
                if (book == null)
                {
                    _view.ShowInvalidRequest();
                    return;
                }

                if (command.Contains("Books/Detail/"))
                {
                    _view.ShowBookDetail(cust, book);
                }
                else if (command.Contains("ShoppingCart/Add/"))
                {
                    AddBookToCart(cust, bookID);
                }
                else if (command.Contains("ShoppingCart/Remove/"))
                {
                    RemoveBookFromCart(cust, bookID);
                }
                else
                {
                    _view.ShowInvalidRequest();
                }
            }
        }