Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(a) Write the method getTotal() which returns the sum of the prices for all Item

ID: 3584483 • Letter: #

Question

(a) Write the method getTotal() which returns the sum of the prices for all Items present in the ShoppingCart

Explanation / Answer

package cart; import entity.Product; import java.util.*; /** * * @author tgiunipero */ public class ShoppingCart { List items; int numberOfItems; double total; public ShoppingCart() { items = new ArrayList(); numberOfItems = 0; total = 0; } /** 031. * Adds a ShoppingCartItem to the ShoppingCart's * items list. If item of the specified product * already exists in shopping cart list, the quantity of that item is * incremented. * * @param product the Product that defines the type of shopping cart item * @see ShoppingCartItem */ public synchronized void addItem(Product product) { boolean newItem = true; for (ShoppingCartItem scItem : items) { if (scItem.getProduct().getId() == product.getId()) { newItem = false; scItem.incrementQuantity(); } } if (newItem) { ShoppingCartItem scItem = new ShoppingCartItem(product); items.add(scItem); } } /** * Updates the ShoppingCartItem of the specified * product to the specified quantity. If '0' * is the given quantity, the ShoppingCartItem is removed * from the ShoppingCart's items list. * * @param product the Product that defines the type of shopping cart item * @param quantity the number which the ShoppingCartItem is updated to * @see ShoppingCartItem */ public synchronized void update(Product product, String quantity) { short qty = -1; // cast quantity as short qty = Short.parseShort(quantity); if (qty >= 0) { ShoppingCartItem item = null; for (ShoppingCartItem scItem : items) { if (scItem.getProduct().getId() == product.getId()) { if (qty != 0) { // set item quantity to new value scItem.setQuantity(qty); } else { // if quantity equals 0, save item and break item = scItem; break; } } } if (item != null) { // remove from cart items.remove(item); } } } /** * Returns the list of ShoppingCartItems. * * @return the items list * @see ShoppingCartItem */ public synchronized List getItems() { return items; } /** * Returns the sum of quantities for all items maintained in shopping cart * items list. * * @return the number of items in shopping cart * @see ShoppingCartItem */ public synchronized int getNumberOfItems() { numberOfItems = 0; for (ShoppingCartItem scItem : items) { numberOfItems += scItem.getQuantity(); } return numberOfItems; } /** * Returns the sum of the product price multiplied by the quantity for all * items in shopping cart list. This is the total cost excluding the surcharge. * * @return the cost of all items times their quantities * @see ShoppingCartItem */ public synchronized double getSubtotal() { double amount = 0; for (ShoppingCartItem scItem : items) { Product product = (Product) scItem.getProduct(); amount += (scItem.getQuantity() * product.getPrice().doubleValue()); } return amount; } /** * Calculates the total cost of the order. This method adds the subtotal to * the designated surcharge and sets the total instance variable * with the result. * * @param surcharge the designated surcharge for all orders * @see ShoppingCartItem */ public synchronized void calculateTotal(String surcharge) { double amount = 0; // cast surcharge as double double s = Double.parseDouble(surcharge); amount = this.getSubtotal(); amount += s; total = amount; } /** * Returns the total cost of the order for the given * ShoppingCart instance. * * @return the cost of all items times their quantities plus surcharge */ public synchronized double getTotal() { return total; } /** * Empties the shopping cart. All items are removed from the shopping cart * items list, numberOfItems and * total are reset to '0'. * * @see ShoppingCartItem */ public synchronized void clear() { items.clear(); numberOfItems = 0; total = 0; } }