예제 #1
0
        /// <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);
        }
예제 #2
0
 /// <summary>
 /// Adds item to its customer's shoping cart.
 /// </summary>
 /// <param name="shoppingCartItem">Book object to be added.</param>
 public void AddItem(ShoppingCartItem shoppingCartItem)
 {
     Items.Add(shoppingCartItem);
 }