Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nikola-Andreev/3752d7c6a1be7bb94dcc80b2543a3c1b to your computer and use it in GitHub Desktop.
Save Nikola-Andreev/3752d7c6a1be7bb94dcc80b2543a3c1b to your computer and use it in GitHub Desktop.
03. Application Logic
package SystemSplit;
import SystemSplit.Hardware.HardwareComponent;
import SystemSplit.Hardware.HeavyHardwareComponent;
import SystemSplit.Hardware.PowerHardwareComponent;
import SystemSplit.Software.ExpressSoftwareComponent;
import SystemSplit.Software.LightSoftwareComponent;
import SystemSplit.Software.SoftwareComponent;
import java.util.ArrayList;
import java.util.LinkedHashMap;
public class TheSystem {
private LinkedHashMap<String, HardwareComponent> hardwareComponents;
public TheSystem() {
this.hardwareComponents = new LinkedHashMap<>();
}
public void registerPowerHardware(String name, int capacity, int memory) {
HardwareComponent component = new PowerHardwareComponent(name, capacity, memory);
this.hardwareComponents.put(name, component);
}
public void registerHeavyHardware(String name, int capacity, int memory) {
HardwareComponent component = new HeavyHardwareComponent(name, capacity, memory);
this.hardwareComponents.put(name, component);
}
public void registerExpressSoftware(String hardwareComponentName, String name, int capacity, int memory) {
SoftwareComponent component = new ExpressSoftwareComponent(name, capacity, memory);
this.hardwareComponents.get(hardwareComponentName).registerSoftwareComponent(component);
}
public void registerLightSoftware(String hardwareComponentName, String name, int capacity, int memory) {
SoftwareComponent component = new LightSoftwareComponent(name, capacity, memory);
this.hardwareComponents.get(hardwareComponentName).registerSoftwareComponent(component);
}
public void releaseSoftwareComponent(String hardwareComponentName, String softwareComponentName) {
this.hardwareComponents.get(hardwareComponentName).releaseSoftwareComponent(softwareComponentName);
}
public String analyze() {
StringBuilder builder = new StringBuilder();
builder.append("System Analysis\n")
.append(String.format("Hardware Components: %d\n", this.hardwareComponents.size()))
.append(String.format("Software Components: %d\n", this.getSoftwareComponents()))
.append(String.format("Total Operational Memory: %d / %d\n", this.getMemoryInUse(), this.getMaxMemory()))
.append(String.format("Total Capacity Taken: %d / %d", this.getCapacityInUse(), this.getMaxCapacity()));
return builder.toString();
}
private int getSoftwareComponents() {
int sum = 0;
for (HardwareComponent hardwareComponent : hardwareComponents.values()) {
sum += hardwareComponent.getSoftware().size();
}
return sum;
}
private int getMemoryInUse() {
int sum = 0;
for (HardwareComponent hardwareComponent : hardwareComponents.values()) {
sum += hardwareComponent.getUsedMemory();
}
return sum;
}
private int getMaxMemory() {
int sum = 0;
for (HardwareComponent hardwareComponent : hardwareComponents.values()) {
sum += hardwareComponent.getMemory();
}
return sum;
}
private int getCapacityInUse() {
int sum = 0;
for (HardwareComponent hardwareComponent : hardwareComponents.values()) {
sum += hardwareComponent.getUsedCapacity();
}
return sum;
}
private int getMaxCapacity() {
int sum = 0;
for (HardwareComponent hardwareComponent : hardwareComponents.values()) {
sum += hardwareComponent.getCapacity();
}
return sum;
}
public String split() {
StringBuilder result = new StringBuilder();
this.hardwareComponents.entrySet().forEach(hard -> {
result.append("Hardware Component - ").append(hard.getKey()).append(System.lineSeparator());
result.append("Express Software Components - ").append(hard.getValue().getSoftware()
.entrySet().stream().filter(c -> c.getValue().getType().equals("Express")).count()).append(System.lineSeparator());
result.append("Light Software Components - ").append(hard.getValue().getSoftware()
.entrySet().stream().filter(c -> c.getValue().getType().equals("Light")).count()).append(System.lineSeparator());
result.append("Memory Usage: ").append(hard.getValue().getUsedMemory()).append(" / ")
.append(hard.getValue().getMemory()).append(System.lineSeparator());
result.append("Capacity Usage: ").append(hard.getValue().getUsedCapacity()).append(" / ")
.append(hard.getValue().getCapacity()).append(System.lineSeparator());
result.append("Type: ").append(hard.getValue().getType()).append(System.lineSeparator());
ArrayList<String> list = new ArrayList<String>(hard.getValue().getSoftware().keySet());
result.append("Software Components: ")
.append(list.isEmpty() ? "None" : list.toString().replace("]", "").replace("[", "")).append(System.lineSeparator());
});
return result.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment