这是代码:
package cart;import javax.ejb.*;
import java.util.*;
import java.rmi.*;public interface Cart extends javax.ejb.EJBObject {
public void addGood(String goodsName,double goodsQuantity) throws RemoteException;
public boolean minusGood(String goodsName,double goodsQuantity) throws RemoteException;
public String[][] showOrder() throws RemoteException;
public double getPrices(String goodsName) throws RemoteException;
public double getQuantity(String goodsName) throws RemoteException;
public void removeOneGood(String goodsName) throws RemoteException;
public void removeGoods() throws RemoteException;
public Hashtable getGoods() throws RemoteException;}
package cart;import javax.ejb.*;
import java.util.*;
import java.rmi.*;public interface Cart extends javax.ejb.EJBObject {
public void addGood(String goodsName,double goodsQuantity) throws RemoteException;
public boolean minusGood(String goodsName,double goodsQuantity) throws RemoteException;
public String[][] showOrder() throws RemoteException;
public double getPrices(String goodsName) throws RemoteException;
public double getQuantity(String goodsName) throws RemoteException;
public void removeOneGood(String goodsName) throws RemoteException;
public void removeGoods() throws RemoteException;
public Hashtable getGoods() throws RemoteException;}
import java.util.*;public class CartBean implements SessionBean {
SessionContext sessionContext;
Hashtable goods;
Hashtable goodsInOrder;
String clientID;
public void ejbCreate(String clientID) throws CreateException {
/**@todo Complete this method*/
if(clientID.trim().length()==0){
throw new CreateException("顾客标识不能为空");
}
this.clientID=clientID;
goods.put("商品1",new Double(20.6));
goods.put("商品2",new Double(32.5));
goods.put("商品3",new Double(27.8));
goods.put("商品4",new Double(29.5));
goods.put("商品5",new Double(48.5));
goods.put("商品6",new Double(61.1));
System.out.println(clientID+"进入商场");
}
public void ejbRemove() {
/**@todo Complete this method*/
System.out.println(clientID+"离开商场");
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public Hashtable getGoods() {
return goods;
} public void addGood(String goodsName,double goodsQuantity) {
/**@todo Complete this method*/
if(goodsInOrder.containsKey(goodsName)){
double oriGoodsQuantity=getQuantity(goodsName);
goodsInOrder.remove(goodsName);
goodsInOrder.put(goodsName,new Double(oriGoodsQuantity+goodsQuantity));
}else{
goodsInOrder.put(goodsName,new Double(goodsQuantity));
} }
public boolean minusGood(String goodsName,double goodsQuantity) {
/**@todo Complete this method*/
if(goodsInOrder.containsKey(goodsName)){
double oriGoodsQuantity=getQuantity(goodsName); if(oriGoodsQuantity>=goodsQuantity){
goodsInOrder.remove(goodsName);
goodsInOrder.put(goodsName,new Double(oriGoodsQuantity-goodsQuantity));
return true;
}else{
return false;
}
}else{
return false;
} }
public String[][] showOrder() {
/**@todo Complete this method*/
String[][] order=new String[goodsInOrder.size()+1][4];
Enumeration goodsNames=goodsInOrder.keys();
String goodsName="";
double price;
double quantity;
double amount=0; if(goodsInOrder.isEmpty()){
order[0][0]="尚没有选择任何商品";
}else{
int i=0;
while(goodsNames.hasMoreElements()){
goodsName=(String)goodsNames.nextElement();
price=getPrices(goodsName);
quantity=getQuantity(goodsName);
order[i][0]=goodsName;
order[i][1]=String.valueOf(price);
order[i][2]=String.valueOf(quantity);
order[i][3]=String.valueOf(price*quantity);
amount+=price*quantity;
i++; }
order[i][0]="总价格";
order[i][1]=String.valueOf(amount);
}
return order;
}
public double getPrices(String goodsName) {
/**@todo Complete this method*/
double price=((Double)goods.get(goodsName)).doubleValue();
return price; }
public double getQuantity(String goodName) {
/**@todo Complete this method*/
try{
double quantity=((Double)goodsInOrder.get(goodName)).doubleValue();
return quantity;
}catch(Exception ex){
return 0;
}
}
public void removeOneGood(String goodsName) {
/**@todo Complete this method*/
goodsInOrder.remove(goodsName); }
public void removeGoods() {
/**@todo Complete this method*/
goodsInOrder.clear();
}
}
import java.util.*;
import java.rmi.*;public interface CartHome extends javax.ejb.EJBHome {
public Cart create(String clientID) throws CreateException, RemoteException;
}