public int compareTo(Object obj) { if (obj == null) { return -1; } ShoppingCart shoppingCart = (ShoppingCart) obj; String pk = shoppingCart.getPrimaryKey(); return getPrimaryKey().compareTo(pk); }
public String execute() { // Get the index of the item to remove int itemIndex = Integer.parseInt(request.getParameter("item")); HttpSession session = request.getSession(); // Get the cart ShoppingCart cart = (ShoppingCart) session.getAttribute("ShoppingCart"); cart.removeItem(itemIndex); return SUCCESS; }
public static void main(String[] args) { String directory = "C:\\Users\\.file\\Desktop\\"; String cartId = null; ShoppingCart cart = null; System.out.println("Enter the ID of the cart file to create and serialise it or q to exit."); try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) { cartId = in.readLine(); if (cartId.equals("q")) { System.exit(0); } cart = new ShoppingCart(new Integer(cartId).intValue()); } catch (IOException | NumberFormatException e) { System.out.println("Exception: " + e); System.exit(-1); } cart.addItem(new Item(101, "Duke Plastic Circular Flying Disc", 10.95)); cart.addItem(new Item(123, "Duke Soccer Pro Soccer Ball", 29.95)); cart.addItem(new Item(45, "Duke \"The Edge\" Tennis Balls - 12-Ball Bag", 17.49)); System.out.println("Shopping cart " + cartId + " contains " + cart.getCartSize() + " items."); System.out.println( "Shopping cart total: " + NumberFormat.getCurrencyInstance().format(cart.getCartTotal())); String cartFile = directory + "cart" + cart.getCartId() + ".ser"; try (FileOutputStream fos = new FileOutputStream(cartFile); ObjectOutputStream oos = new ObjectOutputStream(fos)) { oos.writeObject(cart); } catch (IOException e) { System.out.println("Exception serializing " + cartFile + ": " + e); System.exit(-1); } System.out.println("Successfully serialized shopping cart with ID: " + cart.getCartId()); }
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(); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); // Get client session String email = request.getParameter("email"); String password = request.getParameter("password"); if (!validUser(request, email, password)) { session.setAttribute("login", false); response.sendRedirect("/project2_10/login.jsp"); } else { session.setAttribute("login", true); session = request.getSession(); session.setAttribute("user.login", email); ShoppingCart.initCart(request, response); try { String target = (String) session.getAttribute("user.dest"); // retrieve address if user goes to a page w/o logging in if (target != null) { session.removeAttribute("user.dest"); // redirect to page the user was originally trying to go to response.sendRedirect(target); return; } } catch (Exception ignored) { } // Couldn't redirect to the target. Redirect to the site's homepage. response.sendRedirect("/project2_10/"); } }
public static void main(String ar[]) { ShoppingCart cart = new ShoppingCart(); Item item1 = new Item("C12", 2256); Item item2 = new Item("B22", 1999); Item item3 = new Item("DX2", 999); cart.addItem(item1); cart.addItem(item2); cart.addItem(item3); PaymentStrategy creditCardPayment = new CreditCardPayment("*****@*****.**", "1111111111111111", 111, "11/11/1111"); cart.pay(creditCardPayment); PaymentStrategy paypalPayment = new PaypalPayment("*****@*****.**", "12423"); cart.pay(paypalPayment); }
public boolean equals(Object obj) { if (obj == null) { return false; } ShoppingCart shoppingCart = null; try { shoppingCart = (ShoppingCart) obj; } catch (ClassCastException cce) { return false; } String pk = shoppingCart.getPrimaryKey(); if (getPrimaryKey().equals(pk)) { return true; } else { return false; } }
@Override public String toString() { return "LineItem{" + "quantity=" + quantity + ", shoppingCartId=" + shoppingCart.getUuid() + // ", order=" + order + ", product=" + product + ", price=" + price + '}'; }
public void checkout(ShoppingCart cart) throws IOException { writer.write(new Date() + "\t" + cart.getItems() + "\r\n"); writer.flush(); }
public static void main(String[] args) { // Here we can pass any implementation of PayAmount ShoppingCart cart = new ShoppingCart(new PayWithCreditCard()); cart.payAmount(200); }
/* * 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"; }
@Test public void testClearCart() throws Exception { cart.clearCart(); assertEquals(0, cart.getItems().size()); }
@Test public void testRemoveItem() throws Exception { cart.removeItem("9781430216407"); assertEquals(1, cart.getItems().size()); assertNull(cart.getItems().get("9781430216407")); }
@Test public void testAddItem() throws Exception { cart.addItem("9781430224693"); assertEquals(3, cart.getItems().size()); assertEquals(1, (Object) cart.getItems().get("9781430224693")); }
@Before public void setUp() throws Exception { cart = new ShoppingCart(); cart.addItem("9781430216407"); cart.addItem("9781430219088"); }
public String addToCart() { cart.addProduct(product, saleSize, 1); return "/shopping/shoppingcart.xhtml"; }
public void sessionDestroyed(HttpSessionEvent event) { HttpSession session = event.getSession(); ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingCart"); if (cart == null) { Debug.logInfo("No cart to save, doing nothing.", module); return; } String delegatorName = (String) session.getAttribute("delegatorName"); Delegator delegator = null; if (UtilValidate.isNotEmpty(delegatorName)) { delegator = DelegatorFactory.getDelegator(delegatorName); } if (delegator == null) { Debug.logError( "Could not find delegator with delegatorName in session, not saving abandoned cart info.", module); return; } boolean beganTransaction = false; try { beganTransaction = TransactionUtil.begin(); GenericValue visit = VisitHandler.getVisit(session); if (visit == null) { Debug.logError("Could not get the current visit, not saving abandoned cart info.", module); return; } Debug.logInfo("Saving abandoned cart", module); Iterator cartItems = cart.iterator(); int seqId = 1; while (cartItems.hasNext()) { ShoppingCartItem cartItem = (ShoppingCartItem) cartItems.next(); GenericValue cartAbandonedLine = delegator.makeValue("CartAbandonedLine"); cartAbandonedLine.set("visitId", visit.get("visitId")); cartAbandonedLine.set("cartAbandonedLineSeqId", (Integer.valueOf(seqId)).toString()); cartAbandonedLine.set("productId", cartItem.getProductId()); cartAbandonedLine.set("prodCatalogId", cartItem.getProdCatalogId()); cartAbandonedLine.set("quantity", cartItem.getQuantity()); cartAbandonedLine.set("reservStart", cartItem.getReservStart()); cartAbandonedLine.set("reservLength", cartItem.getReservLength()); cartAbandonedLine.set("reservPersons", cartItem.getReservPersons()); cartAbandonedLine.set("unitPrice", cartItem.getBasePrice()); cartAbandonedLine.set("reserv2ndPPPerc", cartItem.getReserv2ndPPPerc()); cartAbandonedLine.set("reservNthPPPerc", cartItem.getReservNthPPPerc()); if (cartItem.getConfigWrapper() != null) { cartAbandonedLine.set("configId", cartItem.getConfigWrapper().getConfigId()); } cartAbandonedLine.set("totalWithAdjustments", cartItem.getItemSubTotal()); // not doing pre-reservations now, so this is always N cartAbandonedLine.set("wasReserved", "N"); cartAbandonedLine.create(); seqId++; } } catch (GenericEntityException e) { try { // only rollback the transaction if we started one... TransactionUtil.rollback(beganTransaction, "Error saving abandoned cart info", e); } catch (GenericEntityException e2) { Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module); } Debug.logError( e, "An entity engine error occurred while saving abandoned cart information", module); } finally { // only commit the transaction if we started one... this will throw an exception if it fails try { TransactionUtil.commit(beganTransaction); } catch (GenericEntityException e) { Debug.logError( e, "Could not commit transaction for entity engine error occurred while saving abandoned cart information", module); } } }
@Test public void testCreateEmptyShoppingCart() { ShoppingCart cart = new ShoppingCart(); Assert.assertEquals(0, cart.getProductCount()); }