import tester.*; // some grocery items interface IItem { // is this the same IItem as other? boolean sameItem(IItem x); // is this Coffee? boolean isCoffee(); // convert this to Coffee (if feasible) Coffee toCoffee(); // is this Tea? boolean isTea(); // convert this to Tea (if feasible) Tea toTea(); } // to represent a coffee in a grocery store class Coffee implements IItem { private String origin; private int price; // the constructor Coffee(String origin, int price){ this.origin = origin; this.price = price; } public boolean isCoffee(){ return true; } public boolean isTea(){ return false; } public Coffee toCoffee(){ return this; } public Tea toTea(){ throw new RuntimeException("not a tea"); } public boolean sameItem(IItem other){ return (other.isCoffee()) && other.toCoffee().sameCoffee(this); } // is this the same Coffee as other? private boolean sameCoffee(Coffee other){ return this.origin.equals(other.origin) && this.price == other.price; } } //to represent a tea in a grocery store class Tea implements IItem { private String kind; private int price; // the constructor Tea(String kind, int price){ this.kind = kind; this.price = price; } public boolean isTea(){ return true; } public boolean isCoffee(){ return false; } public Tea toTea(){ return this; } public Coffee toCoffee(){ throw new RuntimeException("not a coffee"); } public boolean sameItem(IItem other) { return other.isTea() && other.toTea().sameTea(this); } // is this the same Tea as other? private boolean sameTea(Tea other){ return this.kind.equals(other.kind) && this.price == other.price; } } class ExamplesGroceryItems{ ExamplesGroceryItems(){} IItem tea1 = new Tea("Oolong", 200); IItem tea2 = new Tea("Oolong", 300); IItem tea3 = new Tea("Oolong", 300); IItem coffee1 = new Coffee("Kona", 300); IItem coffee2 = new Coffee("Java", 200); IItem coffee3 = new Coffee("Java", 200); boolean testSameItem(Tester t){ return t.checkExpect(this.tea1.sameItem(this.tea2), false) && t.checkExpect(this.tea3.sameItem(this.tea2), true) && t.checkExpect(this.coffee1.sameItem(this.coffee2), false) && t.checkExpect(this.coffee3.sameItem(this.coffee2), true) && t.checkExpect(this.coffee3.sameItem(this.tea2), false) && t.checkExpect(this.tea3.sameItem(this.coffee1), false) && t.checkException(new RuntimeException("not a tea"), this.coffee1, "toTea") && t.checkException(new RuntimeException("not a coffee"), this.tea2, "toCoffee"); } }