コード例 #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++;
  }