Repository navigation
Expand file tree
/
Copy pathBins.java
More file actions
39 lines (35 loc) · 1.34 KB
/
Copy pathBins.java
File metadata and controls
39 lines (35 loc) · 1.34 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
// patterns/trash/Bins.java
// (c)2021 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.
package patterns.trash;
import java.util.ArrayList;
import java.util.List;
public class Bins {
final List<patterns.trash.Trash> bin;
final List<Aluminum> aluminum = new ArrayList<>();
final List<Paper> paper = new ArrayList<>();
final List<Glass> glass = new ArrayList<>();
final List<Cardboard> cardboard = new ArrayList<>();
public Bins(List<patterns.trash.Trash> source) {
bin = new ArrayList<>(source); // Copy
bin.forEach(t -> {
// Use reflection to discover Trash type:
if (t instanceof Aluminum)
aluminum.add((Aluminum) t);
if (t instanceof Paper)
paper.add((Paper) t);
if (t instanceof Glass)
glass.add((Glass) t);
if (t instanceof Cardboard)
cardboard.add((Cardboard) t);
});
}
public void show() {
patterns.trash.TrashValue.sum(aluminum, "Aluminum");
patterns.trash.TrashValue.sum(paper, "Paper");
patterns.trash.TrashValue.sum(glass, "Glass");
patterns.trash.TrashValue.sum(cardboard, "Cardboard");
patterns.trash.TrashValue.sum(bin, "Trash");
}
}