现在要用java实现网上购物车的功能主要有ShopCar.java和ShopCarItem.java两个实现-----------------------------------------------------------这是ShopCarItem.java代码packagetom....
现在要用java实现网上购物车的功能
主要有ShopCar.java和ShopCarItem.java两个实现
-----------------------------------------------------------
这是ShopCarItem.java代码
package tom.jiafei;
public class ShopCarItem
{Object item;
int quantity;
public ShopCarItem(Object anItem)
{item=anItem;
quantity=1;}
public void incrementQuantity()
{quantity++;}
public void decrementQuantity()
{quantity--;}
public Object getItem()
{return item;}
public int getQuantity()
{return quantity;}
}
编译通过成为.class文件的
------------------------------------------
问题来了
package tom.jiafei;
import java.util.*;
public class ShopCar
{HashMap items=null;
int numberOfItems=0;
public ShopCar()
{items=new HashMap();}
public synchronized void add(String bookid, BookDetail book)
{
if(items.containsKey(bookid))
{ShopCarItem scitem=(ShopCarItem)items.get(bookid);
scitem.incrementQuantity();}
else{ShopCarItem newItem=new ShopCarItem(book);
items.put(bookid,newItem);}
numberOfItems++;
}
public synchronized void remove(String bookid)
{
if(items.containsKey(bookid)){
ShopCarItem scitem=(ShopCarItem)items.get(bookid);
scitem.decrementQuantity();
if(scitem.getQuantity()<=0)
items.remove(bookid);
numberOfItems--;
}
}
public synchronized Collection getItems()
{return items.values();}
protected void finalize() throws Throwable{
items.clear();
}
public synchronized int getNumberOfItems()
{return numberOfItems;}
public synchronized double getTotal()
{ double amount=0.0;
for(Iterator i=getItems().iterator();i.hasNext();)
{ShopCarItem item=(ShopCarItem)i.next();
BookDetail bookDetails=(BookDetail)item.getItem();
amount+=item.getQuantity()*bookDetails.getPrice();
}
return roundOff(amount);
}
private double roundOff(double x)
{long val=Math.round(x*100);
return val/100.0;}
public synchronized void clear()
{
items.clear();
numberOfItems=0;
}
}
这是ShopCarItem.java文件
-------------------------------------------
这是dos截图
一直说BookDetail book和ShopCarItem找不到,大家帮帮忙
第一个错明白了,第二个错能不能讲详细点
展开