Skip to content

Instantly share code, notes, and snippets.

@sandromancuso
Created March 6, 2012 20:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandromancuso/1988805 to your computer and use it in GitHub Desktop.
Save sandromancuso/1988805 to your computer and use it in GitHub Desktop.
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