Created
March 3, 2017 22:02
-
-
Save gustavo-rodrigues-dev/0b5e0147e51259971e8be47f13c94d37 to your computer and use it in GitHub Desktop.
Chain Of Responsibility js
This file contains 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 CashMachine = function(amount) { | |
this.amount = amount; | |
log.add("Requested: $" + amount + "\n"); | |
} | |
CashMachine.prototype = { | |
get: function(bill) { | |
var count = Math.floor(this.amount.toFixed(2) / bill); | |
this.amount -= count * bill; | |
log.add("Dispense " + count + " $" + bill + " bills"); | |
return this; | |
} | |
} | |
var log = (function() { | |
var log = ""; | |
return { | |
add: function(msg) { log += msg + "\n"; }, | |
show: function() { console.log(log); log = ""; } | |
} | |
})(); | |
function requestMoney(money) { | |
var request = new CashMachine(money); | |
request | |
.get(100) | |
.get(50) | |
.get(20) | |
.get(10) | |
.get(5) | |
.get(1) | |
.get(0.5) | |
.get(0.25) | |
.get(0.1) | |
.get(0.01); | |
log.show(); | |
} | |
requestMoney(786.89); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment