📄 carttestclient1.java~6~
字号:
package ejb;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.lang.String;
import java.util.Date;
import java.util.List;
import java.util.Calendar;
import java .util.GregorianCalendar;
public class CartTestClient1 {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private Cart cart = null;
private CartHome cartHome = null;
//Construct the EJB test client
public CartTestClient1() {
initialize();
}
public void initialize() {
try {
//get naming context
Context context = new InitialContext();
//look up jndi name
Object ref = context.lookup("Cart");
//look up jndi name and cast to Home interface
cartHome = (CartHome) PortableRemoteObject.narrow(ref, CartHome.class);
} catch (Exception e) {
e.printStackTrace();
}
}
//----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//----------------------------------------------------------------------------
public Cart create(String cardHolderName, String creditCardNumber,
Date expirationDate) {
try {
cart = cartHome.create(cardHolderName, creditCardNumber,
expirationDate);
} catch (Exception e) {
e.printStackTrace();
}
return cart;
}
//----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//----------------------------------------------------------------------------
public void addItem(Item item) {
if (cart == null) {
System.out.println("Error in addItem(): " + ERROR_NULL_REMOTE);
return;
}
try {
cart.addItem(item);
} catch (Exception e) {
e.printStackTrace();
}
}
public void removeItem(Item item) {
if (cart == null) {
System.out.println("Error in removeItem(): " + ERROR_NULL_REMOTE);
return;
}
try {
cart.removeItem(item);
} catch (Exception e) {
e.printStackTrace();
}
}
public List getContents() {
List returnValue = null;
if (cart == null) {
System.out.println("Error in getContents(): " + ERROR_NULL_REMOTE);
return returnValue;
}
try {
returnValue = cart.getContents();
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public float getTotalPrice() {
float returnValue = 0f;
if (cart == null) {
System.out.println("Error in getTotalPrice(): " + ERROR_NULL_REMOTE);
return returnValue;
}
try {
returnValue = cart.getTotalPrice();
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public void purchase() {
if (cart == null) {
System.out.println("Error in purchase(): " + ERROR_NULL_REMOTE);
return;
}
try {
cart.purchase();
} catch (Exception e) {
e.printStackTrace();
}
}
//----------------------------------------------------------------------------
// Utility Methods
//----------------------------------------------------------------------------
public CartHome getHome() {
return cartHome;
}
private static void summarize(Cart cart) throws Exception{
System.out.println("====================Cart Summary===================");
List items = cart.getContents();
for(int i=0,n=items.size();i<n;i++){
Item current = (Item)items.get(i);
System.out.println("Price:$"+current.getPrice()+"\t"+current.getType()+"\t"+current.getTitle());
}
System.out.println("Total:$"+cart.getTotalPrice());
System.out.println("====================================================");
}
//Main method
public static void main(String[] args) {
CartTestClient1 client = new CartTestClient1();
CartHome home = client.getHome();
String cardHolderName = "Suzy Programmer";
String creditCardNumber = "1234-5678-9012-3456";
Date expirationDate = new GregorianCalendar(2004, Calendar.JULY, 30).getTime();
Cart cart = null;
try {
cart = home.create(cardHolderName, creditCardNumber, expirationDate);
} catch (Exception e) {
System.out.println("Could not create Cart session bean\n" + e);
}
Item kassemBook = new Item("J2EE Blueprints", 39.99f, "Book");
try {
cart.addItem(kassemBook);
} catch (Exception e) {
System.out.println("Could not add the book to the cart\n" + e);
}
Item milesAlbum = new Item("Kind of Blue", 11.97f, "CD");
try {
cart.addItem(milesAlbum);
} catch (Exception e) {
System.out.println("Could not add the CD to the cart\n" + e);
}
try {
summarize(cart);
} catch (Exception e) {
System.out.println("Could not summarize the items in the cart\n" + e);
}
try {
cart.removeItem(kassemBook);
} catch (Exception e) {
System.out.println("Could not remove the book from the cart\n" + e);
}
Item calvertBook = new Item("Charles Calvert's Learn JBuilder 7", 49.95f, "Book");
try {
cart.addItem(calvertBook);
} catch (Exception e) {
System.out.println("Could not add book to the cart\n" + e);
}
try {
summarize(cart);
} catch (Exception e) {
System.out.println("Could not summarize the items in the cart\n" + e);
}
try {
cart.purchase();
} catch(Exception e) {
System.out.println("Could not purchase the items:\n" + e);
}
try {
cart.remove();
} catch(Exception e) {
System.out.println("Could not remove the Cart bean\n" + e);
}
// Use the getHome() method of the client object to call Home interface
// methods that will return a Remote interface reference. Then
// use that Remote interface reference to access the EJB.
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -