Last active
July 7, 2022 00:23
-
-
Save wolivera/6c1806c06779165b9d63f09222d71aa8 to your computer and use it in GitHub Desktop.
GoF Chain of Resp
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
class ATM { | |
constructor(amount) { | |
this.amount = amount; | |
console.log("Requested: $" + amount + "\n"); | |
} | |
get (bill) { | |
const count = Math.floor(this.amount / bill); | |
this.amount -= count * bill; | |
console.log("Dispense " + count + " $" + bill + " bills"); | |
return this; | |
} | |
} | |
const atm = new ATM(442); | |
atm.get(100).get(50).get(20).get(10).get(5).get(1); | |
// Requested: $442 | |
// Dispense 4 $100 bills | |
// Dispense 0 $50 bills | |
// Dispense 2 $20 bills | |
// Dispense 0 $10 bills | |
// Dispense 0 $5 bills | |
// Dispense 2 $1 bills |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment