oop-labs-collection/labs/6/Main.java

122 lines
3.8 KiB
Java
Raw Normal View History

import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
Vent vent = new Vent(true);
Appliance[] devices = {
new ServerRack(false),
new ServerRack(false),
new RPI(false),
new Dishwasher(false)
};
devices[0].plug();
devices[2].plug();
int i = 0;
float ventConsumed = 0f;
float totalPowerConsumed = 0f;
//for (int i = 0; i < 180*10; i++) {
while (true) {
step(vent, devices, 0.1f);
ventConsumed += vent.getPowerConsumption() * 0.1;
totalPowerConsumed += getTotalPowerConsumption(vent, devices) * 0.1 / 3600;
//System.out.print(String.format("Time: %02.1f; ", (float) i/10)
System.out.print("Time: " + floatFormat((float)i/10, 2, 1) + "; "
+ getStats(vent, devices) + "; vent avg = "
+ floatFormat(ventConsumed*10/i, 3, 1)
+ "W; total = "
+ floatFormat(totalPowerConsumed, 3, 3) + "W\r");
if (devices[0].getPowerConsumption() > 205) {
vent.plug();
}
if (devices[0].getPowerConsumption() < 190) {
vent.unplug();
}
if (i == 150) {
devices[1].plug();
} else if (i == 300) {
devices[0].unplug();
} else if (i == 400) {
devices[3].plug();
} else if (i == 700) {
devices[0].plug();
}
try {
//TimeUnit.SECONDS.sleep(1);
Thread.sleep(2);
} catch (Exception e) {
System.exit(0);
}
i++;
}
}
public static void step(Vent vent, Appliance[] devices, float seconds) {
vent.step(seconds);
for (Appliance i : devices) {
i.step(seconds, vent.getRPM());
}
}
public static String getStats(Vent vent, Appliance[] devices) {
float[] powerConsumption = new float[devices.length];
float totalPowerConsumption = 0;
for (int i = 0; i < devices.length; i++) {
powerConsumption[i] = devices[i].getPowerConsumption();
totalPowerConsumption += devices[i].getPowerConsumption();
}
String result = "PPD: ";
for (float i : powerConsumption) {
//result += String.format("%03.1fW ", i);
result += String.format(floatFormat(i, 3, 1) + "W ");
}
float powerLinesDraw = totalPowerConsumption
+ vent.getPowerConsumption();
//result += String.format("; Vent: %03.1fW, %05.0f RPM; Total power: %04.1fW",
result += "; Vent: " + floatFormat(vent.getPowerConsumption(), 3, 1)
+ "W, " + floatFormat(vent.getRPM(), 5, 0)
+ " RPM; Total power: " + floatFormat(powerLinesDraw, 4, 1)
+ "W";
//vent.getPowerConsumption(), vent.getRPM(),
//totalPowerConsumption + vent.getPowerConsumption());
return result;
}
public static float getTotalPowerConsumption(Vent vent,
Appliance[] devices) {
float result = vent.getPowerConsumption();
for (Appliance a : devices) {
result += a.getPowerConsumption();
}
return result;
}
public static String floatFormat(float num, int leading, int trailing) {
String newNum = String.format("%0" + leading + "." + trailing + "f", num);
int targetLength = leading + trailing + 1;
for (int i = newNum.length(); i < targetLength; i++) {
newNum = "0" + newNum;
}
return newNum;
}
}