Beispiel #1
0
 public Controller(ModelStore modelStore, PagePrinter pagePrinter)
 {
     this.modelStore  = modelStore;
     this.pagePrinter = pagePrinter;
 }
Beispiel #2
0
 private static void InitModelStore()
 {
     modelStore = ModelStore.LoadFrom(Console.In);
 }
Beispiel #3
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":
                    {
                        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);
        }
Beispiel #4
0
 public PagePrinter(ModelStore modelStore)
 {
     this.modelStore = modelStore;
 }