public void Detail(int bookID, ModelStore model) { Console.WriteLine("\tBook details:"); Console.WriteLine("\t<h2>{0}</h2>", model.GetBook(bookID).Title); Console.WriteLine("\t<p style=\"margin-left: 20px\">"); Console.WriteLine("\tAuthor: {0}<br />", model.GetBook(bookID).Author); Console.WriteLine("\tPrice: {0} EUR<br />", model.GetBook(bookID).Price); Console.WriteLine("\t</p>"); Console.WriteLine("\t<h3><<a href=\"/ShoppingCart/Add/{0}\">Buy this book</a>></h3>", bookID); }
public static void GenCart(TextWriter writer, string CustFirtsName, List <ShoppingCartItem> Items, ModelStore store) { GenFirstHead(writer); GenStyle(writer); GenCommonHeader(writer, CustFirtsName, Items.Count); writer.WriteLine(" Your shopping cart:"); writer.WriteLine(" <table>"); writer.WriteLine(" <tr>"); writer.WriteLine(" <th>Title</th>"); writer.WriteLine(" <th>Count</th>"); writer.WriteLine(" <th>Price</th>"); writer.WriteLine(" <th>Actions</th>"); writer.WriteLine(" </tr>"); decimal TotalPrice = 0; for (int i = 0; i < Items.Count; i++) { int NumOfBooks = Items[i].Count; Book book = store.GetBook(Items[i].BookId); GenCartItem(writer, book, NumOfBooks, ref TotalPrice); } writer.WriteLine(" </table>"); writer.WriteLine($" Total price of all items: {TotalPrice} EUR"); writer.WriteLine("</body>"); writer.WriteLine("</html>"); writer.Flush(); }
public void ShopingCart(int CustID, ModelStore model) { decimal price = 0; Console.WriteLine("\tYour shopping cart:"); Console.WriteLine("\t<table>"); Console.WriteLine("\t\t<tr>"); Console.WriteLine("\t\t\t<th>Title</th>"); Console.WriteLine("\t\t\t<th>Count</th>"); Console.WriteLine("\t\t\t<th>Price</th>"); Console.WriteLine("\t\t\t<th>Actions</th>"); Console.WriteLine("\t\t</tr>"); foreach (ShoppingCartItem item in model.GetCustomer(CustID).ShoppingCart.Items) { Console.WriteLine("\t\t<tr>"); Console.WriteLine("\t\t\t<td><a href=\"/Books/Detail/{0}\">{1}</a></td>", item.BookId, model.GetBook(item.BookId).Title); Console.WriteLine("\t\t\t<td>{0}</td>", item.Count); if (item.Count > 1) { Console.WriteLine("\t\t\t<td>{0} * {1} = {2} EUR</td>", item.Count, model.GetBook(item.BookId).Price, (model.GetBook(item.BookId).Price *item.Count)); } else { Console.WriteLine("\t\t\t<td>{0} EUR</td>", model.GetBook(item.BookId).Price); } Console.WriteLine("\t\t\t<td><<a href=\"/ShoppingCart/Remove/{0}\">Remove</a>></td>", item.BookId); Console.WriteLine("\t\t</tr>"); price += (item.Count * model.GetBook(item.BookId).Price); } Console.WriteLine("\t</table>"); Console.WriteLine("\tTotal price of all items: {0} EUR", price); }
public void Check() { try { if (ControlURL()) { if (Url.Length > 3 && Url[2] == "Books") { if (Url[3] == "Detail") { Int32.TryParse(Url[4], out IdOfBook); if (IdOfBook != -1) { if (model.GetBooks().Contains(model.GetBook(IdOfBook))) { view.BooksDetail(CustomerId, IdOfBook, model); } else { view.Invalid(); } } } else { view.Invalid(); } } else if (Url.Length > 3 && Url[2] == "ShoppingCart") { if (Url[3] == "Add") { Int32.TryParse(Url[4], out IdOfBook); if (IdOfBook != -1) { if (!model.GetBooks().Contains(model.GetBook(IdOfBook))) { view.Invalid(); return; } if (model.GetCustomer(CustomerId).ShoppingCart.Items.Contains(model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook))) { model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook).Count++; } else { model.GetCustomer(CustomerId).ShoppingCart.Items.Add(new ShoppingCartItem()); model.GetCustomer(CustomerId).ShoppingCart.Items[model.GetCustomer(CustomerId).ShoppingCart.Items.Count - 1].BookId = IdOfBook; model.GetCustomer(CustomerId).ShoppingCart.Items[model.GetCustomer(CustomerId).ShoppingCart.Items.Count - 1].Count = 1; } view.ShoppingCart(CustomerId, model); } } else if (Url[3] == "Remove") { Int32.TryParse(Url[4], out IdOfBook); if (IdOfBook != -1) { if (!model.GetBooks().Contains(model.GetBook(IdOfBook))) { view.Invalid(); return; } if (model.GetCustomer(CustomerId).ShoppingCart.Items.Contains(model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook))) { if (model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook).Count >= 2) { model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook).Count--; } else if (model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook).Count == 1) { model.GetCustomer(CustomerId).ShoppingCart.Items.Remove(model.GetCustomer(CustomerId).ShoppingCart.Items.Find(id => id.BookId == IdOfBook)); } if (model.GetCustomer(CustomerId).ShoppingCart.Items.Count == 0) { view.ShoppingCartEmpty(CustomerId, model); } else { view.ShoppingCart(CustomerId, model); } } else { view.Invalid(); } } } else { view.Invalid(); } } else { if (Url[2] == "Books") { if (model.GetCustomers().Count == 0) { view.Invalid(); return; } view.Books(CustomerId, model); } else if (Url[2] == "ShoppingCart") { if (model.GetCustomers().Count == 0) { view.Invalid(); return; } if (model.GetCustomer(CustomerId).ShoppingCart.Items.Count == 0) { view.ShoppingCartEmpty(CustomerId, model); } else { view.ShoppingCart(CustomerId, model); } } else { view.Invalid(); } } } else { view.Invalid(); } } catch { view.Invalid(); } }
public static void ReadAdnDoRequests(TextReader reader, TextWriter writer, ModelStore store) { Regex BookList = new Regex(@"^GET \d+ http://www.nezarka.net/Books$"); Regex BookDetail = new Regex(@"^GET \d+ http://www.nezarka.net/Books/Detail/\d+$"); Regex ShopingCart = new Regex(@"^GET \d+ http://www.nezarka.net/ShoppingCart$"); Regex ShopingCartAddItem = new Regex(@"^GET \d+ http://www.nezarka.net/ShoppingCart/Add/\d+$"); Regex ShopingCartRemoveItem = new Regex(@"^GET \d+ http://www.nezarka.net/ShoppingCart/Remove/\d+$"); char[] delims = new char[] { ' ', '/' }; // bool first = true; string line = reader.ReadLine(); while (line != null && line != "") { string[] Records = line.Split(delims, StringSplitOptions.RemoveEmptyEntries); /*if (!first) * { * writer.WriteLine("===="); * } * first = false; */ if (BookList.Match(line).Length > 0) { int cusID = Int32.Parse(Records[1]); Customer cust = store.GetCustomer(cusID); if (cust == null) { View.GenInvalidRequest(writer); writer.WriteLine("===="); writer.Flush(); line = reader.ReadLine(); continue; } string firstName = cust.FirstName; List <Book> Books = store.GetBooks(); int numItems = cust.CountItemsInCart(); View.GenBookList(writer, firstName, numItems, Books); } else if (BookDetail.Match(line).Length > 0) { int cusID = Int32.Parse(Records[1]); int bookID = Int32.Parse(Records[Records.Length - 1]); Customer cust = store.GetCustomer(cusID); Book b = store.GetBook(bookID); if (cust == null || b == null) { View.GenInvalidRequest(writer); writer.WriteLine("===="); writer.Flush(); line = reader.ReadLine(); continue; } string firstNsame = cust.FirstName; int numItems = cust.CountItemsInCart(); View.GenBookDetail(writer, firstNsame, numItems, b.Title, b.Author, b.Price, b.Id); } else if (ShopingCart.Match(line).Length > 0) { int cusID = Int32.Parse(Records[1]); Customer cust = store.GetCustomer(cusID); if (cust == null) { View.GenInvalidRequest(writer); writer.WriteLine("===="); writer.Flush(); line = reader.ReadLine(); continue; } string firstName = cust.FirstName; if (cust.CountItemsInCart() == 0) { View.GenCartEmpty(writer, firstName); writer.WriteLine("===="); writer.Flush(); line = reader.ReadLine(); continue; } List <ShoppingCartItem> cart = cust.GetCart(); View.GenCart(writer, firstName, cart, store); } else if (ShopingCartAddItem.Match(line).Length > 0) { int cusID = Int32.Parse(Records[1]); int bookID = Int32.Parse(Records[Records.Length - 1]); Customer cust = store.GetCustomer(cusID); Book b = store.GetBook(bookID); if (cust == null || b == null) { View.GenInvalidRequest(writer); writer.WriteLine("===="); writer.Flush(); line = reader.ReadLine(); continue; } //TODO: create shopping cart? cust.ShoppingCart.AddItem(bookID); string firstName = cust.FirstName; List <ShoppingCartItem> cart = cust.GetCart(); View.GenCart(writer, firstName, cart, store); } else if (ShopingCartRemoveItem.Match(line).Length > 0) { int cusID = Int32.Parse(Records[1]); int bookID = Int32.Parse(Records[Records.Length - 1]); Customer cust = store.GetCustomer(cusID); Book b = store.GetBook(bookID); if (cust == null || b == null) { View.GenInvalidRequest(writer); writer.WriteLine("===="); writer.Flush(); line = reader.ReadLine(); continue; } bool RemoveSuccesful = cust.ShoppingCart.RemoveItem(bookID); if (RemoveSuccesful) { string firstName = cust.FirstName; List <ShoppingCartItem> cart = cust.GetCart(); if (cust.CountItemsInCart() == 0) { View.GenCartEmpty(writer, firstName); writer.WriteLine("===="); writer.Flush(); line = reader.ReadLine(); continue; } View.GenCart(writer, firstName, cart, store); } else { View.GenInvalidRequest(writer); } } else { View.GenInvalidRequest(writer); } writer.WriteLine("===="); writer.Flush(); line = reader.ReadLine(); } }