JSFiddle - React, Tailwind, and code Playground
JavaScript
function Network() {
this._elements = [];
}
Network.DAY = "day";
Network.NIGHT = "night";
Network.prototype.addElements = function() {
for (var i = 0; i < arguments.length; i++) {
this._elements.push(arguments[i]);
}
};
Network.prototype.getPowerProduction = function(timeOfTheDay) {
var powerProduction = 0;
for (var i = 0; i < this._elements.length; i++) {
if (this._elements[i] instanceof PowerStation) {
powerProduction += this._elements[i].getPowerProduction(timeOfTheDay);
}
}
return powerProduction;
};
Network.prototype.getPowerConsumption = function(timeOfTheDay) {
var powerConsumption = 0;
for (var i = 0; i < this._elements.length; i++) {
if (this._elements[i] instanceof LivingHouse) {
powerConsumption += this._elements[i].getPowerConsumption(timeOfTheDay);
}
}
return powerConsumption;
};
Network.prototype.getPowerExcess = function(timeOfTheDay) {
return this.getPowerProduction(timeOfTheDay) - this.getPowerConsumption(timeOfTheDay);
};
Network.prototype.getProfit = function(timeOfTheDay) {
var powerExcess = this.getPowerExcess(timeOfTheDay);
var profit = 0;
var result = 1;
if (powerExcess < 0) {
result = -1;
powerExcess = Math.abs(powerExcess);
}
for (var i = 0; i < this._elements.length; i++) {
if (powerExcess === 0) {
break;
}
if (this._elements[i] instanceof PowerLine) {
profit += this._elements[i].countDailyProfit(powerExcess);
powerExcess -= this._elements[i].getPowerAccumulated();
}
}
return profit*result;
};
function PowerStation(powerProduction) {
if (powerProduction > PowerStation.MAXPOWERPRODUCTION || powerProduction <...