/// <summary> /// Depending on CommandType this method chooses wich HTML page to show the user. /// </summary> /// <param name="type">Carries a type variable. Crucial for this method.</param> /// <param name="customerId">Identification of the customer in Nezarka's database.</param> /// <param name="bookId">Identification of the book in Nezarka's database.</param> public void ExecuteCommand(CommandType type, int customerId, int bookId) { if (!this.Nezarka.IsCustomer(customerId)) { this.storeViewer.InvalidRequest(); this.storeViewer.Indent(); return; } if (type == CommandType.PrintBookDetail || type == CommandType.CartAddBook || type == CommandType.CartRemoveBook) { if (!this.Nezarka.IsBook(bookId)) { this.storeViewer.InvalidRequest(); this.storeViewer.Indent(); return; } } switch (type) { case CommandType.Invalid: this.storeViewer.InvalidRequest(); break; case CommandType.ListBooks: this.storeViewer.GetBooksHtml(Nezarka.GetCustomer(customerId), this.Nezarka); break; case CommandType.PrintBookDetail: this.storeViewer.GetBookDetail(this.Nezarka.GetBook(bookId), this.Nezarka.GetCustomer(customerId)); break; case CommandType.ListCart: this.storeViewer.GetShopingCart(this.Nezarka.GetCustomer(customerId), this.Nezarka); break; case CommandType.CartAddBook: { Customer customer = this.Nezarka.GetCustomer(customerId); customer.ShoppingCart.AddBook(bookId); this.storeViewer.GetShopingCart(this.Nezarka.GetCustomer(customerId), this.Nezarka); } break; case CommandType.CartRemoveBook: { Customer customer = this.Nezarka.GetCustomer(customerId); if (customer.ShoppingCart.RemoveBook(bookId)) { this.storeViewer.GetShopingCart(this.Nezarka.GetCustomer(customerId), this.Nezarka); } else { this.storeViewer.InvalidRequest(); } } break; } this.storeViewer.Indent(); }
/// <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); }