Skip to content

Instantly share code, notes, and snippets.

@pxbuffer
Last active October 1, 2018 16:15
Show Gist options
  • Save pxbuffer/d9b207e2ac11cde35a858d29b7bda310 to your computer and use it in GitHub Desktop.
Save pxbuffer/d9b207e2ac11cde35a858d29b7bda310 to your computer and use it in GitHub Desktop.
function CoffeeMachine(power, capacity) {
Machine.apply(this, arguments);
let waterAmount;
const WATER_HEAT_CAPACITY = 4200;
let timerId;
console.log('Create coffee machine with capacity of ' + power + ' vt');
this.setWaterAmount = function (amount) {
if (amount < 0) throw new Error('The number must be positive');
if (amount > capacity) throw new Error('Do not add more water then ' + capacity);
waterAmount = amount;
}
let getBoilTime = function () {
return this.waterAmount * WATER_HEAT_CAPACITY * 80 / power;
}.bind(this);
function onReady() {
console.log('Coffee is ready');
}
this.setOnReady = function (newReady) {
onReady = newReady;
}
this.getPower = function () {
return console.log('Power of coffee machine -' + power)
}
this.getWaterAmount = function (amount) {
return waterAmount;
};
this.run = function () {
timerId = setTimeout(function () {
onReady();
}, getBoilTime())
}
this.stop = function () {
clearTimeout(timerId);
}
this.addWater = function (water) {
this.setWaterAmount(waterAmount + water);
}
let parentEnable = this.enable;
this.enable = function () {
parentEnable();
console.log('coffee machine enabled')
}
}
function Machine(power) {
this._power = power
this._enabled = false;
let self = this;
this.enable = function () {
this._enabled = true;
console.log(true);
}
this.disable = function () {
this._enabled = false;
}
}
let machine = new Machine(120000);
let coffeeMachine = new CoffeeMachine(100000, 400);
coffeeMachine.setWaterAmount(300);
coffeeMachine.run();
coffeeMachine.setOnReady(function () {
var amount = coffeeMachine.getWaterAmount();
console.log('Готов кофе: ' + amount + 'мл');
});
coffeeMachine.enable();
machine.enable();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment