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 = int.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 void ProcessRequest(string request) { if (_parser.GetRequestType(request) != "GET") { _view.ShowInvalidRequest(); return; } int custID = _parser.GetCustID(request); Customer cust = _model.GetCustomer(custID); if (cust == null) { _view.ShowInvalidRequest(); return; } string command = _parser.GetCommand(request); if (command == null) { _view.ShowInvalidRequest(); return; } if (command == "Books") { _view.ShowAllBooks(cust); } else if (command == "ShoppingCart") { _view.ShowShoppingCart(cust); } else { int bookID = _parser.GetBookID(command); Book book = _model.GetBook(bookID); if (book == null) { _view.ShowInvalidRequest(); return; } if (command.Contains("Books/Detail/")) { _view.ShowBookDetail(cust, book); } else if (command.Contains("ShoppingCart/Add/")) { AddBookToCart(cust, bookID); } else if (command.Contains("ShoppingCart/Remove/")) { RemoveBookFromCart(cust, bookID); } else { _view.ShowInvalidRequest(); } } }