// Java version of Food Visitors Program. Designed for AOP assignment interface visitor { Object visit(fruit x); Object visit(vegetable x); Object visit(meat x); Object visit(nofood x); } interface visitee { int calories(); Object accept(visitor v); } //// concrete visitees // common superclass class foodbase { public String name; public int cals; public visitee next; // implements linked list public int calories() { return cals; } public foodbase(String n, int c, visitee t) { name=n; cals=c; next=t;} }//foodbase class fruit extends foodbase implements visitee { public fruit(String n, visitee t) { super(n,100,t); } // all fruits have 100 calories public Object accept(visitor v) {return v.visit(this);} } class vegetable extends foodbase implements visitee { public String color; // vegetable has additional attribute public vegetable(String n, String c, visitee t) { super(n, 0, t); color = c; } // chew your food! public int calories() { return (int)(Math.random()*500); } public Object accept(visitor v) {return v.visit(this);} } class meat extends foodbase implements visitee { public boolean red; // true if red meat, else white meat public meat(String n, boolean m, visitee t) { super(n,0,t); red=m;} public int calories() { if (red) return 2000; else return 1000; } public Object accept(visitor v) {return v.visit(this);} } class nofood implements visitee { public int calories() { return 0; } public Object accept(visitor v) {return v.visit(this);} } ////////////////// Visitor classes class eater implements visitor // eats and return sum calories ate { private int ax = 0; // calories accumulator public Object visit(fruit x) { ax += x.calories(); return x.next.accept(this); } public Object visit(meat x) { ax += x.calories(); return x.next.accept(this); } public Object visit(vegetable x) { if (x.color.equals("green")) System.out.println("I won't eat green vegetables!"); else ax += x.calories(); return x.next.accept(this); } public Object visit(nofood x) { return new Integer(ax); // older style, but some systems prefer } }//eater visitor // how would you implement a vegetarian eater? class printer implements visitor // prints list { public Object visit(fruit x) { System.out.println("fruit " +x.name); return x.next.accept(this); } public Object visit(vegetable x) { System.out.println(x.color+" vegetable "+x.name); return x.next.accept(this); } public Object visit(meat x) { String r = "white meat "; if (x.red) r = "red meat "; System.out.println(r+x.name); return x.next.accept(this); } public Object visit(nofood x) { return null; } }// printer ////////////////// main for testing (recommend move to different file) public class food { public static void main(String[] argv) throws Exception { visitee a = new fruit("apple",new nofood()); visitee b = new fruit("watermellon",a); visitee c = new vegetable("cabbage soup","white",b); visitee d = new vegetable("spinach","green",c); visitee e = new meat("chicken",false,d); visitee f = new meat("beef",true,e); // f is start of list visitee thefood = f; // start of list; thefood.accept(new printer()); // prints list Integer ate = (Integer)thefood.accept(new eater()); // bon appetite System.out.println("just ate "+ate+" calories, yum!"); }// main }