⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 carttestclient.java~1~

📁 EJB 实例
💻 JAVA~1~
字号:
package ejb;

import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.lang.String;
import java.util.Date;
import java.util.List;

public class CartTestClient {
    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 CartTestClient() {
        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;
    }

    //Main method
    public static void main(String[] args) {
        CartTestClient client = new CartTestClient();
        // 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 + -