예제 #1
0
  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;
  }
예제 #2
0
  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);
  }
예제 #3
0
  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--;
    }
  }
예제 #4
0
  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++;
  }