Remove hardcoded warehouse metadata type

This commit is contained in:
FyloZ 2022-06-07 21:21:50 -04:00
parent 704b40ecc9
commit be21c33755
Signed by: william
GPG Key ID: 835378AE9AF4AE97
4 changed files with 16 additions and 8 deletions

View File

@ -76,7 +76,7 @@ public class XmlConfigurationParser implements ConfigurationParser {
private BuildingMetadata parseBuildingMetadata(Element buildingElement) { private BuildingMetadata parseBuildingMetadata(Element buildingElement) {
String type = buildingElement.getAttribute(ATTRIBUTE_TYPE); String type = buildingElement.getAttribute(ATTRIBUTE_TYPE);
return type.equals("entrepot") ? return type.equals(BuildingMetadata.TYPE_WAREHOUSE) ?
parseWarehouseMetadata(buildingElement, type) : parseWarehouseMetadata(buildingElement, type) :
parseFactoryMetadata(buildingElement, type); parseFactoryMetadata(buildingElement, type);
} }
@ -171,7 +171,7 @@ public class XmlConfigurationParser implements ConfigurationParser {
int x = Integer.parseInt(buildingElement.getAttribute(ATTRIBUTE_X)); int x = Integer.parseInt(buildingElement.getAttribute(ATTRIBUTE_X));
int y = Integer.parseInt(buildingElement.getAttribute(ATTRIBUTE_Y)); int y = Integer.parseInt(buildingElement.getAttribute(ATTRIBUTE_Y));
return type.equals("entrepot") ? return type.equals(BuildingMetadata.TYPE_WAREHOUSE) ?
new Warehouse(id, type, x, y) : new Warehouse(id, type, x, y) :
new Factory(id, type, x, y); new Factory(id, type, x, y);
} }

View File

@ -6,6 +6,8 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
public abstract class BuildingMetadata { public abstract class BuildingMetadata {
public static final String TYPE_WAREHOUSE = "entrepot";
protected final String type; protected final String type;
protected final Map<BuildingState, String> iconsPaths; protected final Map<BuildingState, String> iconsPaths;

View File

@ -1,6 +1,8 @@
package simulation; package simulation;
import configuration.SimulationConfiguration;
import configuration.SimulationConfigurationSingleton; import configuration.SimulationConfigurationSingleton;
import metadata.BuildingMetadata;
import metadata.WarehouseMetadata; import metadata.WarehouseMetadata;
import java.util.ArrayList; import java.util.ArrayList;
@ -9,9 +11,6 @@ import java.util.Collection;
public class Warehouse extends Building implements WarehouseSubject { public class Warehouse extends Building implements WarehouseSubject {
private final Collection<Component> planes = new ArrayList<>(); private final Collection<Component> planes = new ArrayList<>();
private final Collection<WarehouseObserver> observers = new ArrayList<>(); private final Collection<WarehouseObserver> observers = new ArrayList<>();
private final int capacity = ((WarehouseMetadata) SimulationConfigurationSingleton.getInstance()
.getConfiguration().getMetadata().get(getType()))
.getInput().getCapacity();
public Warehouse(int id, String type, int x, int y) { public Warehouse(int id, String type, int x, int y) {
super(id, type, x, y); super(id, type, x, y);
@ -29,12 +28,19 @@ public class Warehouse extends Building implements WarehouseSubject {
observers.add(observer); observers.add(observer);
} }
public void dettach(WarehouseObserver observer) { public void detach(WarehouseObserver observer) {
observers.remove(observer); observers.remove(observer);
} }
public void notifyObservers() { public void notifyObservers() {
boolean isFull = planes.size() >= capacity; boolean isFull = planes.size() >= getCapacity();
observers.forEach(o -> o.update(isFull)); 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();
}
} }

View File

@ -2,6 +2,6 @@ package simulation;
public interface WarehouseSubject { public interface WarehouseSubject {
void attach(WarehouseObserver observer); void attach(WarehouseObserver observer);
void dettach(WarehouseObserver observer); void detach(WarehouseObserver observer);
void notifyObservers(); void notifyObservers();
} }