vendredi 12 novembre 2021

Recursion with interdependency of the lower levels

I have a program which has a number of products. They can be either simple or composed of other products, with the ones composed having a recipe with the type and number of each component. I need to check if I can get enough of a given product. If it is simple, I can just see if I have enough units, but if it is composed, I need to see if I can build enough copies from the rest of the products. The problem is that I can't just try to apply a Composite pattern, because the components can be composed and have products in common. So, to illustrate, imagine that I want to see if i have enough materials to build 4 cars. The problem would be like this:

Car:
   4 tires
   1 wheel
   1 chassis

Tire:
   2 rubber

Wheel:
   4 rubber

Chassis:
   10 metal

**Available**
   2 Cars
   3 Tires
   0 Wheels
   40 Metal
   12 Rubber

With this I can only get three cars (2+1), however, if I checked componentwise, I had enough of each material to build each one of the components for the four cars. What would be the structure of the functions that I need to implement?

Aucun commentaire:

Enregistrer un commentaire