This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

trouble with remove item fro shopping cart

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi.. i'm an amatuer student who just learnt abt JSP.. i was given a project tot do shopping cart.. and it's due soon.. i need help regarding my codes.. i try so many methods but it doesn't work.. there is problem with my remove item.. when i try to remove an item.. it does not remove the item.. can somebody help me with my codes.. is there something wrong with my JavaBeans??

i have 2 javafiles..

Product.java


package product;

public class Product {
String id, name, size, colour, other;
int quantity;
double price, total;

public Product(String newid, String newname, String newsize, String newcolour, String newother, double newprice, int newqty)
//public Product(String newid, String newname, double newprice)

{
id= newid;
name= newname;
size= newsize;
colour= newcolour;
other = newother;
price = newprice;
quantity= newqty;
}

public String getId()
{
return id;
}

public String getName()
{
return name;
}

public String getSize()
{
return size;
}

public String getColour()
{
return colour;
}

public String getOther()
{
return other;
}

public int getQuantity()
{
return quantity;
}

public double getPrice()
{
return price;
}

public double getTotal()
{
return (price*quantity);
}
}



ShoppingCart .java

package product;
import java.util.*;

public class ShoppingCart {
int noItems = 0;
Vector products = new Vector();

public void addProduct(Product i){
products.addElement(i);
}

public void deleteProduct(String id){
Enumeration productEnum = getProducts();
while(productEnum.hasMoreElements()){
Product product = (Product)productEnum.nextElement();
if(product.getId().equals(id)){
//products.removeElement(id);
products.removeElement(product.id);
break;
}
}
}

public void emptyCart(){
products.clear();

}

public int getNoProducts(){
return products.size();
}

public Enumeration getProducts(){
return products.elements();

}

public double getTotal(){
Enumeration productEnum = getProducts();
double total=0;

while(productEnum.hasMoreElements()){
Product product = (Product)productEnum.nextElement();
total = total + product.getTotal();
}
return total;
}

}




viewsc1.jsp


<%@page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>

<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet" type="text/css">
<TITLE>viewsc1.jsp</TITLE>
</HEAD>
<BODY>
<jsp:useBean id="sCart" class="product.ShoppingCart" scope="session"/>
<%@page import="product.Product"%>


<table width="80%" border="0" align="center">
<tr>
<td align="center"><img src="image/title.jpg" width="450" height="80"></td>
</tr>
<tr>
<td align="center" height="90"> </td>
</tr>
<tr>
<td align="center" height="210">

<table width="98%" border="1" height="142">
<tr>
<td height="129">
<table border="0" width="100%">
<tr>
<td colspan="9" bgcolor="#CCCC99" align="center" height="22">
<img src="image/vsc.jpg" width="300" height="50">
</td>
</tr>
<tr>
<td height="15" colspan="9"></td>
</tr>



<%
String name= request.getParameter("name");
if(name!= null){
if(name.equals("Empty")){
sCart.emptyCart();
%>
<SCRIPT>
alert("Your Shopping Cart is empty.");
location.href="chooseCategory.jsp";
</SCRIPT>
<%}else if(name.equals("delete")){
String index = request.getParameter("Id");
sCart.removeItem(index);
out.println(index);

}else if (name.equals("update")){
String[] prodQtys = request.getParameterValues("newQty");
// sCart.updateQtys(prodQtys);
}
}


