Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Items in Shopping Cart

Status
Not open for further replies.

witchblade

Programmer
Aug 1, 2001
50
US
Does anyone have a good example of how I should go about adding up items in a shopping cart that I built? I am at the last point in my project, and I just can't get it to work.

Also, I am using Sessions in the project, and heard that I should not use Cookies if I am using Session?????

[sadeyes]
 
I would recommend creating a Vector as a session variable and appending a new Order line items(OLI) to that vector when a non unique OLI is chosen by the client. This would allow you the ability to iterate through a shopping cart nicely. The API is very descriptive on how to use vectors:

HttpSession session = req.getSession(true);//creates session if it doesnt exist already
session.putValue("ShopCart", new Vector());//creates vector

//if OLI doesnt exist, check by looping through the Vector
make sure you type cast the element with the object class..
then;

OLI addMe = new OLI (productName, OEM, cost, quantity, shippingCost);
ShopCartHere.addElement(addMe);

// looping through the vector can be done to iterate or to see if an OLI already exists

for (int i = 0; i < ShopCartHere.size(); i++){
OLI A = (OLI) ShopCartHere.elementAt(i);
String OEM = req.getParameter(&quot;OEM&quot;);
if (A.getOEM().equals(OEM)){
String Tquantity = req.getParameter(&quot;quantity&quot;);
int quantity = Integer.parseInt(Tquantity);
A.updateQuantity(quantity);
} //end if statement
}//end loop

You can make another function which will delete a Vector element by passing the element number:
// delete is an interger
ShopCartHere.removeElementAt(delete);

this should get you started.

As for your other question I would stick with either client side sessions or server side session IDs. Too many people have cookies turned off..

Bygs





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top