Пример #1
0
        /// <summary>
        /// Handles displaying the order history of a given location ID.
        /// </summary>
        private static void HandleRequestDisplayOrderHistoryOfLocation()
        {
            Log.Information("Handling request displaying order history of location");
            Console.WriteLine("[?] What is the location ID");
            string inputLocationId = Console.ReadLine();

            Log.Information($"User entered '{inputLocationId}' for location id");
            if (!Int32.TryParse(inputLocationId, out int locationId))
            {
                throw new FormatException("[!] Input for location ID is not an integer");
            }

            if (LocationData.GetLocationWithId(locationId) is null)
            {
                throw new BusinessLocationException($"[!] Location {locationId} does not exist");
            }

            ICollection <BusinessOrder> ordersWithLocation = OrderData.GetOrdersWithLocationId(locationId);

            Console.WriteLine($"[*] There are {ordersWithLocation.Count} orders for location {locationId}");
            ordersWithLocation.ToList().ForEach(o => Console.WriteLine(o));
            Console.WriteLine();
        }
Пример #2
0
        /// <summary>
        /// Handles the prompting, input validation, and console output for a customer creating an order.
        /// </summary>
        private static void HandleRequestPlaceOrder()
        {
            Log.Information("Handling request placing order");
            int line = 0;
            Dictionary <int, int>             lineItemsParsed = new Dictionary <int, int>();
            Dictionary <BusinessProduct, int> lineItems       = new Dictionary <BusinessProduct, int>();

            Console.WriteLine("[?] What is the customer ID");
            string inputCustomerId = Console.ReadLine();

            Log.Information($"User entered '{inputCustomerId}' for customer id");
            if (!Int32.TryParse(inputCustomerId, out int customerId))
            {
                throw new FormatException($"[!] Input for customer ID is not an integer");
            }

            Console.WriteLine("[?] What is the location ID");
            string inputLocationId = Console.ReadLine();

            Log.Information($"User entered '{inputLocationId}' for location id");
            if (!Int32.TryParse(inputLocationId, out int locationId))
            {
                throw new FormatException($"[!] Input for location ID is not an integer");
            }

            while (++line > 0)
            {
                Console.WriteLine($"[?] Line {line} - Enter product ID or press enter to finish your order");
                string inputProductId = Console.ReadLine();
                Log.Information($"User entered '{inputProductId}' for product id on line {line}");
                if (inputProductId == "")
                {
                    Log.Information($"User entered nothing for product id on line {line}");
                    break;
                }
                if (!Int32.TryParse(inputProductId, out int productId))
                {
                    throw new FormatException($"[!] Input for product ID is not an integer");
                }

                Console.WriteLine($"[?] Line {line} - Enter quantity");
                string inputQuantity = Console.ReadLine();
                Log.Information($"User entered '{inputQuantity}' for quantity on line {line}");
                if (!Int32.TryParse(inputQuantity, out int quantity))
                {
                    throw new FormatException($"[!] Input for quantity is not an integer");
                }

                lineItemsParsed.Add(productId, quantity);
            }

            if (CustomerData.GetCustomerWithId(customerId) is null)
            {
                throw new BusinessCustomerException($"[!] Customer does not exist");
            }

            if (LocationData.GetLocationWithId(locationId) is null)
            {
                throw new BusinessLocationException($"[!] Location does not exist");
            }

            foreach (KeyValuePair <int, int> lineItemParsed in lineItemsParsed)
            {
                BusinessProduct bProduct = ProductData.GetProductWithId(lineItemParsed.Key);
                if (bProduct is null)
                {
                    throw new BusinessProductException($"[!] Product {lineItemParsed.Key} does not exist");
                }
                lineItems.Add(bProduct, lineItemParsed.Value);
            }

            BusinessCustomer bCustomer = CustomerData.GetCustomerWithId(customerId);
            BusinessLocation bLocation = LocationData.GetLocationWithId(locationId);

            BusinessOrder bOrder = new BusinessOrder()
            {
                StoreLocation = bLocation,
                Customer      = bCustomer,
                OrderTime     = DateTime.Now
            };

            bOrder.AddLineItems(lineItems);
            foreach (KeyValuePair <BusinessProduct, int> lineItem in lineItems)
            {
                bOrder.StoreLocation.DecrementStock(lineItem.Key, lineItem.Value);
            }
            OrderData.CreateOrder(bOrder);
        }