if(sCart.getNoProducts()!=0){%>

<tr>
<td height="15" align="center" width="20%"><B>Item ID</B></td>
<td height="15" align="center" width="20%"><B>Item Name</B></td>
<td height="15" align="center" width="11%"><b>Size</b></td>
<td height="15" align="center" width="9%"><B>Colour</b></td>
<td height="15" align="center" width="13%"><b>Price</b></td>
<td height="15" align="center" width="10%"><b>Qty</b></td>
<td height="15" align="center" width="12%"></td>
<td height="15" align="center" width="12%"><b>Total:</b></td>
<td height="15" align="center" width="12%"></td>
</tr>

<%//<FORM action="viewsc1.jsp?name=update" method=POST>
Enumeration products =sCart.getProducts();
while(products.hasMoreElements()){
Product product = (Product)products.nextElement();
if(product.getQuantity()!=0){
%>

<tr>
<td height="15" align="center" width="20%"><B><%=product.getId() %></B></td>
<td height="15" align="center" width="11%"><b><%=product.getName() %></b></td>
<td height="15" align="center" width="9%"><B><%=product.getSize() %></b></td>
<td height="15" align="center" width="9%"><B><%=product.getColour() %></b></td>
<td height="15" align="center" width="13%"><b><%=product.getPrice() %></b></td>
<td height="15" align="center" width="9%"><input type=text name="newQty" value="<%=product.getQuantity()%>">
<a href="editQty.jsp?id=<%=product.getId() %>&name=<%=product.getName() %>&qty=<%=product.getQuantity()%>">Edit</a>
</td>
<td height="15" align="center" width="9%"><B><%=product.getOther() %></b></td>
<td height="15" align="center" width="10%"><b><%=product.getTotal() %></b></td>
<td align='center'><a href="viewsc1.jsp?name=delete&Id=<%=product.getId() %>">Remove</a></td>
</tr>

<%}else{ %>
<tr>
<td height="15" align="center" width="20%"><B><%=product.getId() %></B></td>
<td height="15" align="center" width="11%"><b><%=product.getName() %></b></td>
<td height="15" align="center" width="9%"><B><%=product.getSize() %></b></td>
<td height="15" align="center" width="9%"><B><%=product.getColour() %></b></td>
<td height="15" align="center" width="13%"><b><%=product.getPrice() %></b></td>
<td height="15" align="center" width="9%"><B><%=product.getQuantity() %></b></td>
<td height="15" align="center" width="9%"><B><%=product.getOther() %></b></td>
<td height="15" align="center" width="10%"><b><%=product.getTotal() %></b></td>
<td align='center'><a href="viewsc1.jsp?name=delete&Id=<%=product.getId() %>">Remove</a></td>
</tr>
<%}

} %>

<tr>
<td height="50" valign="bottom" colspan="9" height="15" align="right" width="12%"><b>Total:</b><%=sCart.getTotal() %></td>
</tr>

<%
}else{%>
<SCRIPT>
alert("Your Shopping Cart is empty.");
location.href="chooseCategory.jsp";
</SCRIPT>
<%}
%>
<tr>
<td colspan="9" height="15" align="center">
<input type=submit value='Update Quantity'>
<a href=chooseCategory.jsp><IMG border="0" src="image/BtnContinueshop.gif" width="153" height="27"></a>
<a href="viewsc1.jsp?name=Empty"><IMG border="0" src="image/BtnEmptyBsk.gif" width="75" height="27"> </a>
<a href=checkout.jsp><IMG border="0" src="image/BtnCheckOut.gif" width="156" height="27"></a>
</td>
</tr>
</form>
</table>

</BODY>
</HTML>
[ February 03, 2005: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you want to remove an item from a Collection, iterate through the collection from back to front in order to keep the elements you've not yet seen in their positions.

Something like


OK, in this code we're removing elements from a collection which are indicated by a checkbox that's turned on.
What the actual classes are you don't need to know, they're all custom classes to our application (even the Collection we use I created myself because I had a need to track items that are removed so they can be at a later stage deleted from the database (the entire application works on the basis of shortterm transactions, so all mutations to a record are collated in the application itself and then executed on the database all at once when the user clicks a button).

I iterate back to front through all the checkboxes. When the checkbox is marked first the item from the collection which corresponds to that checkbox is removed, next the checkbox itself is removed from the collection of checkboxes. Last but not least the counter indicating the posibion in the loop is reduced as well, this is vital in this scenario as otherwise you'd end up skipping an item on every delete.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic