JSFiddle - React, Tailwind, and code Playground
JavaScript
if (!Object.create) {
Object.create = function(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]);
}
}
};
Network.prototype.getPowerProduction = function(timeOfTheDay) {
var powerProduction = 0;
for (var i = 0; i < this._elements.length; i++) {
if (this._elements[i] instanceof Generator) {
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 Consumer) {
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() - prevElem.getPowerCost();
});
} else {
lines.sort(function(prevElem, currentElem) {
return...