Create a gist now

Instantly share code, notes, and snippets.

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