📄 shoppingcartbean.java
字号:
// set the quantity, updates the quantity if the item is already there
//in the cart
cartItem.setQuantity( qty + cartItem.getQuantity() );
}
catch( Exception ex ) {
ex.printStackTrace();
ex.fillInStackTrace().printStackTrace();
throw new CartException( "Unexpected error adding item to cart" +
ex.getMessage(),"error.cart.unknown" );
}
}
public void ejbCreate() throws CreateException{
// initialize contents
cartContents = new HashMap();
}
/**
* Container call back, called just after instance of this bean is activated
*/
public void ejbActivate() {
}
/**
* The business methods returns the current shipping address
* @return <b>Address</b> - the current shipping address
* If the user has not been identified yet, returns null.
* @throws <b>CartException</b> if the shipping address cannot be found
*/
public Address getShippingAddress() throws CartException {
try {
// If the shipping address is not already there and the user of the
// cart has been identfied,
if( shippingAddress == null && userName != null ) {
// find the user
CustomerLocalHome customerHome = (CustomerLocalHome)ServiceLocator.
getLocator().getService("java:comp/env/ejb/UserLocal");
CustomerLocal customer = customerHome.findByPrimaryKey( userName );
// Get the user's address and create an Address object
shippingAddress = new Address();
shippingAddress.setAddress( customer.getAddress() );
shippingAddress.setCity( customer.getCity() );
shippingAddress.setState( customer.getState() );
shippingAddress.setCountry( customer.getCountry().getId());
shippingAddress.setZip( customer.getZip().intValue() );
shippingAddress.setPhone( customer.getPhone() );
}
}
catch( Exception ex ) {
ex.printStackTrace();
throw new CartException( "Unable to get shipping address." +
ex.getMessage(),"error.cart.shippingaddress" );
}
// return the address
return shippingAddress;
}
/**
* Container call back, called just before instance of this bean is passivated
*/
public void ejbPassivate() {
}
/**
* Sets the username for the cart
* @param <b>userName</b> String username
*/
public void setUserName( String userName ) {
this.userName = userName;
}
/**
* Container call back, called by container before it ends the life
* of the session object.
*/
public void ejbRemove() {
}
/**
* Gets the cart contents
* @return <b>CartItem[]</b> array of cart item objects
*/
public CartItem[] getCart() {
return (CartItem[])cartContents.values().
toArray( new CartItem[cartContents.size()] );
}
/**
* Set the associated session context.
* The container calls this method after the instance creation.
* @param ctx - A SessionContext interface for the instance.
*/
public void setSessionContext( SessionContext ctx ) {
this.context = ctx;
}
/**
* The business methods updates the cart with new items/quantities
* @param <b>itemIDs</b> - ids of the items to be added/updated
* @param <b>newQties</b> quantities to be updated
* @throws <b>CartException</b> if the item cannot be updated
*/
public void updateCart( String[] itemIDs, int[] newQties ) throws CartException {
// for each id and quantity, call addToCart().
CartItem cartItem =null;
for( int i = itemIDs.length - 1;i >= 0;i-- ) {
cartItem = (CartItem)cartContents.get(itemIDs[i]);
// addToCart adds the quantity to current quantity,
// but we want to replace the quantity with new quantity
addToCart( itemIDs[i],newQties[i]-cartItem.getQuantity() );
}
}
/*
* Returns the local home for OrderItem entity
* @return <b>OrderItemLocalHome</b> local home interface for OrderItem
*/
private OrderItemLocalHome getOrderItemLocalHome() {
return (OrderItemLocalHome)ServiceLocator.getLocator().
getService( "java:comp/env/ejb/OrderItemLocal" );
}
/*
* Returns the local home for Order entity
* @return <b>OrderLocalHome</b> local home interface for Order
*/
private ProductOrderLocalHome getOrderLocalHome() {
return (ProductOrderLocalHome)ServiceLocator.getLocator().
getService("java:comp/env/ejb/OrderLocal");
}
/*
* Returns the local home for ItemLocal entity
* @return <b>ItemLocalLocalHome</b> local home interface for ItemLocal
*/
private ItemLocalHome getItemLocalHome() {
return (ItemLocalHome)ServiceLocator.getLocator().
getService( "java:comp/env/ejb/ItemLocal" );
}
/*
* Returns the local home for Customer entity
* @return <b>UserLocalHome</b> local home interface for Customer
*/
private CustomerLocalHome getUserLocalHome() {
return (CustomerLocalHome)ServiceLocator.getLocator().
getService("java:comp/env/ejb/UserLocal");
}
/*
* Returns the local home for ItemDetail entity
* @return <b>UserLocalHome</b> local home interface for Customer
*/
private ItemDetailLocalHome getItemDetailLocalHome() {
return (ItemDetailLocalHome)ServiceLocator.getLocator().
getService( "java:comp/env/ejb/ItemDetailLocal" );
}
/**
* Sets the locale of the user. The locale is used to determine language of
* item names and currency
* @param <b>String locale</b> user's locale
*/
public void setUserLocale(Locale locale) {
userLocale = locale;
}
/**
* Returns the locale of the user.
* @return <b>String</b> user's locale
*/
public Locale getUserLocale() {
return userLocale;
}
/**
* Get the tax amount for the purchase done
* @return <b>double</b> The tax amount
*/
public double getTaxAmount(){
return Utilities.round(Constants.TAX * getTotalAmountBeforeTax(),2);
}
/**
* Get the shipping charges for the purchase
* @return <b>double</b> The shipping charges
*/
public double getShippingCharges(){
return Utilities.round(
Constants.SHIPPING_CHARGES*getTotalAmountBeforeTax(),2);
}
/**
* Get the total amount of purchase, before tax and shipping charges
* @return <b>double</b> The total amount
*/
public double getTotalAmountBeforeTax(){
CartItem[] cart= getCart();
double amount=0;
for (int i = cart.length-1; i >=0 ; i--) {
amount+=cart[i].getAmount();
}
return amount;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -