34 lines
924 B
Java
Raw Normal View History

2015-05-18 23:05:20 -07:00
//: patterns/recycleap/RecycleAP.java
2015-05-05 11:20:13 -07:00
// Recycling with RTTI and Prototypes.
package patterns.recycleap;
import patterns.trash.*;
import java.util.*;
public class RecycleAP {
public static void main(String[] args) {
2015-05-18 23:05:20 -07:00
ArrayList<Trash> bin = new ArrayList<>();
2015-05-05 11:20:13 -07:00
// Fill up the Trash bin:
ParseTrash.fillBin("Trash.dat", bin);
2015-05-18 23:05:20 -07:00
ArrayList<Trash>
glassBin = new ArrayList<>(),
paperBin = new ArrayList<>(),
alBin = new ArrayList<>();
Iterator<Trash> sorter = bin.iterator();
2015-05-05 11:20:13 -07:00
// Sort the Trash:
while(sorter.hasNext()) {
2015-05-18 23:05:20 -07:00
Trash t = sorter.next();
2015-05-05 11:20:13 -07:00
// 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);
}
} ///:~