Home > other >  Calculate the total of the items I can make of a product within a product
Calculate the total of the items I can make of a product within a product

Time:04-30

I have a problem I am having trouble solving.

I have a product class which contains sub products:

public class Product
    {
        public string ProductName { get; set; }
        public List<SubProduct> SubProducts { get; set; } = new List<SubProduct>();
    }

    public class SubProduct
    {
        public string ProductName { get; set; }
        public decimal RequiredQuantity { get; set; }
        public decimal StockLevel { get; set; }
    }

Used like this:

static void Main(string[] args)
        {
            Product product = new Product
            {
                ProductName = "Toy Car",
                SubProducts = new List<SubProduct>
                {
                    new SubProduct
                    {
                        ProductName="Wheel",
                        StockLevel=20,
                        RequiredQuantity=4
                    },
                    new SubProduct
                    {
                        ProductName="Engine",
                        StockLevel=5,
                        RequiredQuantity=1
                    },
                    new SubProduct
                    {
                        ProductName="Red Paint",
                        StockLevel=30,
                        RequiredQuantity =1
                    }
                }
            };
        }

I would like to calculate how many toy cars I can make with the stock levels and required quantities of the individual sub products.

I have tried everything I can think of!

Thank you

CodePudding user response:

Divide StockLevel by RequiredQuantity for each item and the lowest number is how many you can manufacture.

CodePudding user response:

Create a function in SubProduct that divides StockLevel / RequiredQuantity and returns the result as int.

Then create a function in Product that calls this function of each SubProduct it has and returns the lowest value of them all.

  • Related