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": { int id = int.Parse(tokens[1]); string title = tokens[2]; string author = tokens[3]; decimal price = decimal.Parse(tokens[4]); if ((id < 0) || (price < 0)) { return(null); } store.books.Add(new Book { Id = id, Title = title, Author = author, Price = price }); } break; case "CUSTOMER": { int id = int.Parse(tokens[1]); if (id < 0) { return(null); } store.customers.Add(new Customer { Id = id, FirstName = tokens[2], LastName = tokens[3] }); } break; case "CART-ITEM": var customer = store.GetCustomer(int.Parse(tokens[1])); int bookId = int.Parse(tokens[2]); int count = int.Parse(tokens[3]); var book = store.GetBook(bookId); if (customer == null || book == null || count < 1) { return(null); } customer.ShoppingCart.Items.Add(new ShoppingCartItem { BookId = bookId, Count = count }); break; default: return(null); } } } catch (Exception ex) { if (ex is FormatException || ex is IndexOutOfRangeException) { return(null); } throw; } return(store); }
public void ProcessCommand(string command) { try { string[] splitedCommand = command.Split(' '); { if (splitedCommand[0] != "GET") { InvalidCommand(); return; } //Checks if customer exists int customerId = int.Parse(splitedCommand[1]); if (modelStore.GetCustomer(customerId) == null) { InvalidCommand(); return; } if (splitedCommand[2].StartsWith("http://www.nezarka.net/")) { splitedCommand[2] = splitedCommand[2].Remove(0, 23); } else { InvalidCommand(); return; } string[] splitedShortCommand = splitedCommand[2].Split('/'); switch (splitedShortCommand[0]) { case "Books": { if (splitedShortCommand.Length == 1) { ShowBooks(customerId); return; } if (splitedShortCommand.Length == 3) { int bookId = int.Parse(splitedShortCommand[2]); if (modelStore.GetBook(bookId) == null) { InvalidCommand(); return; } else { if (splitedShortCommand[1] == "Detail") { ShowBookDetail(customerId, bookId); return; } } } InvalidCommand(); return; } case "ShoppingCart": { if (splitedShortCommand.Length == 1) { ShowShoppingCart(customerId); return; } if (splitedShortCommand.Length == 3) { int bookId = int.Parse(splitedShortCommand[2]); if (modelStore.GetBook(bookId) == null) { InvalidCommand(); return; } else { if (splitedShortCommand[1] == "Add") { AddToCart(customerId, bookId); return; } if (splitedShortCommand[1] == "Remove") { RemoveFromCart(customerId, bookId); return; } } InvalidCommand(); return; } InvalidCommand(); return; } default: InvalidCommand(); return; } } } catch (Exception ex) { if (ex is FormatException || ex is IndexOutOfRangeException) { InvalidCommand(); return; } throw; } }
public void ShowBooks(int customerId) { new PageBooks(modelStore.GetCustomer(customerId), modelStore.GetBooks()).Print(); }