-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChainOfResponsibility.ts
76 lines (59 loc) · 1.77 KB
/
ChainOfResponsibility.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
interface Withdrawing {
withdraw(amount: number): boolean;
}
class MoneyPile implements Withdrawing {
value: number;
quantity: number;
next?: Withdrawing;
constructor(value: number, quantity: number, next?: Withdrawing) {
this.value = value;
this.quantity = quantity;
this.next = next;
}
public withdraw(amount: number): boolean {
let localAmount = amount;
const canTakeSomeBill = (want: number): boolean => ((want / this.value) > 0);
let localQuantity = this.quantity;
while(canTakeSomeBill(localAmount)) {
if (localQuantity === 0) {
break;
}
localAmount -= this.value;
localQuantity -= 1;
}
if (localAmount <= 0) {
return true;
}
if (this.next !== undefined) {
return this.next.withdraw(localAmount);
}
return false;
}
}
class ATM implements Withdrawing {
#hundred: Withdrawing;
#fifty: Withdrawing;
#twenty: Withdrawing;
#ten: Withdrawing;
get startPile(): Withdrawing {
return this.#hundred;
}
constructor(hundred: Withdrawing, fifty: Withdrawing, twenty: Withdrawing, ten: Withdrawing) {
this.#hundred = hundred;
this.#fifty = fifty;
this.#twenty = twenty;
this.#ten = ten;
}
withdraw(amount: number): boolean {
return this.startPile.withdraw(amount);
}
}
// USAGE:
const ten = new MoneyPile(10, 6);
const twenty = new MoneyPile(20, 2, ten);
const fifty = new MoneyPile(50, 2, twenty);
const hundred = new MoneyPile(100, 1, fifty);
// BUILD ATM:
const atm = new ATM(hundred, fifty, twenty, ten);
console.log(atm.withdraw(310)); //false
console.log(atm.withdraw(150)); //true