Created
May 29, 2018 22:23
-
-
Save marekdano/1a557017f4dc9f7e34c9cb9810d4635c to your computer and use it in GitHub Desktop.
js-the-best-practice (nodeschool)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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