I've tried to answer some questions to help newbies like me in this post by Satish Talim. Hope it helps.
Will try to share some more inputs on my experiments with Sinatra in the days to come.
public void remove(String string, int quantity) { | |
Iterator<Map<TaxableItem, Integer>> itemMapIterator = _items.iterator(); | |
int itemIndex = -1; | |
while (itemMapIterator.hasNext()) { | |
Map<TaxableItem, Integer> itemMap = itemMapIterator.next(); | |
TaxableItem item = itemMap.keySet().iterator().next(); | |
if (item.getName() == string) { | |
itemIndex = _items.indexOf(itemMap); | |
} | |
} | |
if (itemIndex >= 0) { | |
Map<TaxableItem, Integer> itemMap = _items.get(itemIndex); | |
Iterator<Integer> quantityIterator = itemMap.values().iterator(); | |
int itemQtyinBasket = quantityIterator.next(); | |
if (quantity >= itemQtyinBasket) { | |
_items.remove(itemIndex); | |
} else { | |
Iterator<TaxableItem> itemIterator = itemMap.keySet() | |
.iterator(); | |
TaxableItem item = itemIterator.next(); | |
itemMap.put(item, itemQtyinBasket - quantity); | |
} | |
} | |
} |
public void remove(String name, int quantity) | |
throws ShoppingBasketException { | |
ShoppingBasketItem sbItemToRemove = findItemFromBasket(name); | |
if (sbItemToRemove != null) { | |
if (quantity > sbItemToRemove.getQuantity()) { | |
itemsInBasket.remove(sbItemToRemove); | |
} else { | |
sbItemToRemove.reduceQuantityBy(quantity); | |
} | |
} | |
} |
public void remove(String name, int quantity) | |
throws ShoppingBasketException { | |
ShoppingBasketItem sbItemToRemove = findItemFromBasket(name); | |
performRemove(sbItemToRemove, quantity); | |
} |
private ShoppingBasketItem findItemFromBasket(String name) { | |
Iterator<ShoppingBasketItem> itemIterator = itemsInBasket.iterator(); | |
while (itemIterator.hasNext()) { | |
ShoppingBasketItem sbItem = itemIterator.next(); | |
if (sbItem.getName().equals(name)) { | |
return sbItem; | |
} | |
} | |
return null; | |
} | |
private void performRemove(ShoppingBasketItem sbItemToRemove, int quantity) | |
throws ShoppingBasketException { | |
if (sbItemToRemove != null) { | |
if (quantity > sbItemToRemove.getQuantity()) { | |
itemsInBasket.remove(sbItemToRemove); | |
} else { | |
sbItemToRemove.reduceQuantityBy(quantity); | |
} | |
} | |
} |