Jsp Tutorial Home

Jsp Home

JSP Resources

Community

Site

Shopping Cart in JSP-1


Now we will be using all the concepts for the JSP learnt so far to implement in the shopping cart. We have a product page where user select the product and add it to cart. Once user has product in the cart he can see the basket page to view the products.


Example :-


ProductUtilities.java


ProductUtilities is the class which will have the hardcoded values for products and supplied the products for adding it to basket and also to display the products to user for choice.









package com.visualbuilder.shopping;


import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;


public class ProductUtilities {





public static HashMap getProductsAsMap() {

    HashMap<String, ProductBean> products= new HashMap<String, ProductBean>();

    products.put("P1", new ProductBean("P1","Flower",10));

    products.put("P2", new ProductBean("P2","Chair",200));

    products.put("P3", new ProductBean("P3","Table",400));

    products.put("P4", new ProductBean("P4","Microwave",400));

    return products;

}

public static List getProductsAsList() {

    List<ProductBean> products= new ArrayList<ProductBean>();

    products.add(new ProductBean("P1","Flower",10));

    products.add(new ProductBean("P2","Chair",200));

    products.add( new ProductBean("P3","Table",400));

    products.add( new ProductBean("P4","Microwave",400));

  return products;

}



}



ProductBean.java


This is the actual bean for the product. All product has code, name, price and quantity as the properties.








package com.visualbuilder.shopping;

public class ProductBean {

String prodCode;

String prodName;

double price;

int qty=1;

    public int getQty() {

        return qty;

        }

    public void setQty(int qty) {

        this.qty = qty;

        }

    public ProductBean(){

        this.prodCode="";

        this.prodName="";

        }

    public ProductBean(String code, String name, double price){

        this.prodCode=code;

        this.prodName=name;

        this.price=price;

        }

    public String getProdCode() {

        return prodCode;

        }

    public void setProdCode(String prodCode) {

        this.prodCode = prodCode;

        }

    public String getProdName() {

        return prodName;

        }

    public void setProdName(String prodName) {

        this.prodName = prodName;

        }

    public double getPrice() {

                return price;

    }

    public void setPrice(double price) {

        this.price = price;

        }

    }



 


 

                    

Copyright © 2012 VisualBuilder. All rights reserved