Created
March 6, 2012 20:34
-
-
Save sandromancuso/1988805 to your computer and use it in GitHub Desktop.
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
public class ShoppingBasketBuilder { | |
private Map<Product, Integer> products = new HashMap<Product, Integer>(); | |
public static ShoppingBasketBuilder aShoppingBasket() { | |
return new ShoppingBasketBuilder(); | |
} | |
public ShoppingBasketBuilder with(Product product) { | |
this.products.put(product, 1); | |
return this; | |
} | |
public ShoppingBasketBuilder with(ProductBuilder productBuilder) { | |
return with(productBuilder.build()); | |
} | |
public ShoppingBasketBuilder with(Integer quantity, Product product) { | |
this.products.put(product, quantity); | |
return this; | |
} | |
public ShoppingBasketBuilder with(Integer quantity, ProductBuilder productBuider) { | |
return with(quantity, productBuider.build()); | |
} | |
public ShoppingBasket build() { | |
ShoppingBasket shoppingBasket = new ShoppingBasket(); | |
for (Map.Entry<Product, Integer> entry : products.entrySet()) { | |
shoppingBasket.add(entry.getKey(), entry.getValue()); | |
} | |
return shoppingBasket; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment