示例#1
0
        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":

                        Book book = new Book
                        {
                            Id    = int.Parse(tokens[1]), Title = tokens[2], Author = tokens[3],
                            Price = decimal.Parse(tokens[4])
                        };
                        if (book.Id < 0 || book.Price < 0 || tokens.Length != 5)
                        {
                            return(null);
                        }

                        store.books.Add(book);

                        break;

                    case "CUSTOMER":
                        Customer cust = new Customer
                        {
                            Id = int.Parse(tokens[1]), FirstName = tokens[2], LastName = tokens[3]
                        };

                        if (cust.Id < 0 || tokens.Length != 4)
                        {
                            return(null);
                        }
                        store.customers.Add(cust);
                        break;

                    case "CART-ITEM":
                        var customer = store.GetCustomer(int.Parse(tokens[1]));
                        if (customer == null)
                        {
                            return(null);
                        }

                        ShoppingCartItem item = new ShoppingCartItem
                        {
                            BookId = int.Parse(tokens[2]), Count = int.Parse(tokens[3])
                        };

                        if (store.GetBook(item.BookId) == null || item.Count < 0)
                        {
                            return(null);
                        }

                        customer.ShoppingCart.Items.Add(item);
                        break;

                    default:
                        return(null);
                    }
                }
            } catch (Exception ex) {
                if (ex is FormatException || ex is IndexOutOfRangeException)
                {
                    return(null);
                }
                throw;
            }

            return(store);
        }
示例#2
0
 public void RemoveItem(ShoppingCartItem item)
 {
     Items.Remove(item);
 }