JSFiddle - React, Tailwind, and code Playground
JavaScript
function IPowerElement(type) {
this.type = type;
}
IPowerElement.prototype.calc_balance = function () {
return 0
};
IPowerElement.Consumenr = 'consumer';
IPowerElement.Producent = 'producent';
function PowerPlant(power) {
IPowerElement.apply(this, [IPowerElement.Producent]);
this.power = power;
}
PowerPlant.prototype = Object.create(IPowerElement.prototype);
PowerPlant.prototype.calc_balance = function () {
return this.power;
};
function SolarPanel(power) {
IPowerElement.apply(this, [IPowerElement.Producent]);
this.power = power;
}
SolarPanel.prototype = Object.create(IPowerElement.prototype);
SolarPanel.prototype.calc_balance = function (is_day) {
return (is_day) ? this.power : 0;
};
function House(population) {
IPowerElement.apply(this, [IPowerElement.Consumenr]);
this.population = population;
}
House.prototype = Object.create(IPowerElement.prototype);
House.prototype.calc_balance = function (is_day) {
return (is_day) ? this.population * 4 : this.population;
};
function Conductor() {
}
Conductor.prototype.buyEnergy = function (quantity) {
return quantity
};
Conductor.prototype.saleEnergy = function (quantity) {
return quantity * (-1)
};
function City(energy_schema, conductor) {
this.day = false;
this.total_balace = 0;
this.power_element = energy_schema;
this.conductor = conductor;
}
City.prototype.set_day = function () {
this.day = true
};
City.prototype.set_night = function () {
this.day = false
};
City.prototype.calc_total_balance = function () {
var temp = this.total_balace;
var temp_day = this.day;
this.power_element.forEach(function (element) {
switch (element.type) {
case IPowerElement.Consumenr:
temp -= element.calc_balance(temp_day);
break;
case IPowerElement.Producent:
temp += element.calc_balance(temp_day);
break;
}
});
this.total_balace...