Skip to content

Instantly share code, notes, and snippets.

@marekdano
Created May 29, 2018 22:23
Show Gist options
  • Save marekdano/1a557017f4dc9f7e34c9cb9810d4635c to your computer and use it in GitHub Desktop.
Save marekdano/1a557017f4dc9f7e34c9cb9810d4635c to your computer and use it in GitHub Desktop.
js-the-best-practice (nodeschool)
var vendingMachine = require('./vendingMachine');
vendingMachine.insertCoin('q');
vendingMachine.insertCoin('q');
vendingMachine.insertCoin('q');
vendingMachine.insertCoin('q');
console.log("Insert 100");
console.log("Get product: ", vendingMachine.vendProduct('A1'));
console.log("Release: ", vendingMachine.releaseChange());
// var vendingMachine = require('./vendingMachine');
var balance = 0;
module.exports = {
decreaseBalance: function (amount) {
if (!this.canAfford(amount)) {
throw new Error("Insufficient balance");
}
balance -= amount;
},
getBalance: function () {
return balance;
},
increaseBalance: function (amount) {
balance += amount;
},
canAfford: function (amount) {
if (!this.isValidAmount(amount)) {
throw new Error("Invalid Input");;
}
return amount <= balance;
},
isValidAmount: function (amount) {
if (amount === null) {
return false;
} else {
return true;
}
}
};
var products = [{
name: 'Skittles',
price: 85,
id: 'A1'
}];
module.exports = {
getProducts: function () {
return products;
},
getProduct: function (productId) {
var product = products.find(function (p) {
return p.id === productId;
});
return product;
}
};
var products = [{
name: 'Skittles',
price: 85,
id: 'A1'
}];
module.exports = {
getProducts: function () {
return products;
},
getProduct: function (productId) {
var product = products.find(function (p) {
return p.id === productId;
});
return product;
}
};
var balanceManager = require('./balanceManager');
var changeHandler = require('./changeHandler');
var productInventory = require('./productInventory');
module.exports = {
insertCoin: function (coinType) {
var value = changeHandler.getAmount(coinType);
balanceManager.increaseBalance(value);
},
vendProduct: function (productId) {
var product = productInventory.getProduct(productId);
balanceManager.decreaseBalance(product.price);
return product;
},
releaseChange: function () {
var currentBalance = balanceManager.getBalance();
balanceManager.decreaseBalance(currentBalance);
return changeHandler.convertToChange(currentBalance);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment