/// <summary>
        /// Specify a the amount to increase the specified Location's inventory by.
        /// </summary>
        /// <param name="locationName">The location's name that will be used to
        /// retrieve the data from the DB</param>
        /// <param name="productName">The product's name that will be used to
        /// retrieve the data from the DB</param>
        /// <param name="addedAmount">Amount of products to increment to the
        /// Locations inventory</param>
        /// <returns>
        /// A Message object that will state whether or not the restock was
        /// processed succsesfully.
        /// </returns>
        public Packet RestockInventory(string locationName, string productName, int addedAmount)
        {
            // Check for scenarios where issues may occur
            if (addedAmount <= 0)
            {
                return(new Packet
                {
                    Text = "Please enter a postive number to increase the inventory by.",
                    Status = PacketStatus.Invalid
                });
            }
            else if (locationName.Length == 0)
            {
                return(new Packet
                {
                    Text = "Please enter a valid location name.",
                    Status = PacketStatus.Invalid
                });
            }
            else if (productName.Length == 0)
            {
                return(new Packet
                {
                    Text = "Please enter a valid product name.",
                    Status = PacketStatus.Invalid
                });
            }

            const int MAX_INCREMENT = 100;

            Location location = GetLocationByName(locationName);

            ProductLogic productLogic = new ProductLogic();
            Product      product      = productLogic.FindProductByName(productName);

            ProductInventoryLogic pIL = new ProductInventoryLogic();
            ProductInventory      productInventory;

            if (addedAmount > MAX_INCREMENT)
            {
                return(new Packet
                {
                    Text = $"The max amount of products that can be given to a store at a time is {MAX_INCREMENT}.",
                    Status = PacketStatus.Invalid
                });
            }
            else if (location == null)
            {
                return(new Packet
                {
                    Text = "The Location you've entered does not exist in our system.",
                    Status = PacketStatus.Invalid
                });
            }
            else if (product == null)
            {
                return(new Packet
                {
                    Text = "The Product you've entered does not exist in our system",
                    Status = PacketStatus.Invalid
                });
            }

            // All validation has passed, we're now set to restock
            productInventory = pIL.GetProductInventory(location, product);
            int newAmount = productInventory.Quanitity + addedAmount;

            pIL.UpdateInventory(location, product, newAmount);

            return(new Packet
            {
                Text = $"Inventory Restocked.  {location.Name}'s quantity of {product.Name} is now {newAmount}",
                Status = PacketStatus.Pass
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Registers a customer making a purchase
        /// </summary>
        /// <param name="customerName">
        ///  The name of the Customer making the purchase
        /// </param>
        /// <param name="locationName">
        ///  The name of the location the customer is purchasing from.
        /// </param>
        /// <param name="productName">
        /// The product being purchased
        /// </param>
        /// <param name="quantity">
        ///  The amount of the product being purchased
        /// </param>
        /// <returns>A Packet containing info of the purchase</returns>
        public Packet MakePurchase(string username, string locationName, string productName, int quantity)
        {
            CustomerLogic customerLogic = new CustomerLogic();
            Customer      customer;

            LocationLogic locationLogic = new LocationLogic();
            Location      location;

            ProductInventoryLogic pIL = new ProductInventoryLogic();
            ProductInventory      productInventory;

            ProductLogic productLogic = new ProductLogic();
            Product      product;

            OrderLogic orderLogic = new OrderLogic();

            // check if customer Exists
            customer = customerLogic.FindCustomerByUsername(username);
            if (customer == null)
            {
                return(new Packet
                {
                    Text = "That customer does not exist.",
                    Status = PacketStatus.Invalid,
                });
            }

            // check location exists
            location = locationLogic.GetLocationByName(locationName);
            if (location == null)
            {
                return(new Packet
                {
                    Text = "The location specified was not found.",
                    Status = PacketStatus.Invalid,
                });
            }
            // check product exists
            product = productLogic.FindProductByName(productName);
            if (product == null)
            {
                return(new Packet
                {
                    Text = "The product specified was not found.",
                    Status = PacketStatus.Invalid,
                });
            }

            // Check location has product in stock
            productInventory = pIL.GetProductInventory(location, product);
            if (productInventory == null)
            {
                return(new Packet
                {
                    Text = "This location does not currently carry this product",
                    Status = PacketStatus.Invalid
                });
            }

            // Check to see if customer isn't buying too many products
            if (quantity > productInventory.Quanitity)
            {
                return(new Packet
                {
                    Text = $"This location does not carry the amount of {product.Name} you are trying to purchase",
                    Status = PacketStatus.Invalid
                });
            }

            // All's good, being registering purchase

            // Decrease quantity of location by the amount the customer is purchasing
            int quantitySold = productInventory.Quanitity - quantity;

            pIL.UpdateInventory(location, product, quantitySold);

            // Register Order for customer
            orderLogic.RegisterOrder(customer, locationName, productName, quantity);

            return(new Packet
            {
                Text = $"Order purchased successfully - Purchaser {username} ordered product {productName} from location {locationName}",
                Status = PacketStatus.Pass
            });
        }