ShoppingCartItem.java表示购物车种每一个物品,物品的详细情况BookDetails,以及数量quantity。package mypack;
public class ShoppingCartItem
{
BookDetails item;
int quantity;
public ShoppingCartItem(BookDetails anItem)
{
item=anItem;
quantity=0;
}
public void incrementQuantity()
{
quantity++;
}
public void decrementQuantity()
{
quantity--;
}
public BookDetails getItem()
{
return item;
}
public int getQuantity()
{
return quantity;
}
}
public class ShoppingCartItem
{
BookDetails item;
int quantity;
public ShoppingCartItem(BookDetails anItem)
{
item=anItem;
quantity=0;
}
public void incrementQuantity()
{
quantity++;
}
public void decrementQuantity()
{
quantity--;
}
public BookDetails getItem()
{
return item;
}
public int getQuantity()
{
return quantity;
}
}
解决方案 »
- 时间转换问题
- Java(jsp)高手进来帮我来看看问题。。。
- 请推荐几种比较灵活运用的java生成excel的工具
- 昨天找到工作(2月1号上班报到)顺带问下谁有C - C 的项目开发经验。散分
- JSP中有若干个同名的checkbox,他们的值不同,我想循环去到所有同名的checkbox的值,请问如何做?
- 汉字排序问题.
- java在Aix系统websphere服务器上面部署问题
- 写jsp程序是否需要很好的html基础
- 设置了404出错页面但是怎么也跳不过去?
- 如何通过post请求获取知乎的登录信息
- InputStream和FileInputStream的区别,OutputStream和FileOutputStream的区别??,欢迎讨论!!
- JSP画面textarea和combox的初始值问题 (新手)
import java.util.*;public class ShoppingCart {
HashMap items = null;
int numberOfItems = 0; public ShoppingCart() {
items = new HashMap();
} public synchronized void add(String bookId, BookDetails book) {
if(items.containsKey(bookId)) {
ShoppingCartItem scitem = (ShoppingCartItem) items.get(bookId);
scitem.incrementQuantity();
} else {
ShoppingCartItem newItem = new ShoppingCartItem(book);
items.put(bookId, newItem);
} numberOfItems++;
} public synchronized void remove(String bookId) {
if(items.containsKey(bookId)) {
ShoppingCartItem scitem = (ShoppingCartItem) 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(); ) {
ShoppingCartItem item = (ShoppingCartItem) i.next();
BookDetails bookDetails = (BookDetails) item.getItem(); amount += item.getQuantity() * bookDetails.getPrice();
}
return roundOff(amount);
} private double roundOff(double x) {
long val = Math.round(x*100); // cents
return val/100.0;
} public synchronized void clear() {
items.clear();
numberOfItems = 0;
}
}