-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathMoneyChainHandler.java
More file actions
66 lines (54 loc) · 1.94 KB
/
MoneyChainHandler.java
File metadata and controls
66 lines (54 loc) · 1.94 KB
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
package com.premaseem.ATMmoneyDispenser;
/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public abstract class MoneyChainHandler {
MoneyChainHandler nextHandler = null;
Integer noteDenomination = 0;
public MoneyChainHandler setNextHandler (MoneyChainHandler nextHandler) {
this.nextHandler = nextHandler;
return this.nextHandler;
}
public void handler (int dollarBill) {
int notes = dollarBill / noteDenomination;
int remainingAmount = dollarBill % noteDenomination;
if (notes > 0) {
System.out.printf("dispatched %d X %d = %d handled by %s \n", noteDenomination, notes, (noteDenomination * notes), this.getClass().getSimpleName());
}
if (nextHandler != null && remainingAmount > 0) {
nextHandler.handler(remainingAmount);
}
}
}
class HundrenDollarHandler_100 extends MoneyChainHandler {
public HundrenDollarHandler_100 (Integer noteDenomination) {
this.noteDenomination = noteDenomination;
}
}
class FiftyDollarHandler_50 extends MoneyChainHandler {
public FiftyDollarHandler_50 (Integer noteDenomination) {
this.noteDenomination = noteDenomination;
}
}
class TenDollarHandler_10 extends MoneyChainHandler {
public TenDollarHandler_10 (Integer noteDenomination) {
this.noteDenomination = noteDenomination;
}
}
class FiveDollarHandler_5 extends MoneyChainHandler {
public FiveDollarHandler_5 (Integer noteDenomination) {
this.noteDenomination = noteDenomination;
}
}
class TwoDollarHandler_2 extends MoneyChainHandler {
public TwoDollarHandler_2 (Integer noteDenomination) {
this.noteDenomination = noteDenomination;
}
}
class OneDollarHandler_1 extends MoneyChainHandler {
public OneDollarHandler_1 (Integer noteDenomination) {
this.noteDenomination = noteDenomination;
}
}