Exemplo n.º 1
0
        public static void Add(int custId, int bookId, ModelStore modelStore)
        {
            Book bookToAdd = modelStore.GetBook(bookId);

            if (bookToAdd == null)
            {
                ViewInvalidRequest.Render();
                return;
            }

            ShoppingCart     cart = modelStore.GetCustomer(custId).ShoppingCart;
            ShoppingCartItem item = cart.GetItem(bookId);

            if (item == null)
            {
                cart.Items.Add(new ShoppingCartItem {
                    BookId = bookId, Count = 1
                });
            }
            else
            {
                item.Count += 1;
            }

            ViewShoppingCart.Render(custId, modelStore);
        }
Exemplo n.º 2
0
        public void RemoveBook_DeleteBook()
        {
            StringWriter output  = new StringWriter();
            ModelStore   model   = ModelMockup();
            Viewer       view    = new Viewer(output);
            Controller   control = new Controller(model, view);

            Customer c = model.GetCustomer(2);
            Book     b = model.GetBook(5);

            control.RemoveBook(c, b);

            Assert.AreEqual(model.GetCustomer(2).ShoppingCart.Items.Find(itm => itm.BookId == b.Id), null);
        }
Exemplo n.º 3
0
        public void AddBook_IncreaseCount()
        {
            StringWriter output  = new StringWriter();
            ModelStore   model   = ModelMockup();
            Viewer       view    = new Viewer(output);
            Controller   control = new Controller(model, view);

            Customer c = model.GetCustomer(2);
            Book     b = model.GetBook(1);

            control.AddBook(c, b);

            Assert.AreEqual(model.GetCustomer(2).ShoppingCart.Items.Find(itm => itm.BookId == b.Id).Count, 4);
        }
Exemplo n.º 4
0
        public static void Render(int custId, ModelStore modelStore)
        {
            ViewHeader.Render(custId, modelStore);

            var shoppingCartItems = modelStore.GetCustomer(custId).ShoppingCart.Items;

            if (shoppingCartItems.Count == 0)
            {
                Console.WriteLine("	Your shopping cart is EMPTY.");
            }
            else
            {
                Console.WriteLine("	Your shopping cart:");
                Console.WriteLine("	<table>");
                Console.WriteLine("		<tr>");
                Console.WriteLine("			<th>Title</th>");
                Console.WriteLine("			<th>Count</th>");
                Console.WriteLine("			<th>Price</th>");
                Console.WriteLine("			<th>Actions</th>");
                Console.WriteLine("		</tr>");

                decimal total = 0;
                foreach (var item in shoppingCartItems)
                {
                    var book = modelStore.GetBook(item.BookId);
                    Console.WriteLine("		<tr>");
                    Console.WriteLine("			<td><a href=\"/Books/Detail/"+ book.Id.ToString() + "\">" + book.Title +
                                      "</a></td>");
                    Console.WriteLine("			<td>"+ item.Count.ToString() + "</td>");

                    Console.WriteLine("			<td>"+ (item.Count == 1 ? book.Price.ToString() : item.Count.ToString() + " * " + book.Price.ToString() + " = " +
                                                   (item.Count * book.Price).ToString()) + " EUR</td>");
                    Console.WriteLine("			<td>&lt;<a href=\"/ShoppingCart/Remove/"+ book.Id.ToString() +
                                      "\">Remove</a>&gt;</td>");
                    Console.WriteLine("		</tr>");

                    total += item.Count * book.Price;
                }

                Console.WriteLine("	</table>");
                Console.WriteLine("	Total price of all items: " + total.ToString() + " EUR");
            }

            Console.WriteLine("</body>");
            Console.WriteLine("</html>");
        }
Exemplo n.º 5
0
        public void RemoveBook_NotExistingBook()
        {
            StringWriter output  = new StringWriter();
            ModelStore   model   = ModelMockup();
            Viewer       view    = new Viewer(output);
            Controller   control = new Controller(model, view);

            Customer c = model.GetCustomer(2);
            Book     b = model.GetBook(2);

            try {
                control.RemoveBook(c, b);
                Assert.Fail("Expected exception was not thrown.");
            }
            catch (Exception ex) {
                Assert.AreEqual("Book doesn't exist in Customers ShoppingCart.", ex.Message);
            }
        }
Exemplo n.º 6
0
        public static void Render(int custId, int bookId, ModelStore modelStore)
        {
            var book = modelStore.GetBook(bookId);

            if (book == null)
            {
                ViewInvalidRequest.Render();
                return;
            }

            ViewHeader.Render(custId, modelStore);

            Console.WriteLine("	Book details:");
            Console.WriteLine("	<h2>" + book.Title + "</h2>");
            Console.WriteLine("	<p style=\"margin-left: 20px\">");
            Console.WriteLine("	Author: " + book.Author + "<br />");
            Console.WriteLine("	Price: " + book.Price.ToString() + " EUR<br />");
            Console.WriteLine("	</p>");
            Console.WriteLine("	<h3>&lt;<a href=\"/ShoppingCart/Add/" + book.Id.ToString() + "\">Buy this book</a>&gt;</h3>");
            Console.WriteLine("</body>");
            Console.WriteLine("</html>");
        }