0

I am new in Java and I am going to write a Java program to model some kind of Bill of Materials. A Bill of Material will contain a name, and a list of another bill of materials. This is my BOM model:

public class BOM {
    private String name;
    private List<BOM> childBOMs;

    public BOM(String name, List<BOM> childBOMs) {
        this.name = name;
        childBOMs = childBOMs;
    }

    // getter setter
}

In the program, I have a map to hold all the bill of materials that I have created:

    private Map<String, BOM> bomMap;
    private List<BOM> boms;

    private void createBOM(String bomName, List<BOM> childBOMs) {
        if (!bomMap.containsKey(bomName)) {
            bomMap.put(bomName, new BOM(bomName, childBOMs));
        } else {
            System.out.println("A BOM with the same name is already put in the system");
        }
    }

Now let's say I create two BOMs:

- BOM1
   + CBOM1
   + CBOM2

- CBOM1
   + CCBOM1

Now in the map I should have two BOMs: BOM1 and CBOM1. Now is my question how can I link the CBOM1 object that I just created to the CBOM1 that in the BOM1 object, so that the BOM1 and my bomMap will become something like:

- BOM1
  + CBOM1
    + CCBOM1
  + CBOM2

- CBOM1
   + CCBOM1

Of course I can do directly like bom1.getChildBOMs().get(0).setChildBOMs(cBOM1.getChildBOMs()) but I want to ask if there is any generic solution here do to this, because I can image that if I have a hundread of BOMs in the system and each of them have some child BOMs and each of child BOMs have some child-child-BOMs, it would be very difficult to handle all. Can someone give me some hint here? Thanks!

Joe
  • 21
  • 2
  • Have you considered using a n-ary Tree? You can look here for inspiration: https://stackoverflow.com/questions/31779028/how-to-represent-a-non-binary-tree-and-how-to-do-lca-on-that-tree – Sofo Gial Mar 06 '19 at 14:56
  • 1
    thank you I will have a look on it – Joe Mar 06 '19 at 15:21

0 Answers0