simplified carTemplate

This commit is contained in:
2021-03-29 15:49:24 +02:00
parent 0d46473234
commit 97f2e5d0f1
4 changed files with 16 additions and 18 deletions

View File

@ -1,35 +1,31 @@
package de.mrgeorgen.v2g; package de.mrgeorgen.v2g;
public class car { public class car {
private int battery;
private final int fullBattery;
private int chargeLock;
private final int chargeSpeed;
private final String model;
private final int id; private final int id;
private final carTemplate carModel;
private int battery;
private int chargeLock;
public car(String model, int fullBattery, int chargeSpeed, int id) { // id can be the same for different models public car(carTemplate carModel, int id) { // id can be the same for different models
this.fullBattery = 1000 * fullBattery; // convert kWh to Wh this.carModel = carModel;
this.chargeSpeed = chargeSpeed * 1000; // convert kW to W
this.setChargeLock(0.6); this.setChargeLock(0.6);
this.id = id; this.id = id;
this.model = model; this.battery = this.carModel.fullBattery / 2;
this.battery = this.fullBattery / 2;
} }
public void charge() { public void charge() {
int chargeAmmount = Math.abs(powerGrid.energieAvailable) < this.chargeSpeed ? powerGrid.energieAvailable : powerGrid.energieAvailable > 0 ? this.chargeSpeed : -1 * this.chargeSpeed; int chargeAmmount = Math.abs(powerGrid.energieAvailable) < this.carModel.chargeSpeed ? powerGrid.energieAvailable : powerGrid.energieAvailable > 0 ? this.carModel.chargeSpeed : -1 * this.carModel.chargeSpeed;
if(this.battery + chargeAmmount <= this.chargeLock && chargeAmmount < 0) chargeAmmount = (this.battery - this.chargeLock) * -1; // do not charge if the car gets under the charge lock if(this.battery + chargeAmmount <= this.chargeLock && chargeAmmount < 0) chargeAmmount = (this.battery - this.chargeLock) * -1; // do not charge if the car gets under the charge lock
if(this.battery + chargeAmmount > this.fullBattery) chargeAmmount = this.fullBattery - this.battery; // prevent the battery from overcharging if(this.battery + chargeAmmount > this.carModel.fullBattery) chargeAmmount = this.carModel.fullBattery - this.battery; // prevent the battery from overcharging
if(this.battery + chargeAmmount < 0) chargeAmmount = this.battery * -1; // prevent the battery from dischargingunder 0% if(this.battery + chargeAmmount < 0) chargeAmmount = this.battery * -1; // prevent the battery from dischargingunder 0%
if(chargeAmmount != 0) System.out.println(this.model + " nr. " + this.id + " is " + (chargeAmmount < 0 ? "dis" : "") + "charging with " + (double)Math.abs(chargeAmmount) / 1000 + " kW. battery: " + (double)this.battery / 1000 + "/" + (double)this.fullBattery / 1000 + " kWh (" + Math.round(getBatteryRelativ() * 100) + "%)"); if(chargeAmmount != 0) System.out.println(this.carModel.model + " nr. " + this.id + " is " + (chargeAmmount < 0 ? "dis" : "") + "charging with " + (double)Math.abs(chargeAmmount) / 1000 + " kW. battery: " + (double)this.battery / 1000 + "/" + (double)this.carModel.fullBattery / 1000 + " kWh (" + Math.round(getBatteryRelativ() * 100) + "%)");
this.battery += chargeAmmount; this.battery += chargeAmmount;
powerGrid.energieAvailable -= chargeAmmount; powerGrid.energieAvailable -= chargeAmmount;
if(chargeAmmount < 0) powerGrid.savedEnergie += Math.abs(chargeAmmount); if(chargeAmmount < 0) powerGrid.savedEnergie += Math.abs(chargeAmmount);
} }
private void setChargeLock(double chargeLock) { private void setChargeLock(double chargeLock) {
this.chargeLock = (int)(this.fullBattery * chargeLock); this.chargeLock = (int)(this.carModel.fullBattery * chargeLock);
} }
public double getBatteryRelativ() { public double getBatteryRelativ() {
return (double)this.battery / this.fullBattery; return (double)this.battery / this.carModel.fullBattery;
} }
} }

View File

@ -19,7 +19,7 @@ public class carGrid {
for(carTemplate carModel : models) { for(carTemplate carModel : models) {
final int numberOfCars = random.nextInt(10); final int numberOfCars = random.nextInt(10);
for(int i = 0; i < numberOfCars; ++i) { for(int i = 0; i < numberOfCars; ++i) {
dockedCars.add(new car(carModel.model, carModel.fullBattery, carModel.chargeSpeed, i)); dockedCars.add(new car(carModel, i));
} }
} }
} }

View File

@ -7,9 +7,9 @@ public class carTemplate {
public final int range; public final int range;
public carTemplate(String model, int fullBattery, int chargeSpeed, int range) { public carTemplate(String model, int fullBattery, int chargeSpeed, int range) {
this.fullBattery = 1000 * fullBattery; // convert kWh to Wh
this.chargeSpeed = chargeSpeed * 1000; // convert kW to W
this.model = model; this.model = model;
this.chargeSpeed = chargeSpeed;
this.fullBattery = fullBattery;
this.range = range; this.range = range;
} }
} }

View File

@ -10,6 +10,8 @@ public class powerGrid {
} }
final carGrid carGrid = new carGrid(); final carGrid carGrid = new carGrid();
carGrid.fillWithCars(); carGrid.fillWithCars();
car[] allCars = new car[carGrid.dockedCars.size()];
allCars = carGrid.dockedCars.toArray(allCars);
final int hourSimulationRuns = Integer.parseInt(args[0]) * 24; final int hourSimulationRuns = Integer.parseInt(args[0]) * 24;
Random random = new Random(); Random random = new Random();
for(int houresPassed = 0; houresPassed < hourSimulationRuns; ++houresPassed) { for(int houresPassed = 0; houresPassed < hourSimulationRuns; ++houresPassed) {