Topic: 求助:java 语法问题

  Print this page

1.求助:java 语法问题 Copy to clipboard
Posted by: jayinseu
Posted on: 2004-09-19 20:50

ShoppingCartItem scitem = (ShoppingCartItem) items.get(bookId);
(ShoppingCartItem) 是什么意思?编译总是通不过
。。。。。。。。。。。。。。。。。。。。。。。。。。。
package mypack;

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;
}
}

2.Re:求助:java 语法问题 [Re: jayinseu] Copy to clipboard
Posted by: lfrick
Posted on: 2004-09-19 21:40

那是强制转型的意思;编译不了是因为没有导入“ShoppingCartItem”这个类

3.Re:求助:java 语法问题 [Re: jayinseu] Copy to clipboard
Posted by: xiaoyu216
Posted on: 2004-09-24 14:24

import java.util.*;
看你的这个路径下有没有ShoppingCartItem这个类,如果没有的话,要用import导入

4.Re:求助:java 语法问题 [Re: jayinseu] Copy to clipboard
Posted by: scottding
Posted on: 2004-09-29 09:39

ShoppingCartItem
这个类一定是你自己定义的类了,java.util.*里面都是JDK系统的类,你的这个类不应该在这下面,看看这个类应该是你自己实现的,确认一下在哪个路径下,导入进来。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923