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 ]