Esempio n. 1
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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        public static void Render(int custId, ModelStore modelStore)
        {
            Customer customer = modelStore.GetCustomer(custId);

            Console.WriteLine("<!DOCTYPE html>");
            Console.WriteLine("<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">");
            Console.WriteLine("<head>");
            Console.WriteLine("	<meta charset=\"utf-8\" />");
            Console.WriteLine("	<title>Nezarka.net: Online Shopping for Books</title>");
            Console.WriteLine("</head>");
            Console.WriteLine("<body>");
            Console.WriteLine("	<style type=\"text/css\">");
            Console.WriteLine("		table, th, td {");
            Console.WriteLine("			border: 1px solid black;");
            Console.WriteLine("			border-collapse: collapse;");
            Console.WriteLine("		}");
            Console.WriteLine("		table {");
            Console.WriteLine("			margin-bottom: 10px;");
            Console.WriteLine("		}");
            Console.WriteLine("		pre {");
            Console.WriteLine("			line-height: 70%;");
            Console.WriteLine("		}");
            Console.WriteLine("	</style>");
            Console.WriteLine("	<h1><pre>  v,<br />Nezarka.NET: Online Shopping for Books</pre></h1>");
            Console.WriteLine("	" + customer.FirstName + ", here is your menu:");
            Console.WriteLine("	<table>");
            Console.WriteLine("		<tr>");
            Console.WriteLine("			<td><a href=\"/Books\">Books</a></td>");
            Console.WriteLine("			<td><a href=\"/ShoppingCart\">Cart ("+ customer.ShoppingCart.Items.Count.ToString() + ")</a></td>");
            Console.WriteLine("		</tr>");
            Console.WriteLine("	</table>");
        }
Esempio n. 5
0
 private bool customerExists(int custID)
 {
     if (modelStore.GetCustomer(custID) == null)
     {
         return(false);
     }
     return(true);
 }
Esempio n. 6
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>");
        }
Esempio n. 7
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);
            }
        }
Esempio n. 8
0
        public static void Remove(int custId, int bookId, ModelStore modelStore)
        {
            ShoppingCart     cart = modelStore.GetCustomer(custId).ShoppingCart;
            ShoppingCartItem item = cart.GetItem(bookId);

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

            item.Count -= 1;
            if (item.Count == 0)
            {
                cart.RemoveItem(item);
            }

            ViewShoppingCart.Render(custId, modelStore);
        }