private void consolidateInvoices() { for (Invoice i : invoices) { for (LineItem l : i.getInvoice()) { String code = l.getProduct().getCode(); int quant = l.getQuantity(); double total = l.getTotal(); double price = l.getProduct().getPrice(); double[] sale = new double[3]; if (salesOrder.containsKey(code)) { sale = salesOrder.get(code); sale[0] += quant; sale[1] += total; } else { sale[0] = quant; sale[1] = total; } sale[2] = 0; salesOrder.put(code, sale); } } Iterator<Map.Entry<String, double[]>> entries = salesOrder.entrySet().iterator(); while (entries.hasNext()) { Map.Entry<String, double[]> entry = entries.next(); String code = entry.getKey(); double[] sale = entry.getValue(); sale[2] = sale[1] / sale[0]; salesOrder.put(code, sale); } }
public void removeItem(LineItem item) { String code = item.getProduct().getCode(); for (int i = 0; i < items.size(); i++) { LineItem lineItem = items.get(i); if (lineItem.getProduct().getCode().equals(code)) { items.remove(i); return; } } }
public void addItem(LineItem item) { // Only add the item if it's not yet in the cart String code = item.getProduct().getCode(); for (int i = 0; i < items.size(); i++) { LineItem lineItem = items.get(i); if (lineItem.getProduct().getCode().equals(code)) { return; } } items.add(item); }
/** * Adds a line item into the cart if it's not already there. Otherwise, the quantity will be * increased. * * @param lineItem The line item to be added to the cart */ public void addItem(LineItem lineItem) { String code = lineItem.getProduct().getCode(); int quantity = lineItem.getQuantity(); for (LineItem i : items) { if (i.getProduct().getCode().equals(code)) { // already exists i.setQuantity(quantity); return; } } items.add(lineItem); }