public synchronized int getNumberOfItems() { numberOfItems = 0; for (Iterator i = getItems().iterator(); i.hasNext(); ) { ShoppingCartItem item = (ShoppingCartItem) i.next(); numberOfItems += item.getQuantity(); System.out.println("number of items is " + numberOfItems); } return numberOfItems; }
public synchronized double getTotal() { double amount = 0.0; for (Iterator i = getItems().iterator(); i.hasNext(); ) { ShoppingCartItem item = (ShoppingCartItem) i.next(); BookDetails bookDetails = (BookDetails) item.getItem(); amount += (item.getQuantity() * bookDetails.getPrice()); } return roundOff(amount); }
public synchronized void remove(String bookId) { if (items.containsKey(bookId)) { ShoppingCartItem scitem = (ShoppingCartItem) items.get(bookId); scitem.decrementQuantity(); if (scitem.getQuantity() <= 0) { items.remove(bookId); } numberOfItems--; } }
public synchronized void add(String bookId, BookDetails book) { if (items.containsKey(bookId)) { ShoppingCartItem scitem = (ShoppingCartItem) items.get(bookId); scitem.incrementQuantity(); System.out.println("in add, quantity is " + scitem.getQuantity()); } else { ShoppingCartItem newItem = new ShoppingCartItem(book); items.put(bookId, newItem); System.out.println("in add, quantity is " + newItem.getQuantity()); } // numberOfItems++; }