// purpose: demonstrate cupcake manipulation import java.util.*; import java.net.*; import java.io.*; public class CupcakeOtherDemo { public static void main( String[] args ) throws IOException { Scanner stdin = new Scanner( System.in ); System.out.print( "Provide three ids: " ); String id1 = stdin.next(); String id2 = stdin.next(); String id3 = stdin.next(); Cupcake c = makeCupcake( id1 ); System.out.println( "c = " + c ); Cupcake d = makeCupcake( id2 ); System.out.println( "d = " + d ); Cupcake e = makeCupcake( id3 ); System.out.println( "e = " + e ); Flavor f1 = c.getCake(); Flavor f2 = c.getIcing(); boolean b = c.isSprinkled(); e.setCake( f1 ); e.setIcing( f2 ); e.setSprinkled( b ); Cupcake f = c; System.out.println( "f = " + f ); // test == operator if ( c == d ) { System.out.println( "c == d: " + true ); } else { System.out.println( "c == d: " + false ); } if ( c == e ) { System.out.println( "c == e: " + true ); } else { System.out.println( "c == e: " + false ); } if ( c == f ) { System.out.println( "c == f: " + true ); } else { System.out.println( "c == f: " + false ); } // test equals() method if ( c.equals( d ) ) { System.out.println( "c.equals( d ): " + true ); } else { System.out.println( "c.equals( d ): " + false ); } if ( c.equals( e ) ) { System.out.println( "c.equals( e ): " + true ); } else { System.out.println( "c.equals( e ): " + false ); } if ( c.equals( f ) ) { System.out.println( "c.equals( f ): " + true ); } else { System.out.println( "c.equals( f ): " + false ); } } public static Cupcake makeCupcake( String id ) throws IOException { Scanner stream = getURLStream( id, "cupcake.txt" ); String s1 = stream.next(); String s2 = stream.next(); boolean b = stream.nextBoolean(); Flavor f1 = Flavor.cast( s1 ); Flavor f2 = Flavor.cast( s2 ); Cupcake c = new Cupcake( f1, f2, b ); return c; } public static Scanner getURLStream( String id, String filename ) throws IOException { String base = "http://www.cs.virginia.edu/cs101x/people/"; String u = base + id + "/" + filename; URL url = new URL( u ); Scanner stream = new Scanner( url.openStream() ); return stream; } }