public

  • Download Gist
ShoppingBasketBuilder.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
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;
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.