Exemple #1
0
        /// <summary>
        /// In this screen the customer can order a product.
        /// </summary>
        public void CreateNewOrder()
        {
            Console.Clear();
            Console.WriteLine("\n What would you like to order? \n *********");
            PrintAllProducts();
            Console.Write(" *********\n Please insert the ID of the selected product: ");

            Order order = new Order();

            order.CustomerID = currentCustomer.ID;
            order.ProductID  = input.GetUserInput <int>();

            Console.Write(" *********\n Please insert the amount you would like to order: ");
            order.Amount = input.GetUserInput <int>();

            Product product = storeDAO.GetProductByID(order.ProductID);

            if (product.ID == 0)
            {
                HandleError(new ProductDoesntExistException("Product doesn't exist"));
                return;
            }

            if (order.Amount <= 0)
            {
                HandleError(new OrderAmountCannotBeZeroOrNegativeNumberException("Order amount cannot be zero or negative number"));
                return;
            }

            if (product.Quantity == 0)
            {
                HandleError(new ProductOutOfStockException("Product is out of stock"));
                return;
            }

            if (product.Quantity < order.Amount)
            {
                HandleError(new NotEnoughItemsInTheStockException("Not enough items in the stock to buy"));
                return;
            }

            order.TotalPrice = order.Amount * product.Price;

            storeDAO.UpdateProductQuantity(product.ID, product.Quantity - order.Amount);
            storeDAO.CreateNewOrder(order);

            if (mode == TestMode.On)
            {
                return;
            }

            Console.WriteLine("\n Your order was placed successfully! press Enter to continue.");

            Console.ReadKey();

            storeDAO.AddLogRecord(
                new LogRecord(DateTime.Now, $"The user {currentCustomer.UserName} has purchased {order.Amount} units of a {product.Name}", "Yes", ""));

            RegisteredCustomerMenuForm();
        }