Bruce Eckel 88dbdbbbcb update
2015-05-18 23:05:30 -07:00

34 lines
924 B
Java

//: patterns/recycleap/RecycleAP.java
// Recycling with RTTI and Prototypes.
package patterns.recycleap;
import patterns.trash.*;
import java.util.*;
public class RecycleAP {
public static void main(String[] args) {
ArrayList<Trash> bin = new ArrayList<>();
// Fill up the Trash bin:
ParseTrash.fillBin("Trash.dat", bin);
ArrayList<Trash>
glassBin = new ArrayList<>(),
paperBin = new ArrayList<>(),
alBin = new ArrayList<>();
Iterator<Trash> sorter = bin.iterator();
// Sort the Trash:
while(sorter.hasNext()) {
Trash t = sorter.next();
// RTTI to show class membership:
if(t instanceof Aluminum)
alBin.add(t);
if(t instanceof Paper)
paperBin.add(t);
if(t instanceof Glass)
glassBin.add(t);
}
Trash.sumValue(alBin);
Trash.sumValue(paperBin);
Trash.sumValue(glassBin);
Trash.sumValue(bin);
}
} ///:~