2015-11-03 12:00:44 -08:00
|
|
|
// patterns/recyclec/RecycleC.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-07-28 12:48:23 -06:00
|
|
|
// {java patterns.recyclec.RecycleC}
|
2015-11-03 12:00:44 -08:00
|
|
|
package patterns.recyclec;
|
|
|
|
import patterns.trash.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
2021-03-04 16:15:04 -07:00
|
|
|
// A List that only admits the right type:
|
|
|
|
class
|
|
|
|
TrashBin<T extends Trash> extends ArrayList<T> {
|
|
|
|
final Class<T> binType;
|
|
|
|
TrashBin(Class<T> binType) {
|
|
|
|
this.binType = binType;
|
2015-11-03 12:00:44 -08:00
|
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
boolean grab(Trash t) {
|
2021-03-04 16:15:04 -07:00
|
|
|
// Compare class types:
|
2015-11-03 12:00:44 -08:00
|
|
|
if(t.getClass().equals(binType)) {
|
2021-03-04 16:15:04 -07:00
|
|
|
add((T)t); // Downcast to this TrashBin type
|
|
|
|
return true; // Trash grabbed
|
2015-11-03 12:00:44 -08:00
|
|
|
}
|
2021-03-04 16:15:04 -07:00
|
|
|
return false; // Trash not grabbed
|
2015-11-03 12:00:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 16:15:04 -07:00
|
|
|
class TrashBinList<T extends Trash>
|
|
|
|
extends ArrayList<TrashBin<? extends T>> { // [1]
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public TrashBinList(Class<? extends T>... types) {
|
|
|
|
for(Class<? extends T> type : types)
|
|
|
|
add(new TrashBin<>(type));
|
|
|
|
}
|
|
|
|
public boolean sort(T t) {
|
|
|
|
for(TrashBin<? extends T> ts : this)
|
2015-11-03 12:00:44 -08:00
|
|
|
if(ts.grab(t))
|
|
|
|
return true;
|
2021-03-04 16:15:04 -07:00
|
|
|
return false; // TrashBin not found for t
|
|
|
|
}
|
|
|
|
public void sortBin(TrashBin<T> bin) { // [2]
|
|
|
|
for(T trash : bin)
|
|
|
|
if(!sort(trash))
|
|
|
|
throw new RuntimeException(
|
|
|
|
"Bin not found for " + trash);
|
2015-11-03 12:00:44 -08:00
|
|
|
}
|
2021-03-04 16:15:04 -07:00
|
|
|
public void show() {
|
|
|
|
for(TrashBin<? extends T> tbin : this) {
|
|
|
|
String typeName = tbin.binType.getSimpleName();
|
|
|
|
TrashValue.sum(tbin, typeName);
|
|
|
|
}
|
2015-11-03 12:00:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class RecycleC {
|
|
|
|
public static void main(String[] args) {
|
2021-03-04 16:15:04 -07:00
|
|
|
TrashBin<Trash> bin =
|
|
|
|
new TrashBin<>(Trash.class);
|
2015-11-03 12:00:44 -08:00
|
|
|
ParseTrash.fillBin("trash", bin);
|
2021-03-04 16:15:04 -07:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
TrashBinList<Trash> trashBins =
|
|
|
|
new TrashBinList<>(
|
|
|
|
Aluminum.class, Paper.class, Glass.class,
|
|
|
|
// Add one item:
|
|
|
|
Cardboard.class // [3]
|
|
|
|
);
|
2021-01-31 15:42:31 -07:00
|
|
|
trashBins.sortBin(bin); // [4]
|
2021-03-04 16:15:04 -07:00
|
|
|
trashBins.show();
|
|
|
|
TrashValue.sum(bin, "Trash");
|
2015-11-03 12:00:44 -08:00
|
|
|
}
|
|
|
|
}
|
2021-03-04 16:15:04 -07:00
|
|
|
/* Output:
|
|
|
|
Loading patterns.trash.Cardboard
|
2015-11-03 12:00:44 -08:00
|
|
|
Loading patterns.trash.Paper
|
|
|
|
Loading patterns.trash.Aluminum
|
2021-03-04 16:15:04 -07:00
|
|
|
Loading patterns.trash.Glass
|
|
|
|
Aluminum weight: 1.80 * price: 1.67 = 3.01
|
|
|
|
Aluminum weight: 3.40 * price: 1.67 = 5.68
|
|
|
|
Aluminum weight: 2.70 * price: 1.67 = 4.51
|
|
|
|
Total Aluminum value = 13.19
|
|
|
|
Paper weight: 8.00 * price: 0.10 = 0.80
|
|
|
|
Paper weight: 6.60 * price: 0.10 = 0.66
|
|
|
|
Paper weight: 9.10 * price: 0.10 = 0.91
|
|
|
|
Total Paper value = 2.37
|
|
|
|
Glass weight: 5.40 * price: 0.23 = 1.24
|
|
|
|
Glass weight: 4.30 * price: 0.23 = 0.99
|
|
|
|
Glass weight: 3.60 * price: 0.23 = 0.83
|
|
|
|
Total Glass value = 3.06
|
|
|
|
Cardboard weight: 4.40 * price: 0.11 = 0.48
|
|
|
|
Cardboard weight: 2.20 * price: 0.11 = 0.24
|
|
|
|
Cardboard weight: 1.20 * price: 0.11 = 0.13
|
|
|
|
Total Cardboard value = 0.86
|
|
|
|
Cardboard weight: 4.40 * price: 0.11 = 0.48
|
|
|
|
Paper weight: 8.00 * price: 0.10 = 0.80
|
|
|
|
Aluminum weight: 1.80 * price: 1.67 = 3.01
|
|
|
|
Glass weight: 5.40 * price: 0.23 = 1.24
|
|
|
|
Aluminum weight: 3.40 * price: 1.67 = 5.68
|
|
|
|
Cardboard weight: 2.20 * price: 0.11 = 0.24
|
|
|
|
Glass weight: 4.30 * price: 0.23 = 0.99
|
|
|
|
Cardboard weight: 1.20 * price: 0.11 = 0.13
|
|
|
|
Paper weight: 6.60 * price: 0.10 = 0.66
|
|
|
|
Aluminum weight: 2.70 * price: 1.67 = 4.51
|
|
|
|
Paper weight: 9.10 * price: 0.10 = 0.91
|
|
|
|
Glass weight: 3.60 * price: 0.23 = 0.83
|
|
|
|
Total Trash value = 19.48
|
2015-11-03 12:00:44 -08:00
|
|
|
*/
|