Java Reference
In-Depth Information
L ISTING 20.2
Continued
public void updateQuantity(String itemId, int quantity) {
if (items.contains(itemId)) {
String[] tmpItem = (String[])items.get(itemId);
tmpItem[3] = Integer.toString(quantity);
}
}
/**
Get an Enumeration to the list of items in the shopping cart.
*/
public Enumeration getEnumeration() {
return items.elements();
}
/**
Get the total cost of all of the items currently in the
shopping cart.
*/
public float getCost() {
Enumeration enum = items.elements();
String[] tmpItem;
float totalCost = 0.00f;
while (enum.hasMoreElements()) {
tmpItem = (String[])enum.nextElement();
totalCost += (Integer.parseInt(tmpItem[3]) *
Float.parseFloat(tmpItem[2]));
}
return totalCost;
}
/**
Get the total number of items currently in the shopping cart.
*/
public int getNumOfItems() {
Enumeration enum = items.elements();
String[] tmpItem;
int numOfItems = 0;
while (enum.hasMoreElements()) {
tmpItem = (String[])enum.nextElement();
numOfItems += Integer.parseInt(tmpItem[3]);
}
20
return numOfItems;
}
 
Search WWH ::




Custom Search