/// <summary> Adds book to Customer's ShoppingCart or increases book's count by 1.</summary> /// <param name="customer">Customer object for custId from command.</param> /// <param name="book">Book object for BookId from command.</param> /// <exception cref="System.Exception">Throws when customer or book doesn't exist.</exception> internal void AddBook(Customer customer, Book book) { if(customer == null || book == null) throw new Exception("Customer or Book doesn't exist."); ShoppingCart cart = customer.ShoppingCart; ShoppingCartItem cItem = cart.Items.Find(itm => itm.BookId == book.Id); if(cItem != null) cItem.Count++; else cart.Items.Add(new ShoppingCartItem { BookId = book.Id, Count = 1 }); view.ShowShoppingCart(customer, model); }
/// <summary> Shows HTML source for Shopping Cart of one customer. (In table with price calculation) </summary> /// <param name="customer">Customer object.</param> /// <param name="model">Model object. (For getting book details) </param> public void ShowShoppingCart(Customer customer, ModelStore model) { var cartCount = customer.ShoppingCart.Items.Count; PrintHeadAndMenu(customer.FirstName, cartCount); if(cartCount == 0) // Special output case when cart is empty stdOut.WriteLine(" Your shopping cart is EMPTY."); else { stdOut.WriteLine(" Your shopping cart:"); stdOut.WriteLine(" <table>"); stdOut.WriteLine(" <tr>"); stdOut.WriteLine(" <th>Title</th>"); stdOut.WriteLine(" <th>Count</th>"); stdOut.WriteLine(" <th>Price</th>"); stdOut.WriteLine(" <th>Actions</th>"); stdOut.WriteLine(" </tr>"); decimal totalPrice = 0; foreach(var item in customer.ShoppingCart.Items) { var book = model.GetBook(item.BookId); var bookTotalPrice = item.Count * book.Price; totalPrice += bookTotalPrice; stdOut.WriteLine(" <tr>"); stdOut.WriteLine(" <td><a href=\"/Books/Detail/" + item.BookId + "\">" + book.Title + "</a></td>"); stdOut.WriteLine(" <td>" + item.Count + "</td>"); if(item.Count == 1) stdOut.WriteLine(" <td>" + bookTotalPrice + " EUR</td>"); else stdOut.WriteLine(" <td>" + item.Count + " * " + book.Price + " = " + bookTotalPrice + " EUR</td>"); stdOut.WriteLine(" <td><<a href=\"/ShoppingCart/Remove/" + item.BookId + "\">Remove</a>></td>"); stdOut.WriteLine(" </tr>"); } stdOut.WriteLine(" </table>"); stdOut.WriteLine(" Total price of all items: " + totalPrice + " EUR"); } stdOut.WriteLine("</body>"); stdOut.WriteLine("</html>"); PrintCmdEnd(); }
/// <summary> Shows HTML source for one book detail. </summary> /// <param name="item">Book object from model.</param> /// <param name="customer">Customer object.</param> public void ShowBookDetail(Book item, Customer customer) { PrintHeadAndMenu(customer.FirstName, customer.ShoppingCart.Items.Count); stdOut.WriteLine(" Book details:"); stdOut.WriteLine(" <h2>" + item.Title + "</h2>"); stdOut.WriteLine(" <p style=\"margin-left: 20px\">"); stdOut.WriteLine(" Author: " + item.Author + "<br />"); stdOut.WriteLine(" Price: " + item.Price + " EUR<br />"); stdOut.WriteLine(" </p>"); stdOut.WriteLine(" <h3><<a href=\"/ShoppingCart/Add/" + item.Id + "\">Buy this book</a>></h3>"); stdOut.WriteLine("</body>"); stdOut.WriteLine("</html>"); PrintCmdEnd(); }
/// <summary> Shows HTML source for list of books. (In table where there are max 3 books in row) </summary> /// <param name="items">List of books from model.</param> /// <param name="customer">Customer object.</param> public void ShowBooks(IList<Book> items, Customer customer) { PrintHeadAndMenu(customer.FirstName, customer.ShoppingCart.Items.Count); stdOut.WriteLine(" Our books for you:"); stdOut.WriteLine(" <table>"); var index = 0; var booksLeft = items.Count; while(booksLeft > 0) { stdOut.WriteLine(" <tr>"); var columns = Math.Min(3, booksLeft); //This calculates how much items will be displayed in next row for(int i = 0; i < columns; i++) { var book = items[index++]; stdOut.WriteLine(" <td style=\"padding: 10px;\">"); stdOut.WriteLine(" <a href=\"/Books/Detail/" + book.Id + "\">" + book.Title + "</a><br />"); stdOut.WriteLine(" Author: " + book.Author + "<br />"); stdOut.WriteLine(" Price: " + book.Price + " EUR <<a href=\"/ShoppingCart/Add/" + book.Id + "\">Buy</a>>"); stdOut.WriteLine(" </td>"); } booksLeft -= columns; stdOut.WriteLine(" </tr>"); } stdOut.WriteLine(" </table>"); stdOut.WriteLine("</body>"); stdOut.WriteLine("</html>"); PrintCmdEnd(); }
/// <summary> Removes book from Customer's ShoppingCart or decreases book's count by 1. </summary> /// <param name="customer">Customer object for custId from command.</param> /// <param name="book">Book object for BookId from command.</param> /// <exception cref="System.Exception">Throws when Book isn't in Customer's ShoppingCart.</exception> /// <exception cref="System.Exception"> /// Throws when Book isn't in Customer's ShoppingCart or when customer or book doesn't exist. /// </exception> internal void RemoveBook(Customer customer, Book book) { if(customer == null || book == null) throw new Exception("Customer or Book doesn't exist."); ShoppingCart cart = customer.ShoppingCart; ShoppingCartItem cItem = cart.Items.Find(itm => itm.BookId == book.Id); if(cItem == null) throw new Exception("Book doesn't exist in Customers ShoppingCart."); if(cItem.Count > 1) cItem.Count--; else cart.Items.Remove(cItem); view.ShowShoppingCart(customer, model); }