2015-05-18 23:05:20 -07:00
|
|
|
//: patterns/trashvisitor/TrashVisitor.java
|
2015-05-05 11:20:13 -07:00
|
|
|
// The "visitor" pattern.
|
|
|
|
package patterns.trashvisitor;
|
|
|
|
import patterns.trash.*;
|
|
|
|
import java.util.*;
|
2015-05-18 23:05:20 -07:00
|
|
|
import static net.mindview.util.Print.*;
|
2015-05-05 11:20:13 -07:00
|
|
|
|
|
|
|
// 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
|
|
|
|
@Override
|
|
|
|
public void visit(VAluminum al) {
|
|
|
|
double v = al.weight() * al.value();
|
2015-05-18 23:05:20 -07:00
|
|
|
print("value of Aluminum= " + v);
|
2015-05-05 11:20:13 -07:00
|
|
|
alSum += v;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void visit(VPaper p) {
|
|
|
|
double v = p.weight() * p.value();
|
2015-05-18 23:05:20 -07:00
|
|
|
print("value of Paper= " + v);
|
2015-05-05 11:20:13 -07:00
|
|
|
pSum += v;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void visit(VGlass g) {
|
|
|
|
double v = g.weight() * g.value();
|
2015-05-18 23:05:20 -07:00
|
|
|
print("value of Glass= " + v);
|
2015-05-05 11:20:13 -07:00
|
|
|
gSum += v;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void visit(VCardboard c) {
|
|
|
|
double v = c.weight() * c.value();
|
2015-05-18 23:05:20 -07:00
|
|
|
print("value of Cardboard = " + v);
|
2015-05-05 11:20:13 -07:00
|
|
|
cSum += v;
|
|
|
|
}
|
|
|
|
void total() {
|
2015-05-18 23:05:20 -07:00
|
|
|
print(
|
2015-05-05 11:20:13 -07:00
|
|
|
"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
|
|
|
|
@Override
|
|
|
|
public void visit(VAluminum al) {
|
|
|
|
alSum += al.weight();
|
2015-05-18 23:05:20 -07:00
|
|
|
print("Aluminum weight = " + al.weight());
|
2015-05-05 11:20:13 -07:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void visit(VPaper p) {
|
|
|
|
pSum += p.weight();
|
2015-05-18 23:05:20 -07:00
|
|
|
print("Paper weight = " + p.weight());
|
2015-05-05 11:20:13 -07:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void visit(VGlass g) {
|
|
|
|
gSum += g.weight();
|
2015-05-18 23:05:20 -07:00
|
|
|
print("Glass weight = " + g.weight());
|
2015-05-05 11:20:13 -07:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void visit(VCardboard c) {
|
|
|
|
cSum += c.weight();
|
2015-05-18 23:05:20 -07:00
|
|
|
print("Cardboard weight = " + c.weight());
|
2015-05-05 11:20:13 -07:00
|
|
|
}
|
|
|
|
void total() {
|
2015-05-18 23:05:20 -07:00
|
|
|
print("Total weight Aluminum:" + alSum);
|
|
|
|
print("Total weight Paper:" + pSum);
|
|
|
|
print("Total weight Glass:" + gSum);
|
|
|
|
print("Total weight Cardboard:" + cSum);
|
2015-05-05 11:20:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class TrashVisitor {
|
|
|
|
public static void main(String[] args) {
|
2015-05-27 23:30:19 -07:00
|
|
|
ArrayList<Trash> bin = new ArrayList<>();
|
2015-05-05 11:20:13 -07:00
|
|
|
// ParseTrash still works, without changes:
|
|
|
|
ParseTrash.fillBin("VTrash.dat", bin);
|
|
|
|
// You could even iterate through
|
|
|
|
// a list of visitors!
|
|
|
|
PriceVisitor pv = new PriceVisitor();
|
|
|
|
WeightVisitor wv = new WeightVisitor();
|
2015-05-27 23:30:19 -07:00
|
|
|
for(Trash aBin : bin) {
|
|
|
|
Visitable v = (Visitable) aBin;
|
2015-05-05 11:20:13 -07:00
|
|
|
v.accept(pv);
|
|
|
|
v.accept(wv);
|
|
|
|
}
|
|
|
|
pv.total();
|
|
|
|
wv.total();
|
|
|
|
}
|
|
|
|
} ///:~
|