Carrinho

1 resposta
L

Fiz akele exemplo de carrinho de compras no livro “Java para web com Servlets, jsp e ejb”, mas ele esta faltando uma coisinha…ele nao soma o valor de todos os produtos que estao na sessao…alguem sabe fazer isso?

public Product getProductDetails(int prod)
	{
		Product product = null;
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection connection = DriverManager.getConnection("jdbc:odbc:database");
			Statement s = connection.createStatement();
			String sql = "select * from produtos" +
				" where cod_produto =" + Integer.toString(prod);
			ResultSet rs = s.executeQuery(sql);
			if(rs.next())
			{
				product = new Product();
				product.cod_produto = rs.getInt(1);
				product.nome_produto = rs.getString(2);
				product.desc_produto = rs.getString(3);
				product.preco_produto = rs.getDouble(4);
			}
			rs.close();
			s.close();
			connection.close();
		}
		catch(SQLException e) {}
		catch(ClassNotFoundException e) {}
		return product;
	}
<%@ page import="com.site.servlet.Product" %>
<%@ page import="com.site.servlet.ShoppingItem" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>

<jsp:useBean id="dbBean" scope="application" class="com.site.servlet.DbBean"/>

<%
Hashtable shoppingCart = (Hashtable) session.getAttribute("shoppingCart");
if(shoppingCart == null)
	shoppingCart = new Hashtable(10);

String action = request.getParameter("action");
if(action!=null && action.equals("addShoppingItem"))
{
	try{
		int prod = Integer.parseInt(request.getParameter("prod"));
		Product product = dbBean.getProductDetails(prod);
		if(product!=null){
			ShoppingItem item = new ShoppingItem();
			item.cod_produto = prod;
			item.quantity = 1;
			item.preco_produto = product.preco_produto;
			item.nome_produto = product.nome_produto;
			item.desc_produto = product.desc_produto;
			
			shoppingCart.remove(Integer.toString(prod));
			shoppingCart.put(Integer.toString(prod), item);
			session.setAttribute("shoppingCart", shoppingCart);
		}
	}
	catch(Exception e){
		out.println("Error adding the selected product to the shopping cart");
	}
}

if(action!=null && action.equals("updateShoppingItem"))
{
	try{
		int prod = Integer.parseInt(request.getParameter("prod"));
		int quantity = Integer.parseInt(request.getParameter("quantity"));
		ShoppingItem item = (ShoppingItem) shoppingCart.get(Integer.toString(prod));
		if(item!=null){
			item.quantity = quantity;
		}
	}
	catch(Exception e){
		out.println("Error updating shopping cart");
	}
}

if(action!=null && action.equals("deleteShoppingItem"))
{
	try{
		int prod = Integer.parseInt(request.getParameter("prod"));
		shoppingCart.remove(Integer.toString(prod));
	}
	catch(Exception e){
		out.println("Error deleting the selected item from the shopping cart");
	}
}
%>

<table width="600" align="center">
        <tr>
          <td></td>
        </tr>
        <tr>
          <td><% %>
              <table width="600" align="center">
                <tr class="style1">
                  <td><span class="style1"><b>Nome</b></span></td>
                  <td><span class="style1"><b>Descrição</b></span></td>
                  <td><span class="style1"><b>Preço</b></span></td>
                  <td><span class="style1"><strong>Quantidade</strong></span></td>
                  <td><span class="style1"><b>SubTotal</b></span></td>
                  <td><span class="style1"><b>Atualizar</b></span></td>
                  <td><span class="style1"><b>Deletar</b></span></td>
                </tr>
                <%
	Enumeration enum = shoppingCart.elements();
	while(enum.hasMoreElements()){
		ShoppingItem item = (ShoppingItem) enum.nextElement();
	%>
                <tr>
                  <td><span class="style1"><%=item.nome_produto%></span></td>
                  <td><span class="style1"><%=item.desc_produto%></span></td>
                  <td><span class="style1"><%=item.preco_produto%></span></td>
                  <form>
                    <input type="hidden" name="action" value="updateShoppingItem">
                    <input type="hidden" name="prod" value="<%=item.cod_produto%>">
                    <td><input name="quantity" type="text" class="FormDiaMes" value="<%=item.quantity%>" size="2"></td>
                    <td><span class="style1"><%=item.quantity * item.preco_produto%></span></td>
                    <td><input type="submit" class="FormBotao" value="Atualizar"></td>
                  </form>
                  <form>
                    <input type="hidden" name="action" value="deleteShoppingItem">
                    <input type="hidden" name="prod" value="<%=item.cod_produto%>">
                    <td><input type="submit" class="FormBotao" value="Deletar"></td>
                  </form>
                </tr>
                <% } %>
                <tr>
                  <td colspan="7"><a href="#" class="style1">Finalizar Carrinho</a></td>
                </tr>
                          </table></td>
        </tr>
      </table>

1 Resposta

X

é so adicionar isso no scriplet:

Enumeration enum = shoppingCart.elements();

while(enum.hasMoreElements()){

ShoppingItem item = (ShoppingItem) enum.nextElement();

total = total +  item.quantity * item.preco_produto;

e adicionar o html no fim:

<input type=“text” name=“total” value="<%=total%>">
<input type=“submit” class=“FormBotao” value=“finalizar”>

adicionar na açao finalizar:

if(action.equals(finalizar))

{

//redireciona com a session pra pagina de pagamento;

}

[/b]

Criado 28 de junho de 2006
Ultima resposta 14 de jul. de 2006
Respostas 1
Participantes 2