Skip to content

Instantly share code, notes, and snippets.

@wolivera
Last active July 7, 2022 00:23
Show Gist options
  • Save wolivera/6c1806c06779165b9d63f09222d71aa8 to your computer and use it in GitHub Desktop.
Save wolivera/6c1806c06779165b9d63f09222d71aa8 to your computer and use it in GitHub Desktop.
GoF Chain of Resp
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