package a2Safecasting; /*********************************************** * Coffee, done right ***********************************************/ import tester.*; interface Drink { // Is this drink the same as that drink? Boolean same(Drink that); // Is this drink a tea? Boolean isTea(); // Is this drink a coffee? Boolean isCoffee(); // Is this drink a latte? Boolean isLatte(); // Cast this drink to a tea Tea toTea(); // Cast this drink a coffee Coffee toCoffee(); // Cast this drink to a latte Latte toLatte(); } abstract class ADrink { public Boolean isTea() { return false; } public Boolean isCoffee() { return false; } public Boolean isLatte() { return false; } // Turn you into a Tea public Tea toTea() { throw new RuntimeException("What were you thinking!"); } // Turn you into a Coffee public Coffee toCoffee() { throw new RuntimeException("What were you thinking!"); } // Turn you into a Latte public Latte toLatte() { throw new RuntimeException("What were you thinking!"); } } class Tea extends ADrink implements Drink { // Is this tea the same as that drink? public Boolean same(Drink that) { if (that.isTea()) return this.sameTea(that.toTea()); else return false; } // Is this tea drink a tea? public Boolean isTea() { return true; } // Turn you into a Tea public Tea toTea() { return this; } // Is this tea drink the same as the given tea drink? public boolean sameTea(Tea that){ return true; } } class Coffee extends ADrink implements Drink { // Is this coffee the same as that drink? public Boolean same(Drink that) { if (that.isCoffee()) return this.sameCoffee(that.toCoffee()); else return false; } // Is this coffee drink a coffee? public Boolean isCoffee() { return true; } // Turn you into a Coffee public Coffee toCoffee() { return this; } // Is this coffee drink the same as the given coffee drink? public boolean sameCoffee(Coffee that){ return true; } } class Latte extends Coffee { // Is this latte the same as that drink? public Boolean same(Drink that) { if (that.isLatte()) return this.sameLatte(that.toLatte()); else return false; } // Is this latte drink a coffee? public Boolean isCoffee() { return false; } // Is this latte drink a latte? public Boolean isLatte() { return true; } // Turn you into a Latte public Latte toLatte() { return this; } // Is this latte drink the same as the given latte drink? public Boolean sameLatte(Latte that) { return false; } } public class ExamplesCoffeeSafeCasting { ExamplesCoffeeSafeCasting(){} public static ExamplesCoffeeSafeCasting examplesInstance = new ExamplesCoffeeSafeCasting(); Drink t = new Tea(); Drink c = new Coffee(); Drink l = new Latte(); public Boolean testSameDrink(Tester t) { return t.checkExpect(this.t.same(this.t), true) && t.checkExpect(this.c.same(this.c), true) && t.checkExpect(this.t.same(this.c), false) && t.checkExpect(this.c.same(this.t), false); } public Boolean testSameCoffee(Tester t) { return t.checkExpect(this.l.same(this.c), false) // works! && t.checkExpect(this.c.same(this.l), false); // still works! } // run all tests public static void main(String[] argv){ ExamplesCoffeeSafeCasting e = new ExamplesCoffeeSafeCasting(); Tester.runReport(e, false, false); } }