Skip to content

Commit

Permalink
Adding Product to users cart
Browse files Browse the repository at this point in the history
  • Loading branch information
shashirajraja committed Nov 20, 2019
1 parent 139a0bd commit 95c74fe
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 145 deletions.
2 changes: 1 addition & 1 deletion shopping-cart/WebContent/adminViewProduct.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
String password = (String)session.getAttribute("password");
String userType = (String)session.getAttribute("usertype");
if(!userType.equals("admin") || userType==null || userName == null || password==null){
if(userType==null || userName == null || password==null || !userType.equals("admin")){
response.sendRedirect("loginFirst.jsp");
}
Expand Down
39 changes: 39 additions & 0 deletions shopping-cart/WebContent/css/changes.css
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,42 @@ button:hover{

}

/* Shopping Cart notification Batch */

*.icon-blue {
color: #0088cc;
}

*.icon-grey {
color: grey;
}

i {
width: 70px;
height: 50px;
text-align: center;
vertical-align: middle;
position: relative;
}

.badge:after {
content: attr(data-count);
position: absolute;
background: #ff6600;
height: 2rem;
top: 0.15rem;
right: 0.15rem;
width: 2rem;
text-align: center;
line-height: 2rem;
font-size: 1rem;
border-radius: 50%;
color: white;
border: 1px solid #ff6600;
font-family: sans-serif;
font-weight: bold;
}
i.fa::before{
font-size: 3rem !important;
}

4 changes: 2 additions & 2 deletions shopping-cart/WebContent/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ <h1>Shopping Center</h1>
<li><a href="laptop.jsp">Laptops</a></li>
</ul>
</li>
<li><a href="#"><span class="glyphicon glyphicon-search"></span></a></li>
</ul>
<!-- <li><a href="#"><span class="glyphicon glyphicon-search"></span></a></li>
--> </ul>
</div>
</div>
</nav>
Expand Down
5 changes: 4 additions & 1 deletion shopping-cart/WebContent/updateProduct.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
else if(uname == null || pwd==null){
response.sendRedirect("loginFirst.jsp");
}
Expand All @@ -34,6 +35,8 @@
if(prodid != null && product !=null){
//out.println("Product ID: "+prodid);
%>

<%@ include file="adminHeader.html" %>
Expand All @@ -56,7 +59,7 @@
<div style="margin: 5px">

<form action="./UpdateProductSrv" method="post" >
<input type="hidden" name="pid" value=<%=product.getProdId() %>">
<input type="hidden" name="pid" value="<%=product.getProdId() %>">
<table border="0">
<tr><td>Product Name: </td><td><input type="text" name="name"style="font-size: 15px;font-weight: normal; width:80%" value="<%=product.getProdName() %>" required></td></tr>
<tr><td><br></td><td><br></td></tr>
Expand Down
52 changes: 40 additions & 12 deletions shopping-cart/WebContent/userHeader.jsp
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.shashi.dao.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<title>Shoping Center</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="css/changes.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="css/changes.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>

<%
int notf = new CartDaoImpl().getCartCount((String)session.getAttribute("username"));
%>
<!--Company Header Starting -->
<div class="jumbotron text-center">
<h1>Shopping Center</h1>
Expand All @@ -28,35 +36,55 @@

<!-- Starting Navigation Bar -->
<nav class="navbar navbar-default navbar-fixed-top">

<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html"><span class="glyphicon glyphicon-home">&nbsp;</span>Shopping Center</a>
</div>
<a class="navbar-brand" href="index.html"><span class="glyphicon glyphicon-home">&nbsp;</span>Shopping Center</a>
</div>

<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav navbar-right">
<li><a href="./LogoutSrv">Logout</a></li>
<!-- <li><a href="./ProfileSrv">Profile</a></li>
--> <li><a href="cart.jsp"> <span class="glyphicon glyphicon-shopping-cart"></span>&nbsp;Cart</a></li>
<!-- <li><a href="./ProfileSrv">Profile</a></li> -->
<%
if(notf == 0) {
%>

<li> <a href="cartDetails.jsp" style="margin:0px;padding:0px;" id="mycart"><i class="fa fa-shopping-cart fa-3x icon-white" style="background-color:#333;margin:0px;padding:0px; margin-top:5px;" >
</i>Cart</a></li>

<%
}
else{
%>
<li> <a href="cartDetails.jsp" style="margin:0px;padding:0px;" id="mycart"><i data-count="<%=notf %>" class="fa fa-shopping-cart fa-3x icon-white badge" style="background-color:#333;margin:0px;padding:0px; margin-top:5px;" >
</i>Cart</a></li>
<%
}
%>

<li><a href="./LogoutSrv">Logout</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">MORE
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="mobile.jsp">Mobiles</a></li>
<li><a href="tv.jsp">Tvs</a></li>
<li><a href="laptop.jsp">Laptops</a></li>
<li><a href="userHome.jsp">Mobiles</a></li>
<li><a href="userHome.jsp">Tvs</a></li>
<li><a href="userHome.jsp">Laptops</a></li>
</ul>
</li>
<li><a href="#"><span class="glyphicon glyphicon-search"></span></a></li>
</ul>
<!-- <li><a href="#"><span class="glyphicon glyphicon-search"></span></a></li>
--> </ul>
</div>
</div>
</nav>
<!-- End of Navigation Bar -->


</body>
</html>
8 changes: 6 additions & 2 deletions shopping-cart/WebContent/userHome.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="css/changes.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
Expand All @@ -24,12 +25,15 @@
response.sendRedirect("loginFirst.jsp");
}
%>



<%@ include file="userHeader.html" %>
<%@ include file="userHeader.jsp" %>

<!-- <script>document.getElementById('mycart').innerHTML='<i data-count="20" class="fa fa-shopping-cart fa-3x icon-white badge" style="background-color:#333;margin:0px;padding:0px; margin-top:5px;"></i>'</script>
-->
<!-- Start of Product Items List -->
<div class="products">
<div class="row text-center">
Expand All @@ -55,7 +59,7 @@
<p class="price">Rs <%=product.getProdPrice() %> </p>
<form method="post">
<button type="submit" formaction="./AddtoCart?uid=<%=userName %>&pid=<%=product.getProdId() %>">Add to Cart</button>&nbsp;&nbsp;&nbsp;
<button type="submit" formaction="#">Buy Now</button>
<button type="submit" formaction="./BuyNow?uid=<%=userName %>&pid=<%=product.getProdId() %>">Buy Now</button>
</form>
</div>
</div>
Expand Down
14 changes: 14 additions & 0 deletions shopping-cart/src/com/shashi/dao/CartDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.shashi.dao;

import java.util.List;

import com.shashi.beans.CartBean;

public interface CartDao{

public String addProductToCart(String userId, String prodId);

public List<CartBean> getAllCartItems(String userId);

public int getCartCount(String userId);
}
160 changes: 160 additions & 0 deletions shopping-cart/src/com/shashi/dao/CartDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package com.shashi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.shashi.beans.CartBean;
import com.shashi.utility.DBUtil;

public class CartDaoImpl implements CartDao{

@Override
public String addProductToCart(String userId, String prodId) {
String status= "Failed to Add into Cart";


Connection con = DBUtil.provideConnection();

PreparedStatement ps = null;
PreparedStatement ps2 = null;
ResultSet rs = null;

try {

ps = con.prepareStatement("select * from usercart where username=? and prodid=?");

ps.setString(1, userId);
ps.setString(2, prodId);

rs = ps.executeQuery();

if(rs.next()) {

int prodQuantity = rs.getInt("quantity");

prodQuantity += 1;

ps2 = con.prepareStatement("update usercart set quantity=? where username=? and prodid=?");

ps2.setInt(1, prodQuantity);

ps2.setString(2, userId);

ps2.setString(3, prodId);

int k = ps2.executeUpdate();

if(k>0)
status = "Product Successfully Added to Cart!";

}
else {

ps2 = con.prepareStatement("insert into usercart values(?,?,?)");

ps2.setString(1, userId);

ps2.setString(2, prodId);

ps2.setInt(3, 1);

int k = ps2.executeUpdate();

if(k>0)
status = "Product Successfully Added to Cart!";

}

} catch (SQLException e) {
status = "Error: "+ e.getMessage();
e.printStackTrace();
}


DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
DBUtil.closeConnection(rs);
DBUtil.closeConnection(ps2);

return status;
}

@Override
public List<CartBean> getAllCartItems(String userId) {
List<CartBean> items = new ArrayList<CartBean>();

Connection con = DBUtil.provideConnection();

PreparedStatement ps = null;
ResultSet rs = null;

try {

ps = con.prepareStatement("select * from usercart where username=?");

ps.setString(1, userId);

rs = ps.executeQuery();

while(rs.next()) {
CartBean cart = new CartBean();

cart.setUserId(rs.getString("username"));
cart.setProdId(rs.getString("prodid"));
cart.setQuantity(Integer.parseInt(rs.getString("quantity")));

items.add(cart);

}


} catch (SQLException e) {

e.printStackTrace();
}


DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
DBUtil.closeConnection(rs);


return items;
}

@Override
public int getCartCount(String userId) {
int count = 0;

Connection con = DBUtil.provideConnection();

PreparedStatement ps = null;

ResultSet rs = null;

try {
ps = con.prepareStatement("select sum(quantity) from usercart where username=?");

ps.setString(1, userId);

rs = ps.executeQuery();

if(rs.next())
count = rs.getInt(1);

} catch (SQLException e) {

e.printStackTrace();
}

DBUtil.closeConnection(con);
DBUtil.closeConnection(ps);
DBUtil.closeConnection(rs);

return count;
}
}
Loading

0 comments on commit 95c74fe

Please sign in to comment.