示例#1
0
        public static void GetDataAndAddOrder(IProject0Repo p0Repo)
        {
            NLog.ILogger logger = LogManager.GetCurrentClassLogger();

            // Get all customers to validate that there is at least one customer.
            // There must be at least one customer to place an order.
            List <Library.Customer> customers = p0Repo.GetAllCustomers().ToList();

            if (customers.Count == 0)
            {
                logger.Error("You have to add at least one customer before you can add an order.");
                return;
            }
            // Get a customer
            int customerId = ConsoleRead.GetCustomer(p0Repo);

            if (customerId == -1)
            {
                return;
            }
            if (!p0Repo.CheckCustomerExists(customerId))
            {
                logger.Error($"Customer {customerId} is not in the list of customers.");
                return;
            }
            // Get a location
            int locationId = ConsoleRead.GetLocation(p0Repo,
                                                     "Please enter a valid store Id for the order or 'd' for customer default location:", customerId);

            if (locationId == -1)
            {
                return;
            }
            if (!p0Repo.CheckLocationExists(locationId))
            {
                logger.Error($"Location {locationId} is not in the list of stores.");
                return;
            }
            // The following checks to see if the customer can order from this store location
            // If the customer has ordered at this store within the past 2 hours, than they shouldn't be
            // able to order again.
            var orders = p0Repo.GetAllOrders().ToList();

            if (Library.Customer.CheckCustomerCannotOrder(customerId, locationId, orders))
            {
                logger.Error("Customer can't place an order at this store because it hasn't been 2 hours \n" +
                             "since there last order yet.");
                return;
            }
            // Get some cupcakes from the user inputs, both type and quantity
            // These are placed in a dictionary.
            Dictionary <int, int> cupcakeInputs = ConsoleRead.GetCupcakes(p0Repo);

            if (cupcakeInputs.Count == 0)
            {
                return;
            }
            bool cupcakeFound = false;

            foreach (var item in cupcakeInputs)
            {
                if (item.Value > 0)
                {
                    cupcakeFound = true;
                    break;
                }
            }
            // Make sure that the user entered at least one cupcake, qnty pairing
            // No empty orders, no orders with zero quantities.
            if (!cupcakeFound)
            {
                logger.Error("You must enter at least one cupcake to place an order.");
                return;
            }
            // The following gets all orders and their associated order items in order
            // to validate that the store location's supply of the cupcakes in the customer's
            // requested order have not been exhausted.
            // Cupcake exhaustion happens when a store location has had more than 1000 cupcakes of one
            // type sold within 24 hours, at that point they cannot sell anymore.
            // This is arbitrary business logic that I added in order to satisfy the Project0
            // requirements.
            var orderItems = p0Repo.GetAllOrderItems().ToList();

            if (!Library.Location.CheckCanOrderCupcake(locationId, cupcakeInputs, orders, orderItems))
            {
                logger.Error("This store has exhausted supply of that cupcake. Try back in 24 hours.");
                return;
            }
            // The following gets the recipes for the cupcakes in the customer's requested order
            // and checks to make sure that the store location's inventory can support the order.
            var recipes     = p0Repo.GetRecipes(cupcakeInputs);
            var locationInv = p0Repo.GetLocationInv(locationId);

            if (!Library.Location.CheckOrderFeasible(recipes, locationInv, cupcakeInputs))
            {
                logger.Error("This store does not have enough ingredients to place the requested order.");
                return;
            }

            // Add the cupcake order
            p0Repo.AddCupcakeOrder(locationId, customerId);
            // Get the new order Id that was just added in order to report it to the user.
            int newOrderId = p0Repo.GetLastCupcakeOrderAdded();

            // Add the order items to the cupcake order
            p0Repo.AddCupcakeOrderItems(newOrderId, cupcakeInputs);
            // Remove ingredient quantities from the store location's inventory
            p0Repo.UpdateLocationInv(locationId, recipes, cupcakeInputs);
            Console.WriteLine($"Order with Id of {newOrderId} successfully created!");
        }