示例#1
0
  public static void main(String args[]) throws Exception {

    shoppingCart.addItem("Bread");
    shoppingCart.addItem("Milk");
    Thread.sleep(15000);
    shoppingCart.addItem("Tea");

    System.out.println("Contents of your cart are:");
    Collection<String> items = shoppingCart.getItems();
    for (String item : items) {
      System.out.println(item);
    }

    shoppingCart.finished();
  }
  /*
   * Processes the customer order by forwarding the order details to the supplier.
   */
  public String submit() {
    ocs = new OrderConfirmations();

    FacesContext context = FacesContext.getCurrentInstance();
    ELContext elContext = context.getCurrentInstance().getELContext();
    ValueExpression ve =
        context
            .getApplication()
            .getExpressionFactory()
            .createValueExpression(elContext, "#{CoffeeBreakBean}", Object.class);
    CoffeeBreakBean cbBean = (CoffeeBreakBean) ve.getValue(elContext);

    RetailPriceList rpl = cbBean.getRetailPriceList();
    ShoppingCart cart = cbBean.getCart();

    ConfirmationBean confirmation = null;
    String orderId = CCNumber;

    AddressBean address = new AddressBean();
    address.setStreet(street);
    address.setCity(city);
    address.setState(state);
    address.setZip(zip);

    CustomerBean customer = new CustomerBean();
    customer.setFirstName(firstName);
    customer.setLastName(lastName);
    customer.setPhoneNumber("(" + areaCode + ") " + phoneNumber);
    customer.setEmailAddress(email);

    for (Iterator d = rpl.getSuppliers().iterator(); d.hasNext(); ) {
      String supplier = (String) d.next();
      logger.info(supplier);

      List<LineItemBean> lis = new ArrayList<LineItemBean>();
      BigDecimal price = new BigDecimal("0.00");
      BigDecimal total = new BigDecimal("0.00");

      for (Iterator c = cart.getItems().iterator(); c.hasNext(); ) {
        ShoppingCartItem sci = (ShoppingCartItem) c.next();

        if ((sci.getItem().getSupplier()).equals(supplier)
            && ((sci.getPounds()).floatValue() > 0)) {
          price = sci.getItem().getWholesalePricePerPound().multiply(sci.getPounds());
          total = total.add(price);

          LineItemBean li = new LineItemBean();
          li.setCoffeeName(sci.getItem().getCoffeeName());
          li.setPounds(sci.getPounds());
          li.setPrice(sci.getItem().getWholesalePricePerPound());
          lis.add(li);
        }
      }

      if (!lis.isEmpty()) {
        logger.info("creating OrderBean");

        OrderBean order = new OrderBean();
        order.setAddress(address);
        order.setCustomer(customer);
        order.setId(orderId);

        int i = 0;

        for (Iterator<LineItemBean> j = lis.iterator(); j.hasNext(); ) {
          order.getLineItems().add(j.next());
          i++;
        }

        order.setTotal(total);

        String SAAJOrderURL = URLHelper.getSaajURL() + "/orderCoffee";

        if (supplier.equals(SAAJOrderURL)) {
          logger.info("creating OrderRequest for" + SAAJOrderURL);

          OrderRequest or = new OrderRequest(SAAJOrderURL);
          confirmation = or.placeOrder(order);
        } else {
          logger.info("creating OrderCaller");

          OrderCaller ocaller = new OrderCaller(supplier);
          confirmation = ocaller.placeOrder(order);
        }

        logger.info("crating OrderConfirmation");

        OrderConfirmation oc = new OrderConfirmation(order, confirmation);
        ocs.add(oc);
      }
    }

    logger.info("returning submit");

    return "submit";
  }
示例#3
0
 @Test
 public void testClearCart() throws Exception {
   cart.clearCart();
   assertEquals(0, cart.getItems().size());
 }
示例#4
0
 @Test
 public void testRemoveItem() throws Exception {
   cart.removeItem("9781430216407");
   assertEquals(1, cart.getItems().size());
   assertNull(cart.getItems().get("9781430216407"));
 }
示例#5
0
 @Test
 public void testAddItem() throws Exception {
   cart.addItem("9781430224693");
   assertEquals(3, cart.getItems().size());
   assertEquals(1, (Object) cart.getItems().get("9781430224693"));
 }
示例#6
0
 public void checkout(ShoppingCart cart) throws IOException {
   writer.write(new Date() + "\t" + cart.getItems() + "\r\n");
   writer.flush();
 }