/// <summary> /// Clears the memmory. /// </summary> public void DisposeStore() { this.Nezarka = null; }
/// <summary> /// Parse INPUT data and create new ModelStore object. /// </summary> /// <param name="wrongInput">Flag signaling weather the INPUT was correct or not.</param> public void GetData(TextReader dataReader, out bool wrongInput) { wrongInput = default(bool); this.Nezarka = ModelStore.LoadFrom(dataReader, out wrongInput); }
/// <summary> /// Prints HTML containing list of all books the customer has. /// </summary> /// <param name="customer">Customer object from Nezarka's database.</param> /// <param name="store">Nezarka it self. Used solely for it's database records.</param> public void GetBooksHtml(Customer customer, ModelStore store) { IList <Book> books = store.GetBooks(); Writer.WriteLine("<!DOCTYPE html>"); Writer.WriteLine("<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">"); Writer.WriteLine("<head>"); Writer.WriteLine(" <meta charset=\"utf-8\" />"); Writer.WriteLine(" <title>Nezarka.net: Online Shopping for Books</title>"); Writer.WriteLine("</head>"); Writer.WriteLine("<body>"); Writer.WriteLine(" <style type=\"text/css\">"); Writer.WriteLine(" table, th, td {"); Writer.WriteLine(" border: 1px solid black;"); Writer.WriteLine(" border-collapse: collapse;"); Writer.WriteLine(" }"); Writer.WriteLine(" table {"); Writer.WriteLine(" margin-bottom: 10px;"); Writer.WriteLine(" }"); Writer.WriteLine(" pre {"); Writer.WriteLine(" line-height: 70%;"); Writer.WriteLine(" }"); Writer.WriteLine(" </style>"); Writer.WriteLine(" <h1><pre> v,<br />Nezarka.NET: Online Shopping for Books</pre></h1>"); Writer.WriteLine(" {0}, here is your menu:", customer.FirstName); Writer.WriteLine(" <table>"); Writer.WriteLine(" <tr>"); Writer.WriteLine(" <td><a href=\"/Books\">Books</a></td>"); Writer.WriteLine($" <td><a href=\"/ShoppingCart\">Cart ({customer.ShoppingCart.Items.Count})</a></td>"); Writer.WriteLine(" </tr>"); Writer.WriteLine(" </table>"); Writer.WriteLine(" Our books for you:"); Writer.WriteLine(" <table>"); if (books.Count > 0) { Writer.WriteLine(" <tr>"); } for (int i = 0; i < books.Count; i++) { if (i % 3 == 0 && i != 0) { Writer.WriteLine(" </tr>"); Writer.WriteLine(" <tr>"); } Writer.WriteLine(" <td style=\"padding: 10px;\">"); Writer.WriteLine(" <a href=\"/Books/Detail/{0}\">{1}</a><br />", books[i].Id, books[i].Title); Writer.WriteLine(" Author: {0}<br />", books[i].Author); Writer.WriteLine(" Price: {0} EUR <<a href=\"/ShoppingCart/Add/{1}\">Buy</a>>", books[i].Price, books[i].Id); Writer.WriteLine(" </td>"); } if (books.Count > 0) { Writer.WriteLine(" </tr>"); } Writer.WriteLine(" </table>"); Writer.WriteLine("</body>"); Writer.WriteLine("</html>"); Writer.Flush(); }
/// <summary> /// Prints HTML of customers shoping cart. /// </summary> /// <param name="customer">Customer object from Nezarka's database.</param> /// <param name="store">Nezarka it self. Used solely for it's database records.</param> public void GetShopingCart(Customer customer, ModelStore store) { Writer.WriteLine("<!DOCTYPE html>"); Writer.WriteLine("<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">"); Writer.WriteLine("<head>"); Writer.WriteLine(" <meta charset=\"utf-8\" />"); Writer.WriteLine(" <title>Nezarka.net: Online Shopping for Books</title>"); Writer.WriteLine("</head>"); Writer.WriteLine("<body>"); Writer.WriteLine(" <style type=\"text/css\">"); Writer.WriteLine(" table, th, td {"); Writer.WriteLine(" border: 1px solid black;"); Writer.WriteLine(" border-collapse: collapse;"); Writer.WriteLine(" }"); Writer.WriteLine(" table {"); Writer.WriteLine(" margin-bottom: 10px;"); Writer.WriteLine(" }"); Writer.WriteLine(" pre {"); Writer.WriteLine(" line-height: 70%;"); Writer.WriteLine(" }"); Writer.WriteLine(" </style>"); Writer.WriteLine(" <h1><pre> v,<br />Nezarka.NET: Online Shopping for Books</pre></h1>"); Writer.WriteLine($" {customer.FirstName}, here is your menu:"); Writer.WriteLine(" <table>"); Writer.WriteLine(" <tr>"); Writer.WriteLine(" <td><a href=\"/Books\">Books</a></td>"); Writer.WriteLine($" <td><a href=\"/ShoppingCart\">Cart ({customer.ShoppingCart.Items.Count})</a></td>"); Writer.WriteLine(" </tr>"); Writer.WriteLine(" </table>"); if (customer.ShoppingCart.Items.Count == 0) { Writer.WriteLine(" Your shopping cart is EMPTY."); } else { 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 SUM = 0; for (int i = 0; i < customer.ShoppingCart.Items.Count; i++) { Book b = store.GetBook(customer.ShoppingCart.Items[i].BookId); Writer.WriteLine(" <tr>"); Writer.WriteLine($" <td><a href=\"/Books/Detail/{b.Id}\">{b.Title}</a></td>"); Writer.WriteLine($" <td>{customer.ShoppingCart.Items[ i ].Count}</td>"); if (customer.ShoppingCart.Items[i].Count > 1) { Writer.WriteLine($" <td>{customer.ShoppingCart.Items[ i ].Count} * {b.Price} = {customer.ShoppingCart.Items[ i ].Count * b.Price} EUR</td>"); } else { Writer.WriteLine($" <td>{b.Price} EUR</td>"); } SUM += (customer.ShoppingCart.Items[i].Count * b.Price); Writer.WriteLine($" <td><<a href=\"/ShoppingCart/Remove/{b.Id}\">Remove</a>></td>"); Writer.WriteLine(" </tr>"); } Writer.WriteLine(" </table>"); Writer.WriteLine($" Total price of all items: {SUM} EUR"); } Writer.WriteLine("</body>"); Writer.WriteLine("</html>"); Writer.Flush(); }
/// <summary> /// Parses input data to database friendly format an includes them in it. /// If there is any error in the given data, process is aborted and empty object is returned. /// </summary> /// <param name="reader">Text reader. Must be set to read STDIN.</param> /// <param name="wrongInput">Flag that carries information of error in data loading / parsing.</param> /// <returns>Database object if the data were correct. NULL if they were not.</returns> public static ModelStore LoadFrom(TextReader reader, out bool wrongInput) { var store = new ModelStore(); wrongInput = true; try { if (reader.ReadLine() != "DATA-BEGIN") { return(null); } while (true) { wrongInput = true; string line = reader.ReadLine(); if (line == null) { return(null); } else if (line == "DATA-END") { wrongInput = false; break; } string[] tokens = line.Split(';'); switch (tokens[0]) { case "BOOK": if (Book.TryParse(tokens, out Book book)) { //is this book original? if (store.GetBook(book.Id) == null) { store.books.Add(book); wrongInput = false; } } break; case "CUSTOMER": if (Customer.TryParse(tokens, out Customer customer)) { //is this customer new? if (store.GetCustomer(customer.Id) == null) { store.customers.Add(customer); wrongInput = false; } } break; case "CART-ITEM": if (ShoppingCartItem.TryParse(tokens, out ShoppingCartItem shoppingCartItem, out int userID)) { //does this customer exist? if (store.GetCustomer(userID) == null) { break; } //does the book exist? if (store.GetBook(shoppingCartItem.BookId) == null) { break; } Customer existingCustomer = store.GetCustomer(userID); existingCustomer.ShoppingCart.AddItem(shoppingCartItem); wrongInput = false; } break; default: break; } if (wrongInput) { return(default(ModelStore)); } } } catch (Exception ex) { if (ex is FormatException || ex is IndexOutOfRangeException) { return(null); } throw; } return(store); }