Advertisement
Guest User

Carrinho.jsp

a guest
Jan 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. <%@page import="java.util.ArrayList"%>
  2. <%@page import="modelo.Produto"%>
  3. <%@page import="modelo.Itemcarrinho"%>
  4. <%@page import="dao.ProdutoDAO"%>
  5. <%@page import="modelo.Carrinho"%>
  6. <%@include file="cabecalho.jsp"%>
  7.  
  8.  
  9. <% if (session.getAttribute("usuario") == null) {
  10. response.sendRedirect("login.jsp");
  11. } else {
  12.  
  13. Carrinho carrinho;
  14. ProdutoDAO pdao = new ProdutoDAO();
  15.  
  16. if (session.getAttribute("carrinho") == null) {
  17. carrinho = new Carrinho();
  18. } else {
  19.  
  20. carrinho = (Carrinho) session.getAttribute("carrinho");
  21.  
  22. if (request.getParameter("codigo") != null) {
  23.  
  24. //EXCLUIR PRODUTO DO CARRINHO
  25. for (Itemcarrinho teste : carrinho.getItens()) {
  26. if (teste.getProduto().getCodigo() == Integer.parseInt(request.getParameter("codigo"))) {
  27. carrinho.getItens().remove(teste);
  28. Double total = (carrinho.getTotal() - ((teste.getProduto().getPreco().doubleValue()) * (teste.getQuantidade())));
  29. carrinho.setTotal(total);
  30. break;
  31. }
  32. }
  33. }
  34. }
  35.  
  36. if (request.getMethod().equals("POST")) {
  37.  
  38. Integer qtd = Integer.parseInt(request.getParameter("txtQuantidade"));
  39. Itemcarrinho item = new Itemcarrinho();
  40. Produto produto = pdao.buscarPorChavePrimaria(Integer.parseInt(request.getParameter("txtCodigo")));
  41.  
  42. item.setQuantidade(qtd);
  43. item.setProduto(produto);
  44.  
  45. //VER SE A LISTA JA EXISTE
  46. if (carrinho.getItens() == null) {
  47. List<Itemcarrinho> itens = new ArrayList<Itemcarrinho>();
  48. itens.add(item);
  49. carrinho.setItens(itens);
  50. carrinho.setTotal(qtd * produto.getPreco().doubleValue());
  51. } else {
  52. carrinho.getItens().add(item);
  53. Double total = (carrinho.getTotal() + (qtd * produto.getPreco().doubleValue()));
  54. carrinho.setTotal(total);
  55. }
  56. session.setAttribute("carrinho", carrinho);
  57. }
  58.  
  59. if (carrinho.getItens() != null) {
  60. for (Itemcarrinho item : carrinho.getItens()) {
  61. Double precototalunidade = (item.getProduto().getPreco().doubleValue() * item.getQuantidade());
  62.  
  63.  
  64. %>
  65.  
  66.  
  67. <img src="../fotos/<%=item.getProduto().getImagem1()%>"/>
  68. <ul>
  69. <li><p>Nome : <%=item.getProduto().getTitulo()%> </p></li>
  70. <li><p>Qtd : <%=item.getQuantidade()%></p></li>
  71. <li><p>Preço : $<%=item.getProduto().getPreco()%></p></li>
  72. <li><p>Preço total : $<%=precototalunidade%></p></li>
  73. <li><span><%=item.getProduto().getDescricao()%></span></li>
  74. </ul>
  75. <a href="CarrinhoLive.jsp?codigo=<%=item.getProduto().getCodigo()%>">Excluir</a>
  76. --------------
  77. <h1>Price total : $<%=carrinho.getTotal()%></h1>
  78. <a class="order" href="finalizar.jsp">Finalizar Compra</a>
  79.  
  80. <%
  81. }
  82. }
  83. }
  84. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement