Search...

Saturday, February 11, 2012

Shopping Cart example


using System.Collections.Generic;
using System.Linq;

public class Products
{
    public Products() { }
    public int ProductId{get;set;}
    public string ProductName{get;set;}
    public string ProductDetails{get;set;}
    public decimal UnitPrice {get;set;}
    public int Quantity { get; set; }

    public List<Products> getProducts()
    {
        List<Products> list = new List<Products>();
        list.Add(new Products { ProductId = 1, ProductName = "Dell Laptop",ProductDetails="Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB",UnitPrice=450000M,Quantity=5 });
        list.Add(new Products { ProductId = 2, ProductName = "HP Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 550000M ,Quantity=4});
        list.Add(new Products { ProductId = 3, ProductName = "HCL Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 350000M ,Quantity=20});
        list.Add(new Products { ProductId = 4, ProductName = "Linovo Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 2GB", UnitPrice = 250000M ,Quantity=30});
        list.Add(new Products { ProductId = 5, ProductName = "Compac Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 300000M ,Quantity=30});
        list.Add(new Products { ProductId = 6, ProductName = "Dell Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 450000M ,Quantity=10});
        list.Add(new Products { ProductId = 7, ProductName = "HP Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 550000M ,Quantity=30});
        list.Add(new Products { ProductId = 8, ProductName = "HCL Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 2GB", UnitPrice = 350000M ,Quantity=20});
        list.Add(new Products { ProductId = 9, ProductName = "Linovo Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 250000M ,Quantity=30});
        list.Add(new Products { ProductId = 10, ProductName = "Compac Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 300000M ,Quantity=60});
        list.Add(new Products { ProductId = 11, ProductName = "Dell Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 350000M ,Quantity=30});
        list.Add(new Products { ProductId = 12, ProductName = "HP Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 550000M ,Quantity=30});
        list.Add(new Products { ProductId = 13, ProductName = "HCL Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 340000M ,Quantity=20});
        list.Add(new Products { ProductId = 14, ProductName = "Linovo Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 350000M ,Quantity=30});
        list.Add(new Products { ProductId = 15, ProductName = "Compac Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 400000M ,Quantity=30});
        list.Add(new Products { ProductId = 16, ProductName = "Dell Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 650000M ,Quantity=30});
        list.Add(new Products { ProductId = 17, ProductName = "HP Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 750000M ,Quantity=1});
        list.Add(new Products { ProductId = 18, ProductName = "HCL Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 450000M ,Quantity=30});
        list.Add(new Products { ProductId = 19, ProductName = "Linovo Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 450000M ,Quantity=30});
        list.Add(new Products { ProductId = 20, ProductName = "Compac Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 370000M ,Quantity=5});
        return list;
    }
}

*******************************************************************************


using System.Linq;
using System.Collections.Generic;

public class ShoppingCard
{
    public ShoppingCard() { }

    public Dictionary<int, Products> items = new Dictionary<int, Products>();

    public void addItems(int productId, int quentity)
    {

        Products productInStore =new Products();

        Products productInCart = productInStore.getProducts().Where(q => q.ProductId == productId).SingleOrDefault();

        if (items.ContainsKey(productInCart.ProductId))
        {
            if (items.TryGetValue(productInCart.ProductId, out productInCart))
                {
                    productInCart.Quantity += quentity;
                }
        }
        else
        {
            productInCart.Quantity = quentity;
            items.Add(productInCart.ProductId, productInCart);
        }
    }

    public void deleteItems(int pid)
    {
        items.Remove(pid);
    }  
   
}
 
********************************************************************************
/*Create a webpage named Default.aspx*/


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Shopping Cart Demo</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div id="header">Shopping Cart Demo</div>
    <div id="content">
    <p><a href="CartItems.aspx" id="cartItems" runat="server" visible="false">View Cart Items</a></p>
    <asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
    <table>
    <tr>
    <th>Product Name</th>
    <th>Product Details</th>
    <th>Unit Price</th>
    <th></th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
        <td><%# Eval("ProductName") %></td>
        <td><%# Eval("ProductDetails")%></td>
        <td><%# "Rs "+Eval("UnitPrice")+" /-"%></td>
        <td><a href="CartItems.aspx?id=<%# Eval("ProductId") %>">Add to cart</a></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    </div>
    <div id="footer"></div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Products products = new Products();
            Repeater1.DataSource = products.getProducts();
            Repeater1.DataBind();
        }

        if (Session["cart"] != null)
        {
            cartItems.Visible = true;
        }
        
    }
}

********************************************************************************
/*Create a web page named CartItems.aspx*/

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CartItems.aspx.cs" Inherits="Scripts_CartItems" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>My Cart Iems</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    
    <div id="header">Shopping Cart Demo</div>
    <div id="content">
    <p><a href="Default.aspx">Want to shop more ?</a></p>
  
    <asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
    <table>
    <tr>
    <th>Product Name</th>
    <th>Product Details</th>
    <th>Unit Price</th>
    <th>Quantity</th>
    <th colspan="2"></th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
        <td><%# Eval("ProductName") %></td>
        <td><%# Eval("ProductDetails")%></td>
        <td><%# "Rs "+Eval("UnitPrice")+" /-"%></td>
        <td><label id="quantity"><%# Eval("Quantity")%></label></td>
        <td><a href="CartItems.aspx?did=<%# Eval("ProductId") %>">Delete item</a></td>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>

    </div>
    <div id="footer"></div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Scripts_CartItems : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            int productId = Convert.ToInt16(Request.QueryString["id"]);
            ShoppingCard shoppingCart = (ShoppingCard)Session["cart"];
            if (shoppingCart == null)
            shoppingCart = new ShoppingCard();
            shoppingCart.addItems(productId, 1);
            Session["cart"] = shoppingCart;
        }
        
        BindCartItems();

        DeleteCartItems();
        
    }

    private void BindCartItems()
    {
        if (Session["cart"] != null)
        {
            List<Products> cartItems = new List<Products>();
            ShoppingCard shoppingCart = (ShoppingCard)Session["cart"];
            foreach (Products data in shoppingCart.items.Values)
            {
                cartItems.Add(data);
            }
            if (cartItems.Count != 0)
            {
                Repeater1.DataSource = cartItems.OrderBy(q=>q.ProductId);
                Repeater1.DataBind();
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }
    }

    private void DeleteCartItems()
    {
        if (Request.QueryString["did"] != null)
        {
            int productId = Convert.ToInt16(Request.QueryString["did"]);
            ShoppingCard shoppingCart = (ShoppingCard)Session["cart"];
            shoppingCart.deleteItems(productId);
            BindCartItems();
        }
    }

}

*******************************************************************************
/*Create style sheet named StyleSheet.css*/

body 
{
   border:1px solid #005c99; 
}

#header
{
background:#0091f3;
height:50px; 
color:#fff;
font-size:28px;
padding:10px;   
}

#content
{
  padding:20px 0 20px 0;
}

#content table
{
 margin:0px auto;
 width:70%;
 border:1px solid #005c99;     
}

#content table th
{
 background:#49a9dc;
 color:#fff;   
}

#content table td
{
 border:1px solid #005c99;     
 text-align:center;  
}

#footer
{
background:#0091f3;
height:30px;    
}

a
{
color:#9a0808;
font-weight:bold;
text-decoration:none;    
}

a:hover
{
color:#005c99;
font-weight:bold; 
text-decoration:underline;    
}






No comments: