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); }
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); }
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); }
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>"); }
private bool customerExists(int custID) { if (modelStore.GetCustomer(custID) == null) { return(false); } return(true); }
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><<a href=\"/ShoppingCart/Remove/"+ book.Id.ToString() + "\">Remove</a>></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>"); }
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); } }
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); }