0

I have created a shopping cart application using jsp and servlets.

I am able to add items to shopping cart.

But I want to show the items on "View Shopping Cart" jsp page in following table format.

Item    |   Quantity   |   Price
book1   |       1      |   100
book2   |       2      |   200
Total   |       3      |   300

Also I want updateQuantity() and removeFromCart() functions in ViewCart.jsp page.

How should I do it?

The code for the pages is as follows.


ShoppingCart.java

 package beans;

import java.util.Enumeration;
import java.util.Hashtable;

public class ShoppingCart {

protected Hashtable items=new Hashtable();



public void addItem(String itemId,String desc,float price,int quantity)
{
    String [] item={itemId,desc,Float.toString(price),Integer.toString(quantity)};

    if(items.containsKey(itemId))
    {
        String [] tmpItem=(String[])items.get(itemId);
        int tmpQuant=Integer.parseInt(tmpItem[3]);
        quantity+=tmpQuant;
        tmpItem[3]=Integer.toString(quantity);
    }

    else
    {
        items.put(itemId,item);
    }
}

public void removeItem(String itemId)
{
    if(items.containsKey(itemId))
    {
        items.remove(itemId);
    }
}

public void updateQuantity(String itemId,int quantity)
{
    if(items.contains(itemId))
    {
        String [] tmpItem=(String[])items.get(itemId);
        tmpItem[3]=Integer.toString(quantity);
    }
}

public Enumeration getEnumeration()
{
    return items.elements();
}

public float getCost()
{
    Enumeration enumeration=items.elements();
    String [] tmpItem;
    float totalCost=0.00f;

    while(enumeration.hasMoreElements())
    {
        tmpItem=(String[]) enumeration.nextElement();
        totalCost+=(Integer.parseInt(tmpItem[3]))*(Float.parseFloat(tmpItem[2]));
    }

    return totalCost;
}

public int getNumberOfItems()
{
    Enumeration enumeration=items.elements();
    String [] tmpItem;
    int numberOfItems=0;

    while(enumeration.hasMoreElements())
    {
        tmpItem=(String[])enumeration.nextElement();
        numberOfItems+=Integer.parseInt(tmpItem[3]);
    }

    return numberOfItems;
}

public String[] showCart()
{
    String itemId = null;
    String [] tmpItem = null;
    int tmpId;
    String desc = null;
    float price = 0;
    int quantity = 0;
    String [] item={itemId,desc,Float.toString(price),Integer.toString(quantity)};
    Enumeration enumeration=items.elements();
    while(enumeration.hasMoreElements())
    {
         tmpItem=(String[])enumeration.nextElement();
        tmpId=Integer.parseInt(tmpItem[0]);


    }


   return tmpItem;
}
}

Product.jsp

<%@page import="beans.ShoppingCart"%>
<%@page import="java.util.*" %>
<%@page import="javax.servlet.http.*" %>
<jsp:useBean id="cart" scope="session" class="beans.ShoppingCart" />

<head>
<title>Product</title>  
</head>  

<body>     
   <p>Shopping Cart  Quantity : <%=cart.getNumberOfItems()  Price : <%=cart.getCost() %></p>
   <h2>Book 1 </h2><br>

   <h2> Price :  250</h2><br>

   <form action="Product.jsp" method="POST">
    <input type="submit" name="Submit" value="Add to Cart">
    <input type="hidden" name="id" value="1">
    <input type="hidden" name="desc" value="Book 1">
    <input type="hidden" name="price" value="250">
  </form>

  <h2> BOOK 2</h2><br>

  <h2> Price : 300</h2>><br>    

  <form action="Product.jsp" method="POST">
  <input type="submit" name="Submit" value="Add to Cart">
    <input type="hidden" name="id" value="2">
    <input type="hidden" name="desc" value="Book 2">
    <input type="hidden" name="price" value="300">
  </form>

</body>
</html>
JaredMcAteer
  • 21,688
  • 5
  • 49
  • 65
sourabhgkr
  • 89
  • 4
  • 10
  • It's high time to learn about Javabeans and JSTL. See also http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202, http://stackoverflow.com/tags/javabeans/info and http://stackoverflow.com/tags/jstl/info – BalusC Jan 04 '13 at 16:08
  • Hi,Please tell me that how should I print values form hashtable in table format shown above. – sourabhgkr Jan 05 '13 at 07:44

0 Answers0