package simulation; import configuration.SimulationConfiguration; import configuration.SimulationConfigurationSingleton; import metadata.BuildingMetadata; import metadata.WarehouseMetadata; import java.util.ArrayList; import java.util.Collection; import java.util.Optional; public class Warehouse extends Building implements WarehouseSubject { private final WarehouseMetadata metadata; private final Collection observers = new ArrayList<>(); private int planeCount = 0; public Warehouse(int id, String type, int x, int y, WarehouseMetadata metadata) { super(id, type, x, y); this.metadata = metadata; } @Override public Optional update() { return Optional.empty(); } @Override public void processInput(Component input) { planeCount++; } private void sellComponent() { } public void attach(WarehouseObserver observer) { observers.add(observer); } public void detach(WarehouseObserver observer) { observers.remove(observer); } public void notifyObservers() { boolean isFull = planeCount >= getCapacity(); observers.forEach(o -> o.update(isFull)); } private static int getCapacity() { SimulationConfiguration configuration = SimulationConfigurationSingleton.getInstance().getConfiguration(); WarehouseMetadata metadata = (WarehouseMetadata) configuration.getMetadata().get(BuildingMetadata.TYPE_WAREHOUSE); return metadata.getInput().getCapacity(); } @Override public String toString() { boolean isFull = planeCount >= getCapacity(); return String.format("[planes: %d, full: %s]", planeCount, isFull); } }