Repository navigation
Expand file tree
/
Copy pathTrashVisitor.java
More file actions
143 lines (126 loc) · 3.8 KB
/
Copy pathTrashVisitor.java
File metadata and controls
143 lines (126 loc) · 3.8 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// patterns/trashvisitor/TrashVisitor.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {java patterns.trashvisitor.TrashVisitor}
package trashvisitor;
import patterns.trash.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// Specific group of algorithms packaged
// in each implementation of Visitor:
class PriceVisitor implements Visitor {
private double alSum; // Aluminum
private double pSum; // Paper
private double gSum; // Glass
private double cSum; // Cardboard
public static void show(String s) {
System.out.println(s);
}
@Override
public void visit(Aluminum al) {
double v = al.weight() * al.value();
show("value of Aluminum= " + v);
alSum += v;
}
@Override
public void visit(Paper p) {
double v = p.weight() * p.value();
show("value of Paper= " + v);
pSum += v;
}
@Override
public void visit(Glass g) {
double v = g.weight() * g.value();
show("value of Glass= " + v);
gSum += v;
}
@Override
public void visit(Cardboard c) {
double v = c.weight() * c.value();
show("value of Cardboard = " + v);
cSum += v;
}
@Override
public void total() {
show(
"Total Aluminum: $" + alSum + "\n" +
"Total Paper: $" + pSum + "\n" +
"Total Glass: $" + gSum + "\n" +
"Total Cardboard: $" + cSum);
}
}
class WeightVisitor implements Visitor {
private double alSum; // Aluminum
private double pSum; // Paper
private double gSum; // Glass
private double cSum; // Cardboard
public static void show(String s) {
System.out.println(s);
}
@Override
public void visit(Aluminum al) {
alSum += al.weight();
show("Aluminum weight = " + al.weight());
}
@Override
public void visit(Paper p) {
pSum += p.weight();
show("Paper weight = " + p.weight());
}
@Override
public void visit(Glass g) {
gSum += g.weight();
show("Glass weight = " + g.weight());
}
@Override
public void visit(Cardboard c) {
cSum += c.weight();
show("Cardboard weight = " + c.weight());
}
@Override
public void total() {
show("Total weight Aluminum:" + alSum);
show("Total weight Paper:" + pSum);
show("Total weight Glass:" + gSum);
show("Total weight Cardboard:" + cSum);
}
}
public class TrashVisitor {
public static void main(String[] args) {
List<Trash> bin = new ArrayList<>();
// ParseTrash still works, without changes:
ParseTrash.fillBin("trashvisitor", bin);
List<Visitor> visitors = Arrays.asList(
new PriceVisitor(), new WeightVisitor());
bin.forEach(t -> {
Visitable v = (Visitable) t;
visitors.forEach(visitor -> v.accept(visitor));
});
visitors.forEach(Visitor::total);
}
}
/* Output: (First and Last 10 Lines)
Loading patterns.trashvisitor.Glass
Loading patterns.trashvisitor.Paper
Loading patterns.trashvisitor.Aluminum
Loading patterns.trashvisitor.Cardboard
value of Glass= 12.420000225305557
Glass weight = 54.0
value of Paper= 2.2000000327825546
Paper weight = 22.0
value of Paper= 1.1000000163912773
Paper weight = 11.0
...________...________...________...________...
value of Cardboard = 5.060000091791153
Cardboard weight = 22.0
Total Aluminum: $860.0499778985977
Total Paper: $35.80000053346157
Total Glass: $150.1900027245283
Total Cardboard: $40.02000072598457
Total weight Aluminum:515.0
Total weight Paper:358.0
Total weight Glass:653.0
Total weight Cardboard:174.0
*/