Java Reference
In-Depth Information
L ISTING 20.2
Continued
desc - a text description of the item
price - the unit price for this item
quantity - number of this item to insert into the
shopping cart
*/
public void addItem(String itemId,
String desc,
float price,
int quantity) {
String[] item = {itemId, desc, Float.toString(price),
Integer.toString(quantity)};
if (items.containsKey(itemId)) {
String[] tmpItem = (String[])items.get(itemId);
int tmpQuant = Integer.parseInt(tmpItem[3]);
quantity += tmpQuant;
tmpItem[3] = Integer.toString(quantity);
}
else {
items.put(itemId, item);
}
}
/**
Remove an item from the shopping cart.
*
attributes
itemId - the unique key associated with the item to be
removed
*/
public void removeItem(String itemId) {
if (items.containsKey(itemId)) {
items.remove(itemId);
}
}
/**
Change the quantity of a specific item in the shopping cart.
The item must have previously been added to perform this
function.
*
attributes
itemId - unique key for the item to be updated
quantity - the new quantity to be stored in the shopping
cart
*/
Search WWH ::




Custom Search