-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sample Solutions Updated (Fall 2022)
- Loading branch information
1 parent
9b0514f
commit 109d716
Showing
42 changed files
with
839 additions
and
2 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file not shown.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
134 changes: 134 additions & 0 deletions
134
demos/demo8/counter-burger-updated-bluej/BuildOrder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
|
||
public class BuildOrder { | ||
public static Component getOrder() | ||
{ | ||
Composite order = new Composite( "Order" ) ; | ||
|
||
// Get Custom Burgers | ||
CustomBurger customBurger1 = getBurger1() ; | ||
CustomBurger customBurger2 = getBurger2() ; | ||
|
||
// Add Custom Burger to the Order | ||
order.addChild( customBurger1 ); | ||
order.addChild( customBurger2 ); | ||
|
||
return order ; | ||
} | ||
|
||
private static CustomBurger getBurger1() { | ||
CustomBurger customBurger1 = new CustomBurger( "Build Your Own Burger" ) ; | ||
// Burger | ||
Burger b = new Burger( "Burger" ) ; | ||
String[] bo = { "Organic Bison*", "1/2lb.", "On A Bun" } ; | ||
b.setOptions( bo ) ; | ||
// Cheese | ||
Cheese c = new Cheese( "Cheese" ) ; | ||
String[] co = { "Yellow American", "Spicy Jalapeno Jack" } ; | ||
c.setOptions( co ) ; | ||
c.wrapDecorator( b ) ; | ||
// Premium Cheese | ||
PremiumCheese pc = new PremiumCheese( "Premium Cheese" ) ; | ||
String[] pco = { "Danish Blue Cheese" } ; | ||
pc.setOptions( pco ) ; | ||
pc.wrapDecorator( c ) ; | ||
// Sauce | ||
Sauce s = new Sauce( "Sauce" ) ; | ||
String[] so = { "Mayonnaise", "Thai Peanut Sauce" } ; | ||
s.setOptions( so ) ; | ||
s.wrapDecorator( pc ) ; | ||
// Unlimited Toppings | ||
UnlimitedToppings ut = new UnlimitedToppings( "Unlimited Toppings" ) ; | ||
String[] uto = { "Dill Pickle Chips", "Black Olives", "Spicy Pickles" } ; | ||
ut.setOptions( uto ) ; | ||
ut.wrapDecorator( s ) ; | ||
// Premium Toppings | ||
PremiumToppings pt = new PremiumToppings( "Premium Toppings" ) ; | ||
String[] pto = { "Marinated Tomatoes" } ; | ||
pt.setOptions( pto ) ; | ||
pt.wrapDecorator( ut ) ; | ||
// Bun Choice | ||
BunChoice bc = new BunChoice( "Bun Choice" ) ; | ||
String[] bco = { "Ciabatta (Vegan)" } ; | ||
bc.setOptions( bco ) ; | ||
bc.wrapDecorator( pt ) ; | ||
// Extra Sides | ||
ExtraSides ex = new ExtraSides( "Extra Sides" ) ; | ||
String[] exo = { "Shoestring Fries" } ; | ||
ex.setOptions( exo ) ; | ||
ex.wrapDecorator( bc ) ; | ||
|
||
// Setup Custom Burger Ingredients | ||
customBurger1.setDecorator( ex ) ; | ||
customBurger1.addChild( b ) ; | ||
customBurger1.addChild( c ) ; | ||
customBurger1.addChild( pc ) ; | ||
customBurger1.addChild( s ) ; | ||
customBurger1.addChild( ut ) ; | ||
customBurger1.addChild( pt ) ; | ||
customBurger1.addChild( bc ) ; | ||
customBurger1.addChild( ex ) ; | ||
|
||
// return custom burger | ||
return customBurger1 ; | ||
} | ||
|
||
private static CustomBurger getBurger2() { | ||
|
||
CustomBurger customBurger2 = new CustomBurger( "Build Your Own Burger" ) ; | ||
|
||
// Burger | ||
Burger b = new Burger( "Burger" ) ; | ||
String[] bo = { "Hormone & Antibiotic Free Beef*", "1/3lb.", "On A Bun" } ; | ||
b.setOptions( bo ) ; | ||
// Cheese | ||
Cheese c = new Cheese( "Cheese" ) ; | ||
String[] co = { "Smoked Gouda", "Greek Feta" } ; | ||
c.setOptions( co ) ; | ||
c.wrapDecorator( b ) ; | ||
// Premium Cheese | ||
PremiumCheese pc = new PremiumCheese( "Premium Cheese" ) ; | ||
String[] pco = { "Fresh Mozzarella" } ; | ||
pc.setOptions( pco ) ; | ||
pc.wrapDecorator( c ) ; | ||
// Sauce | ||
Sauce s = new Sauce( "Sauce" ) ; | ||
String[] so = { "Habanero Salsa" } ; | ||
s.setOptions( so ) ; | ||
s.wrapDecorator( pc ) ; | ||
// Unlimited Toppings | ||
UnlimitedToppings ut = new UnlimitedToppings( "Unlimited Toppings" ) ; | ||
String[] uto = { "Crushed Peanuts" } ; | ||
ut.setOptions( uto ) ; | ||
ut.wrapDecorator( s ) ; | ||
// Premium Toppings | ||
PremiumToppings pt = new PremiumToppings( "Premium Toppings" ) ; | ||
String[] pto = { "Sunny Side Up Egg*", "Marinated Tomatoes" } ; | ||
pt.setOptions( pto ) ; | ||
pt.wrapDecorator( ut ) ; | ||
// Bun Choice | ||
BunChoice bc = new BunChoice( "Bun Choice" ) ; | ||
String[] bco = { "Gluten-Free Bun" } ; | ||
bc.setOptions( bco ) ; | ||
bc.wrapDecorator( pt ) ; | ||
// Extra Sides | ||
ExtraSides ex = new ExtraSides( "Extra Sides" ) ; | ||
String[] exo = { "Shoestring Fries" } ; | ||
ex.setOptions( exo ) ; | ||
ex.wrapDecorator( bc ) ; | ||
|
||
// Setup Custom Burger Ingredients | ||
customBurger2.setDecorator( ex ) ; | ||
customBurger2.addChild( b ) ; | ||
customBurger2.addChild( c ) ; | ||
customBurger2.addChild( pc ) ; | ||
customBurger2.addChild( s ) ; | ||
customBurger2.addChild( ut ) ; | ||
customBurger2.addChild( pt ) ; | ||
customBurger2.addChild( bc ) ; | ||
customBurger2.addChild( ex ) ; | ||
|
||
// return custom burger | ||
return customBurger2 ; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
public class BunChoice extends LeafDecorator | ||
{ | ||
private String[] options ; | ||
|
||
|
||
public BunChoice( String d ) | ||
{ | ||
super(d) ; | ||
} | ||
|
||
public void setOptions( String[] options ) | ||
{ | ||
this.options = options ; | ||
for ( int i = 0; i<options.length; i++ ) | ||
{ | ||
if ( "Gluten-Free Bun".equals(options[i]) ) this.price += 1.00 ; | ||
else if ( "Hawailan Bun".equals(options[i]) ) this.price += 1.00 ; | ||
else if ( "Pretzel Bun".equals(options[i]) ) this.price += 0.50 ; | ||
else this.price += 0.00 ; | ||
} | ||
} | ||
|
||
public String getDescription() | ||
{ | ||
String desc = "" ; | ||
for ( int i = 0; i<options.length; i++ ) | ||
{ | ||
if (i>0) desc += " + " + options[i] ; | ||
else desc = options[i] ; | ||
} | ||
return desc ; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
public class Burger extends LeafDecorator | ||
{ | ||
private String[] options ; | ||
|
||
|
||
public Burger( String d ) | ||
{ | ||
super(d) ; | ||
} | ||
|
||
public void setOptions( String[] options ) | ||
{ | ||
this.options = options ; | ||
for ( int i = 0; i<options.length; i++ ) | ||
{ | ||
if ( "1/3lb.".equals(options[i]) ) this.price += 9.00 ; | ||
if ( "1/2lb.".equals(options[i]) ) this.price += 12.00 ; | ||
if ( "1lb.".equals(options[i]) ) this.price += 18.00 ; | ||
if ( "In A Bowl".equals(options[i]) ) this.price += 1.50 ; | ||
if ( "Organic Bison*".equals(options[i]) ) this.price += 4.00 ; | ||
if ( "Ahi Tuna*".equals(options[i]) ) this.price += 4.00 ; | ||
} | ||
} | ||
|
||
public String getDescription() | ||
{ | ||
String desc = "" ; | ||
for ( int i = 0; i<options.length; i++ ) | ||
{ | ||
if (i>0) desc += " + " + options[i] ; | ||
else desc = options[i] ; | ||
} | ||
return desc ; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
public class Cheese extends LeafDecorator | ||
{ | ||
private String[] options ; | ||
|
||
|
||
public Cheese( String d ) | ||
{ | ||
super(d) ; | ||
} | ||
|
||
// 1 cheese free, extra cheese +1.00 | ||
public void setOptions( String[] options ) | ||
{ | ||
this.options = options ; | ||
if ( options.length > 1 ) | ||
this.price += (options.length-1) * 1.00 ; | ||
} | ||
|
||
public String getDescription() | ||
{ | ||
String desc = "" ; | ||
for ( int i = 0; i<options.length; i++ ) | ||
{ | ||
if (i>0) desc += " + " + options[i] ; | ||
else desc = options[i] ; | ||
} | ||
return desc ; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
|
||
public class Client { | ||
|
||
public static void runTest() | ||
{ | ||
Component theOrder = BuildOrder.getOrder() ; | ||
theOrder.printDescription(); | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
|
||
public interface Component { | ||
|
||
void printDescription() ; | ||
void addChild(Component c); | ||
void removeChild(Component c); | ||
Component getChild(int i); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
|
||
import java.util.ArrayList; | ||
|
||
public class Composite implements Component { | ||
|
||
protected ArrayList<Component> components = new ArrayList<Component>() ; | ||
protected String description ; | ||
|
||
public Composite ( String d ) | ||
{ | ||
description = d ; | ||
} | ||
|
||
public void printDescription() { | ||
System.out.println( "\n" + description ); | ||
for (Component obj : components) | ||
{ | ||
obj.printDescription(); | ||
} | ||
} | ||
|
||
public void addChild(Component c) { | ||
components.add( c ) ; | ||
} | ||
|
||
public void removeChild(Component c) { | ||
components.remove(c) ; | ||
} | ||
|
||
public Component getChild(int i) { | ||
return components.get( i ) ; | ||
} | ||
|
||
} | ||
|
28 changes: 28 additions & 0 deletions
28
demos/demo8/counter-burger-updated-bluej/CustomBurger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
import java.text.DecimalFormat; | ||
|
||
public class CustomBurger extends Composite | ||
{ | ||
PriceDecorator decorator = null ; | ||
|
||
public CustomBurger ( String d ) | ||
{ | ||
super(d) ; | ||
} | ||
|
||
public void setDecorator( PriceDecorator p ) | ||
{ | ||
this.decorator = p ; | ||
} | ||
|
||
public void printDescription() { | ||
DecimalFormat fmt = new DecimalFormat("0.00"); | ||
System.out.println( "\n\n" + description + " " + fmt.format(decorator.getPrice()) ); | ||
for (Component obj : components) | ||
{ | ||
obj.printDescription(); | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
public class ExtraSides extends LeafDecorator | ||
{ | ||
private String[] options ; | ||
|
||
|
||
public ExtraSides( String d ) | ||
{ | ||
super(d) ; | ||
} | ||
|
||
public void setOptions( String[] options ) | ||
{ | ||
this.options = options ; | ||
this.price += (options.length) * 3.00 ; | ||
} | ||
|
||
public String getDescription() | ||
{ | ||
String desc = "" ; | ||
for ( int i = 0; i<options.length; i++ ) | ||
{ | ||
if (i>0) desc += " + " + options[i] ; | ||
else desc = options[i] ; | ||
} | ||
return desc ; | ||
} | ||
|
||
} |
Oops, something went wrong.