package com.sourcefellows.patterns.composite;

import java.util.ArrayList;
import java.util.List;

public class CompoundComponent implements Component{

    private List<Component> components;

    public CompoundComponent() {
        components = new ArrayList<>();
    }

    public void addComponent(Component component) {
        components.add(component);
    }

    @Override
    public Weight getWeight() {
        var weight = Weight.createWeight(0, Weight.Unit.MILLIGRAMS);
        for (var component : components) {
            weight = Weight.sum(weight, component.getWeight());
        }
        return weight;
    }
}
