JSFiddle - React, Tailwind, and code Playground
JavaScript
function createObj(proto){
if(Object.create){
return Object.create(proto);
}
function F() {}
F.prototype = proto;
var obj = new F;
return obj;
}
function Network() {
this._elements = [];
}
Network.DAY = "day";
Network.NIGHT = "night";
Network.prototype.addElements = function() {
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] instanceof NetworkElement) {
this._elements.push(arguments[i]);
}
else{
throw new Error("Аргумент "+ arguments[i]+" не является элементом сети");
}
}
};
Network.prototype.getPowerProduction = function(timeOfTheDay) {
var powerProduction = 0;
for (var i = 0; i < this._elements.length; i++) {
if (this._elements[i].getPowerProduction) {
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].getPowerConsumption) {
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;
var lines = [];
if (powerExcess < 0) {
result = -1;
powerExcess = Math.abs(powerExcess);
}
for (var i = 0; i < this._elements.length; i++) {
if (this._elements[i] instanceof PowerLine) {
lines.push(this._elements[i]);
}
}
if (result > 0) {
lines.sort(function(prevElem, currentElem) {
return currentElem.getPowerCost() -...