Exemplo n.º 1
0
        public void PlaceNewOrder(Store store)
        {
            Outputter.Write("Enter a customer id to order for them: ");
            int id = Inputter.GetIntegerInput();

            try
            {
                Customer customer = CustomerRepo.GetCustomerByID(id);
                bool     ordering = true;
                Dictionary <Product, int> cart    = new Dictionary <Product, int>();
                Dictionary <Product, int> tempInv = store.Inventory;
                while (ordering)
                {
                    int option = 0;
                    while (!(option > 0 && option < 6))
                    {
                        Outputter.WriteLine("Choose an action\n[1] Add item to cart\n[2] Remove item from cart\n[3] Show cart\n[4] Submit order\n[5] Cancel");
                        option = Inputter.GetIntegerInput();
                    }
                    switch (option)
                    {
                    case 1:
                        PrintInventory(store.ID, cart);
                        Outputter.Write("Enter an item to add to cart: ");
                        int item = Inputter.GetIntegerInput();
                        Outputter.Write("Enter how many you would like: ");
                        int quantity = Inputter.GetIntegerInput();
                        try
                        {
                            Product p = GetProductFromStoreByID(store, item);
                            if (cart.ContainsKey(p) && tempInv[p] >= quantity)
                            {
                                cart[p]    += quantity;
                                tempInv[p] -= quantity;
                            }
                            else if (!cart.ContainsKey(p) && tempInv[p] >= quantity)
                            {
                                cart[p]     = quantity;
                                tempInv[p] -= quantity;
                            }
                            else
                            {
                                throw new ArgumentException("Trying to add too many!");
                            }
                            Outputter.WriteLine("Item added successfully!");
                        }
                        catch (Exception)
                        {
                            Outputter.WriteLine("Couldn't find product to add to cart or too much quantity trying to be added.");
                        }
                        break;

                    case 2:
                        PrintCart(cart);
                        Outputter.Write("Enter an item to remove from cart: ");
                        int item2 = Inputter.GetIntegerInput();
                        Outputter.Write("Enter how many you would like to remove: ");
                        int quantity2 = Inputter.GetIntegerInput();
                        try
                        {
                            Product p = GetProductFromStoreByID(store, item2);
                            foreach (var cartItem in cart)
                            {
                                if (cartItem.Key.ID == p.ID)
                                {
                                    if (cartItem.Value <= quantity2)
                                    {
                                        cart.Remove(cartItem.Key);
                                        tempInv[cartItem.Key] += cartItem.Value;
                                        Outputter.WriteLine("Item removed successfully!");
                                    }
                                    else
                                    {
                                        cart[cartItem.Key]    -= quantity2;
                                        tempInv[cartItem.Key] += quantity2;
                                        Outputter.WriteLine($"Removed {quantity2} {p.Name}'s from cart successfully!");
                                    }
                                }
                            }
                        }
                        catch (Exception)
                        {
                            Outputter.WriteLine("Couldn't find product to remove from cart.");
                        }
                        break;

                    case 3:
                        PrintCart(cart);
                        break;

                    case 4:
                        try
                        {
                            Order order = new Order(customer, store);
                            order.Items = cart;
                            order.CalculateOrderTotal();
                            order.SubmitOrder();
                            StoreRepo.UpdateStore(order.Store);
                            StoreRepo.ProcessInventoryForOrder(order.Store, cart);
                            ordering = false;
                            OrderRepo.AddOrder(order);
                            order = OrderRepo.GetMostRecentOrder();
                            foreach (var orderItem in cart)
                            {
                                OrderRepo.AddOrderItem(orderItem.Key, order, orderItem.Value);
                            }
                            Outputter.WriteLine("Order placed successfully!");
                        }
                        catch (Exception)
                        {
                            Outputter.WriteLine("Couldn't process order");
                        }
                        break;

                    case 5:
                        ordering = false;
                        Outputter.WriteLine("Order cancelled.");
                        break;
                    }
                }
            }
            catch (Exception)
            {
                Outputter.WriteLine("Couldn't find customer with that ID.");
            }
        }