// purpose: demonstrate cupcake manipulation import java.util.*; import java.net.*; import java.io.*; public class CupcakeDemo { 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 c1 = makeCupcake( id1 ); Cupcake c2 = makeCupcake( id2 ); Cupcake c3 = makeCupcake( id3 ); System.out.println( id1 + " = " + c1 ); System.out.println( id2 + " = " + c2 ); System.out.println( id3 + " = " + c3 ); System.out.println(); for ( int j = 0; j < 2; ++j ) { System.out.print( "User # and cake flavor: " ); int number = stdin.nextInt(); String s = stdin.next().toUpperCase(); Flavor f; if ( s.equals( "CHOCOLATE" ) ) { f = Flavor.CHOCOLATE; } else { f = Flavor.VANILLA; } if ( number == 1 ) { c1.setCake( f ); } else if ( number == 2 ) { c2.setCake( f ); } else { c3.setCake( f ); } System.out.println( id1 + " = " + c1 ); System.out.println( id2 + " = " + c2 ); System.out.println( id3 + " = " + c3 ); System.out.println(); } System.out.println(); for ( int j = 0; j < 2; ++j ) { System.out.print( "User # and icing flavor: " ); int number = stdin.nextInt(); String s = stdin.next().toUpperCase(); Flavor f; if ( s.equals( "CHOCOLATE" ) ) { f = Flavor.CHOCOLATE; } else { f = Flavor.VANILLA; } if ( number == 1 ) { c1.setIcing( f ); } else if ( number == 2 ) { c2.setIcing( f ); } else { c3.setIcing( f ); } System.out.println( id1 + " = " + c1 ); System.out.println( id2 + " = " + c2 ); System.out.println( id3 + " = " + c3 ); System.out.println(); } System.out.println(); for ( int j = 0; j < 2; ++j ) { System.out.print( "User # and whethered sprinkled: " ); int number = stdin.nextInt(); boolean b = stdin.nextBoolean(); if ( number == 1 ) { c1.setSprinkled( b ); } else if ( number == 2 ) { c2.setSprinkled( b ); } else { c3.setSprinkled( b ); } System.out.println( id1 + " = " + c1 ); System.out.println( id2 + " = " + c2 ); System.out.println( id3 + " = " + c3 ); System.out.println(); } } public static Cupcake makeCupcake( String id ) throws IOException { Scanner stream = getURLStream( id, "cupcake.txt" ); String s1 = stream.next().toUpperCase(); String s2 = stream.next().toUpperCase(); boolean b = stream.nextBoolean(); Flavor f1; if ( s1.equals( "CHOCOLATE" ) ) { f1 = Flavor.CHOCOLATE; } else { f1 = Flavor.VANILLA; } Flavor f2; if ( s1.equals( "CHOCOLATE" ) ) { f2 = Flavor.CHOCOLATE; } else { f2 = Flavor.VANILLA; } 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; } }