The followings are the 3 files.
Can you debug the problem as I mentioned earlier.
Thanks in advance.
File no.1--videoShop.java
package video;
import java.lang.*;
import java.util.*;
public class videoShop {
protected Hashtable items = new Hashtable();
public videoShop() { }
public void addItem(
String itemId, String desc, float price, int quantity) {
String[] item = {itemId, desc, Float.toString(price), Integer.toString(quantity)};
if(items.containsKey(itemId)) { //what if loop is doing.
String [] tmpItem = (String[])items.get(itemId);//why type casting ?
int tmpQuant=Integer.parseInt(tmpItem[3]);
quantity+=tmpQuant;
tmpItem[3]=Integer.toString(quantity);
}
else {
items.put(itemId,item);
}
}
public void removeItem(String itemId) {
if(items.containsKey(itemId)) {
items.remove(itemId);
}
}
public void updateQuantity(String itemId, int quantity) {
if(items.contains(itemId)) {
String[] tmpItem = (String[])items.get(itemId);
tmpItem[3]=Integer.toString(quantity);
}
}
public Enumeration getEnumeration() {
return items.elements();
}
public float getCost() {
Enumeration enum = items.elements();
String[] tmpItem;
float totalCost = 0.00f;
while(enum.hasMoreElements()) {
tmpItem = (String[])enum.nextElement();
totalCost+=(Integer.parseInt(tmpItem[3])*Float.parseFloat(tmpItem[2]));
}
return totalCost;
}
public int getNumOfItems() {
Enumeration enum = items.elements();
String[] tmpItem;
int numOfItems=0;
while(enum.hasMoreElements()) {
tmpItem=(String[])enum.nextElement();
numOfItems+=Integer.parseInt(tmpItem[3]);
}
return numOfItems;
}
}
File no2--AddToVideoShop.jsp
<%@ page language="java" import"videoShop"%>
<jsp:useBean id="cart" scope="session" class="videoShop"/>
<html>
<head><title>VCD/DVD MENU</title></head>
<body>
<!--if there is new item to the request add it to the cart-->
<% String id = request.getParameter("id");
if(id!=null) {
String desc=request.getParameter("desc");
Float price=new Float(request.getParameter("price");
cart.addItem(id,desc,price.floatValue(),1);
}
%>
<!--print the current quantity of the shoppping cart-->
<hr>
<center><h3>VCD/DVD MENU</h3></CENTER>
<table border="1" width="300" cellspacing="0" cellpadding="2" align="center">
<tr>
<th>Name</th>
<th>Cost</th>
</tr>
<tr>
<form action="AddToVideoShop.jsp" method="post">
<td>where Eagles Dare</td>
<td>$20.00</td>
<td><input type="submit" name="Submit" value="Add"></td>
<input type="hidden" name="id" value="1">
<input type="hidden" name="desc" value="where Eagles dare">
<input type="hidden" name="price" value="20.00">
</form>
</tr>
<tr>
<form action="AddToVideoShop.jsp" method="post">
<td>Ghost Busters</td>
<td>$18.45</td>
<td><input type="submit" name="Submit" value="Add"></td>
<input type="hidden" name="id" value="2">
<input type="hidden" name="desc" value="Ghost Busters">
<input type="hidden" name="price" value="18.45">
</form>
</tr>
<tr>
<form action="AddToVideoShop.jsp" method="post">
<td>Mrs.DoubtFire</td>
<td>$21.00</td>
<td><input type="submit" name="Submit" value="Add"></td>
<input type="hidden" name="id" value="3">
<input type="hidden" name="desc" value="Mrs.DoubtFire">
<input type="hidden" name="price" value="21.50">
</form>
</tr>
<tr>
<form action="AddToVideoShop.jsp" method="post">
<td>Tomorrow Never Dies</td>
<td>$18.00</td>
<td><input type="submit" name="Submit" value="Add"></td>
<input type="hidden" name="id" value="4">
<input type="hidden" name="desc" value="Tomorrow Never Dies">
<input type="hidden" name="price" value="18.75">
</form>
</tr>
<tr>
<form action="AddToVideoShop.jsp" method="post">
<td>Mission Impossible</td>
<td>$20.00</td>
<td><input type="submit" name="submit" value="Add"></td>
<input type="hidden" name="id" value="5">
<input type="hidden" name="desc" value="Mission Impossible">
<input type="hidden" name="price" value="20.00">
</form>
</tr>
</table>
</body>
</html>
File no.3--videoShop.jsp
<%@ page language="java" import="videoShop"%>
<%@ page import="java.util.*"%>
<jsp:useBean id="cart" scope="session" class="videoShop"%>
<html>
<head><title>sharp videos</title></head>
<body
<center>
<table width="300" border="1" cellspacing="0" cellpadding="2" border="0">
<caption><b>video shop content</b></caption>
<tr>
<th>Name</th>
<th>cost</th>
<th>Quantity</th>
</tr>
<%
Enumeration enum=cart.getEnumeration();
String[] tmpItem;
While(enum.hasMoreElements()) {
tmpItem=(String[])enum.nextElement();
%>
<tr>
<td><%=tmpItem[1]%></td>
<td align="center">$<%=tmpItem[2]%></td>
<td align="center"><%=tmpItem[3]%></td>
</tr>
<%
}
%>
</table>
</center>
</body>
</html>