コード例 #1
0
  @Override
  public int getTotalValue() {
    int totalCost = 0;

    // Go through the product map and add up the total costs
    for (Map.Entry<String, Integer> iterProductMap : quantities.entrySet()) {
      // get the product cost
      Product product = catalog.getProduct(iterProductMap.getKey());

      if (product != null) {
        // multiply by quantity and add to running total
        totalCost = totalCost + (product.getPrice() * iterProductMap.getValue());
      } else {
        System.out.println(
            "Product Id ["
                + iterProductMap.getKey()
                + "] was not found in the catalog.  Excluding from total cost.");
      }
    }

    return totalCost;
  }
コード例 #2
0
ファイル: Homework1.java プロジェクト: mcgilman/707
  /**
   * Seeds the catalog with test products.
   *
   * @param catalog Catalog to seed
   */
  private static void seedCatalog(Catalog catalog, PropertyChangeListener productListener) {
    for (int i = 0; i < NUM_PRODUCTS; i++) {
      Product product = GeneralFactory.createProduct();
      product.addPropertyChangeListener(productListener);

      // define the product and add to the catalog
      product.setId(IDS[i]);
      product.setName(NAMES[i]);
      product.setDescription(DESCRIPTIONS[i]);
      product.setPrice(PRICES[i]);
      catalog.addProduct(product);
    }
  }