/// <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> Main function which handles Loading from input and processing to output </summary> /// <param name="args">Program arguments.</param> /// <param name="stdIn">Input stream object.</param> /// <param name="stdOut">Output stream object.</param> public static void Run(string[] args, TextReader stdIn, TextWriter stdOut) { ModelStore store = ModelStore.LoadFrom(stdIn); if (store == null) { stdOut.Write("Data error."); return; } Viewer view = new Viewer(stdOut); Controller controller = new Controller(store, view); controller.ProcessAllCommands(stdIn); }
public static ModelStore LoadFrom(TextReader reader) { var store = new ModelStore(); try { if (reader.ReadLine() != "DATA-BEGIN") { return(null); } while (true) { string line = reader.ReadLine(); if (line == null) { return(null); } else if (line == "DATA-END") { break; } string[] tokens = line.Split(';'); switch (tokens[0]) { case "BOOK": store.books.Add(new Book { Id = int.Parse(tokens[1]), Title = tokens[2], Author = tokens[3], Price = decimal.Parse(tokens[4]) }); break; case "CUSTOMER": store.customers.Add(new Customer { Id = int.Parse(tokens[1]), FirstName = tokens[2], LastName = tokens[3] }); break; case "CART-ITEM": var customer = store.GetCustomer(int.Parse(tokens[1])); if (customer == null) { return(null); } customer.ShoppingCart.Items.Add(new ShoppingCartItem { BookId = int.Parse(tokens[2]), Count = int.Parse(tokens[3]) }); break; default: return(null); } } } catch (Exception ex) { if (ex is FormatException || ex is IndexOutOfRangeException) { return(null); } throw; } return(store); }
public Controller(ModelStore model, Viewer view) { this.model = model; this.view = view; }
/// <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(); }
public static ModelStore LoadFrom(TextReader reader) { var store = new ModelStore(); try { if(reader.ReadLine() != "DATA-BEGIN") { return null; } while(true) { string line = reader.ReadLine(); if(line == null) { return null; } else if(line == "DATA-END") { break; } string[] tokens = line.Split(';'); switch(tokens[0]) { case "BOOK": store.books.Add(new Book { Id = int.Parse(tokens[1]), Title = tokens[2], Author = tokens[3], Price = decimal.Parse(tokens[4]) }); break; case "CUSTOMER": store.customers.Add(new Customer { Id = int.Parse(tokens[1]), FirstName = tokens[2], LastName = tokens[3] }); break; case "CART-ITEM": var customer = store.GetCustomer(int.Parse(tokens[1])); if(customer == null) { return null; } customer.ShoppingCart.Items.Add(new ShoppingCartItem { BookId = int.Parse(tokens[2]), Count = int.Parse(tokens[3]) }); break; default: return null; } } } catch(Exception ex) { if(ex is FormatException || ex is IndexOutOfRangeException) { return null; } throw; } return store; }